R:置信区间由ggplot2部分显示(使用geom_smooth()) [英] R : confidence interval being partially displayed with ggplot2 (using geom_smooth())
本文介绍了R:置信区间由ggplot2部分显示(使用geom_smooth())的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下简单的R代码:
I have the following simple R code:
disciplines <- c("A","C","B","D","E")
# To stop ggplot from imposing alphabetical ordering on x-axis
disciplines <- factor(disciplines, levels=disciplines, ordered=T)
d1 <- c(0.498, 0.521, 0.332, 0.04, 0.04)
d2 <- c(0.266, 0.202, 0.236, 0.06, 0.06)
d3 <- c(0.983, 0.755, 0.863, 0.803, 0.913)
d4 <- c(0.896, 0.802, 0.960, 0.611, 0.994)
df <- data.frame(disciplines, d1, d2, d3, d4)
df.m <- melt(df)
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")
输出看起来像这样:
The output looks like this:
为什么没有在整个曲线上显示置信区间?
Why does the confidence interval not display along the entire curve?
注意:
- 我不想使用
fullrange=TRUE
,因为那样只会产生一条蓝色的直线,而不是当前输出中的锯齿形. - 我正在将该图与另一个在(0,-1]范围内具有负值的图进行比较,这就是y轴具有
limits=c(-1,1)
)的原因
- I don't want to have
fullrange=TRUE
because that just yields a single straight blue line instead of the zigzag shape in the current output. - I am comparing this plot with another plot which has negative values in the (0,-1] range, which is why the y-axis has
limits=c(-1,1)
)
推荐答案
对于置信区间的前三个分段,范围的顶端至少部分超出范围(范围为[-1,1] ,而不是轴上稍微扩大的范围). ggplot
的默认行为是不显示部分超出范围的任何对象.您可以通过在scale_y_continuous
中添加oob=scales::rescale_none
来解决此问题:
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)
这篇关于R:置信区间由ggplot2部分显示(使用geom_smooth())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文