ggplot2:geom_smooth置信区间不会延伸到图的边缘,即使是fullrange = TRUE也是如此 [英] ggplot2: geom_smooth confidence band does not extend to edge of graph, even with fullrange=TRUE

查看:1301
本文介绍了ggplot2:geom_smooth置信区间不会延伸到图的边缘,即使是fullrange = TRUE也是如此的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直致力于在ggplot2中生成一些散点图,并发现我的geom_smooth色阴影(与stat_smooth相同的问题)不会扩展我的阴谋的全部范围(请参见阴谋图),这使我疯狂。



你可以从代码中看到我使用了fullrange = TRUE,它确实延长了线条本身(以及我的其他合身线上的阴影),但无论出于何种原因,都会妨碍我的一条合身线条。



这似乎是一个问题,它与情节的上边界相冲突。如果我将范围扩展到线条撞到右边界的位置,阴影仍然没有问题,但这样做不是一个选项,因为我必须加倍x和y轴范围才能实现此目的,这会使我的数据。



有没有人知道如何让阴影一直延伸到上轴界线?



解决方案

您可能需要添加 coord_cartesian 除了 scale_x / y_continuous 之外。 scale_x / y_continuous 删除图表范围之外的点,但是 coord_cartesian 覆盖此点并使用所有数据,即使其中的一些在图中不可见。在你的情节中,红色点的置信区间结束于带顶部超过图的y范围。



没有实际的数据扩展的图形范围,但 geom_smooth 会将它生成的点绘制为数据,以便确定要绘制的内容。



请看下面的例子。第一个图只使用 scale_x / y_continuous 。第二个添加 coord_cartesian ,但请注意,置信区间仍未绘制。在第三个图中,我们仍然使用 coord_cartesian ,但是我们将 scale_y_continuous 范围向下展开,以使置信区内的点在零范围内包含在y范围内。然而, coord_cartesian 决定了实际绘制的范围,也防止排除范围之外的点。



实际上我发现这种行为很混乱。我以为你可以单独使用带有所需x和y范围的 coord_cartesian ,并且一直绘制到图的边缘的置信区间和回归线。无论如何,希望这会得到你想要的。

  p1 = ggplot(mtcars,aes(wt, mpg,color = factor(am)))+ 
geom_smooth(fullrange = TRUE,method =lm)+
scale_x_continuous(expand = c(0,0),limits = c(0,10 ))+
scale_y_continuous(expand = c(0,0),limits = c(0,100))+
ggtitle(scale_x / y_continuous)

p2 = ggplot( mtcars,aes(wt,mpg,color = factor(am)))+
geom_smooth(fullrange = TRUE,method =lm)+
scale_x_continuous(expand = c(0,0) = c(0,10))+
scale_y_continuous(expand = c(0,0),limits = c(0,100))+
coord_cartesian(xlim = c(0,10),ylim = c (0,100))+
ggtitle(Add coord_cartesian; same y-range)

p3 = ggplot(mtcars,aes(wt,mpg,color = factor(am)))+
geom_smooth(fullrange = TRUE,method =lm)+
scale_x_continuous(expand = c(0,0),limits = c(0,10))+
scale_y_continuous(expand = c(0,0),limits = c(-50,100))+
coord_cartesian(xlim = c(0,10),ylim = c(0,100))+
g gtitle(添加coord_cartesian;扩展的y范围)

gridExtra :: grid.arrange(p1,p2,p3)


I've been working on generating a few scatterplots in ggplot2 and found that my geom_smooth se shade (same exact issue with stat_smooth) won't extend the full range of my plot (see plot image), which is driving me crazy.

You can see from the code that I have used "fullrange = TRUE" and it does extend the line itself (and the se shade on my other fit line), but for whatever reason stunts the shade on one of my fit lines.

It seems to be an issue with it conflicting with the upper boundary of the plot. If I extend the range to the point that the line hits the right boundary instead, the shade continues with no issues, but doing that is not an option because I have to double the x and y axis ranges to make that happen, which squashes my data.

Does anyone have any idea how to get the shade to extend all the way to the upper axis boundary?

code to produce plot

解决方案

You probably need to add coord_cartesian in addition to scale_x/y_continuous. scale_x/y_continuous removes points that are outside the range of the graph, but coord_cartesian overrides this and uses all of the data, even if some of it is not visible in the graph. In your plot, the confidence band for the red points ends where the top of the band exceeds the y-range of the graph.

There's no actual "data" in the extended range of your graph, but geom_smooth treats the points it generates for plotting the confidence bands as "data" for the purposes of deciding what to plot.

Take a look at the examples below. The first plot uses only scale_x/y_continuous. The second adds coord_cartesian, but note that the confidence bands are still not plotted. In the third plot, we still use coord_cartesian, but we expand the scale_y_continuous range downward so that points in the confidence band below zero are included in the y-range. However, coord_cartesian is what determines the range that's actually plotted and also prevents points outside the range from being excluded.

I actually find this behavior confusing. I would have thought that you could just use coord_cartesian alone with the desired x and y ranges and still have the confidence bands and regression lines plotted all the way to the edges of the graph. In any case, hopefully this will get you what you're looking for.

p1 = ggplot(mtcars, aes(wt, mpg, colour=factor(am))) + 
  geom_smooth(fullrange=TRUE, method="lm") +
  scale_x_continuous(expand=c(0,0), limits=c(0,10)) +
  scale_y_continuous(expand=c(0,0), limits=c(0,100)) +
  ggtitle("scale_x/y_continuous")

p2 = ggplot(mtcars, aes(wt, mpg, colour=factor(am))) + 
  geom_smooth(fullrange=TRUE, method="lm") +
  scale_x_continuous(expand=c(0,0), limits=c(0,10)) +
  scale_y_continuous(expand=c(0,0), limits=c(0,100)) +
  coord_cartesian(xlim=c(0,10), ylim=c(0,100)) +
  ggtitle("Add coord_cartesian; same y-range")

p3 = ggplot(mtcars, aes(wt, mpg, colour=factor(am))) +
  geom_smooth(fullrange=TRUE, method="lm") +
  scale_x_continuous(expand=c(0,0), limits=c(0,10)) +
  scale_y_continuous(expand=c(0,0), limits=c(-50,100)) +
  coord_cartesian(xlim=c(0,10), ylim=c(0,100)) +
  ggtitle("Add coord_cartesian; expanded y-range")

gridExtra::grid.arrange(p1, p2, p3)

这篇关于ggplot2:geom_smooth置信区间不会延伸到图的边缘,即使是fullrange = TRUE也是如此的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆