I'm using geom_density to plot densities with very thin tails. I want to restrict the y-axis range (so the top of the distribution will be off screen and the tail is more clearly visible) but it's throwing away data that is off-screen when it calculate the density, rather than just not showing what is off screen.
E.g.
This plots the full distribution,
testData = data.frame(counts=c(rep(1,5), 1:10))
ggplot(testData, aes(x=testData$counts))+geom_density()
but when the y range is restricted, it looks as though the distribution has smaller support.
ggplot(testData, aes(x=testData$counts))+geom_density()+scale_y_continuous(limits=c(0,0.1))
How can I "zoom in" on the y axis without throwing away data?
To change the axis scales on a plot in base R Language, we can use the xlim() and ylim() functions. The xlim() and ylim() functions are convenience functions that set the limit of the x-axis and y-axis respectively.
Use scale_xx() functions It is also possible to use the functions scale_x_continuous() and scale_y_continuous() to change x and y axis limits, respectively.
The y-axis in a density plot is the probability density function for the kernel density estimation. However, we need to be careful to specify this is a probability density and not a probability. The difference is the probability density is the probability per unit on the x-axis.
I believe you're looking for coord_cartesian():
ggplot(testData, aes(x=testData$counts))+geom_density()+coord_cartesian(ylim=c(0, 0.1))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With