R : confidence interval being partially displayed with ggplot2 (using geom_smooth())

前端 未结 2 734
旧时难觅i
旧时难觅i 2021-01-19 10:27

I have the following simple R code:

disciplines <- c(\"A\",\"C\",\"B\",\"D\",\"E\")
# To stop ggplot from imposing alphabetical ordering on x-axis
discipl         


        
相关标签:
2条回答
  • 2021-01-19 11:03

    For the first three segments of the confidence interval, the top end of the range is at least partially out of bounds (the bounds being [-1, 1], not the slightly expanded range on the axes). ggplot's default behavior is to not display any object that is partially out of bounds. You can fix this by adding oob=scales::rescale_none to scale_y_continuous:

    library(scales)
    graph <- ggplot(df.m, aes(group=1,disciplines,value,colour=variable,shape=variable)) +
             geom_point() +
             geom_smooth(stat="smooth", method=loess, level=0.95) +
             scale_x_discrete(name="Disciplines") +
             scale_y_continuous(limits=c(-1,1), name="Measurement", oob=rescale_none)
    
    0 讨论(0)
  • 2021-01-19 11:09

    A better documented, and perhaps more intuitive, solution would be to simply use coord_cartesian:

    ggplot(df.m, aes(group=1,disciplines,value,colour=variable,shape=variable)) +
             geom_point() +
             geom_smooth(stat="smooth", method=loess, level=0.95) +
             scale_x_discrete(name="Disciplines") +
             coord_cartesian(ylim = c(-1,1))
    
    0 讨论(0)
提交回复
热议问题