简体   繁体   中英

ggplot2 geom_density plot splits around 0

I have a piece of R code that analyses differences over time in two sensor readings. As such, they center around a mean of 0.

b = 30 # Number of bins
xl = -.025  # Hard axis limit on left x value 
xr = .025  # Hard axis limit on right x value 
yl = 0 # Hard axis limit on left y value (always 0)
yr = 120  # Hard axis limit on right y value

diff1 %>%
   ggplot(aes(x=value, geom="line"), position="identity") +
   geom_histogram(bins=b, fill="#0000FF") + 
   geom_density(alpha=.1, fill="#000000") +
   xlim(xl, xr) +  
   ylim(yl, yr)

I would like to show a continuous distribution overlay using the geom_density function, but I get something like below.

ggplot图像输出

As you see, there is a split on the value around 0, but I can't figure out how I can get rid of it. I tried different AES options, none of them providing fruitful results. I would like to show a single continues geom_density line, instead of two groups. Any help would be greatly appreciated.

As Bas pointed out, the visualisation got cut off by my axis limits. The following code did the trick. Hopefully, my answer can help someone in the future!

diff1 %>%
    ggplot(aes(x=value, geom="line"), position="identity") +
    geom_histogram(bins=b, fill="#0000FF") + 
    geom_density(alpha=.1, fill="#000000") +
    xlim(xl, xr)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM