Reputation: 143
The following code produces two density plots, first using the R base graphics and the second using ggplot2. The second plot has an artificial peak at the beginning of the curve which is not present in the first plot. The peak at the start is always present when start of the x-axis limit is set to more than zero. Why ggplot makes this peak and how to avoid it?
I can't post images due to lack of reputation points. PLease, try it yourself. This code should work as it is.
library(ggplot2)
set.seed(101)
xval<-rlnorm(n=10000)
xdf<-data.frame(xval)
plot(density(xdf$xval), xlim=c(1, 10))
ggplot(data=xdf, aes(x=xval))+geom_density()+xlim(1, 10)
Is this a bug in ggplot2?
Upvotes: 1
Views: 476
Reputation:
If you change xlim()
for coord_cartesian()
, it works:
library(ggplot2)
set.seed(101)
xval <- rlnorm(n=10000)
xdf <- data.frame(xval)
par(xaxs = "i") # change the style to fix exact x limits to (1, 10)
plot(density(xdf$xval), xlim = c(1, 10))
ggplot(data = xdf, aes(x = xval)) +
stat_density(geom = "line") +
scale_x_continuous(breaks = c(2,4,6,8,10)) +
coord_cartesian(xlim = c(1, 10))
Upvotes: 1