Does it do what you hoped for?
p + geom_density(aes(x=scale(y1)), fill=NA) + geom_density(aes(x=scale(y2)), alpha=0.3,col=NA,fill='red')
The scale function with a single data argument will center the empirical distribution by 0 and then divide the resulting values by the standard deviation of the sample so that the result has a standard deviation of 1. You can change the default values for the location and the degree of “compression” or “expansion”. You will probably need to investigate the placement of the corresponding x_scales for y1 and y2. This may require some pre-processing with a scale. The scaling factor is recorded in the attribute of the returned object.
attr(scale(data$y2), "scaled:scale")
source share