ggplot2 中的每个面板平滑

发布于 2024-08-10 11:31:29 字数 349 浏览 3 评论 0原文

我正在使用 ggplot2 中的facet 绘制一组曲线。我希望将平滑器应用于有足够平滑点的图,但不适用于点很少的图。特别是,当其中一个面板只有 1 或 2 点时,我想阻止绘图失败。

例子:

a <- data.frame( x=1:100, y=sin(seq(0.1,10,0.1) )) 
b <- data.frame( x=1:5, y=sin(seq(0.1,0.2,0.1) )) 
l <- melt(list(a=a,b=b),id.vars="x") 
qplot( x, value, data=l ) + geom_smooth() + facet_wrap( ~ L1 )

I'm plotting a group of curves, using facet in ggplot2. I'd like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I'd like to stop the plot failing when one of the panels only has 1 or 2 points.

Example:

a <- data.frame( x=1:100, y=sin(seq(0.1,10,0.1) )) 
b <- data.frame( x=1:5, y=sin(seq(0.1,0.2,0.1) )) 
l <- melt(list(a=a,b=b),id.vars="x") 
qplot( x, value, data=l ) + geom_smooth() + facet_wrap( ~ L1 )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美人骨 2024-08-17 11:31:30

试试这个

library(ggplot2)
a <- data.frame( x=1:100, y=sin(seq(0.1,10,0.1) )) 
b <- data.frame( x=1:2, y=sin(seq(0.1,0.2, length = 2) )) 
l <- melt(list(a=a,b=b),id.vars="x") 

more_than <- function(n) {
  function(df)  {
    if (nrow(df) > n) {
      df
    }
  }
}

lbig <- ddply(l, "L1", more_than(5))

qplot( x, value, data=l ) + geom_smooth() + facet_wrap( ~ L1 )
qplot( x, value, data=l ) + geom_smooth(data = lbig) + facet_wrap( ~ L1 )

Try this

library(ggplot2)
a <- data.frame( x=1:100, y=sin(seq(0.1,10,0.1) )) 
b <- data.frame( x=1:2, y=sin(seq(0.1,0.2, length = 2) )) 
l <- melt(list(a=a,b=b),id.vars="x") 

more_than <- function(n) {
  function(df)  {
    if (nrow(df) > n) {
      df
    }
  }
}

lbig <- ddply(l, "L1", more_than(5))

qplot( x, value, data=l ) + geom_smooth() + facet_wrap( ~ L1 )
qplot( x, value, data=l ) + geom_smooth(data = lbig) + facet_wrap( ~ L1 )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文