在ggplot2中仅画出stat_smooth的边界 [英] Drawing only boundaries of stat_smooth in ggplot2

查看:206
本文介绍了在ggplot2中仅画出stat_smooth的边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用 stat_smooth() geom_point 是否有办法去除阴影拟合区域,但只绘制其外边界?我知道我可以用类似的方式去除阴影区域:

  geom_point(aes(x = x,y = y))+ geom_stat(aes(x = x,y = y),alpha = 0)

使它的外部边界(外部曲线)仍然可以看到微弱的黑色线条? 您也可以使用 geom_ribbon fill = NA。

  gg < -  ggplot(mtcars,aes(qsec,wt))+ 
geom_point()+
stat_smooth(alpha = 0,method ='loess')

rib_data< - ggplot_build(gg)$ data [[2]]

ggplot(mtcars)+
stat_smooth(aes(qsec,wt),alpha = 0,method ='loess')+
geom_point(aes(qsec,wt))+
geom_ribbon(data = rib_data,aes x = x,ymin = ymin,ymax = ymax,col ='blue'),
fill = NA,linetype = 1)



...如果由于某种原因,您不想使用竖线,则可以使用两个 geom_line 图层:

  ggplot(mtcars)+ 
stat_smooth(aes(qsec,wt),alpha = 0,method ='loess')+
geom_line(aes(qsec,wt))+
geom_line(data = rib_data,aes(x = x,y = ymax))+
geom_line(data = rib_data,aes(x = x,y = ymin))


When using stat_smooth() with geom_point is there a way to remove the shaded fit region, but only draw its outer bounds? I know I can remove the shaded region with something like:

 geom_point(aes(x=x, y=y)) + geom_stat(aes(x=x, y=y), alpha=0)

but how can I make the outer bounds of it (outer curves) still visible as faint black lines?

解决方案

You can also use geom_ribbon with fill = NA.

gg <- ggplot(mtcars, aes(qsec, wt))+
        geom_point() +  
        stat_smooth( alpha=0,method='loess')

rib_data <- ggplot_build(gg)$data[[2]]

ggplot(mtcars)+
  stat_smooth(aes(qsec, wt), alpha=0,method='loess')+
  geom_point(aes(qsec, wt)) +  
  geom_ribbon(data=rib_data,aes(x=x,ymin=ymin,ymax=ymax,col='blue'),
                fill=NA,linetype=1) 

...and if for some reason you don't want the vertical bars, you can just use two geom_line layers:

ggplot(mtcars)+
    stat_smooth(aes(qsec, wt), alpha=0,method='loess')+
    geom_point(aes(qsec, wt)) + 
    geom_line(data = rib_data,aes(x = x,y = ymax)) + 
    geom_line(data = rib_data,aes(x = x,y = ymin))

这篇关于在ggplot2中仅画出stat_smooth的边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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