【问题标题】:Per panel smoothing in ggplot2ggplot2中的每个面板平滑
【发布时间】:2010-12-13 21:06:11
【问题描述】:

我正在绘制一组曲线,使用 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 )

【问题讨论】:

  • 你检查过这个问题了吗? stackoverflow.com/questions/1570379/…
  • 是的。将数据的子集提供给平滑器并不容易,因为它将被分面 - 我必须弄清楚哪些方面有足够的数据,然后从原始数据中仅对这些方面进行子集化。着色不起作用,因为我正试图阻止更平滑的运行
  • 请提供一个可重现的小例子
  • a
  • 糟糕,这不起作用,我已将其添加到问题中

标签: r ggplot2 smoothing


【解决方案1】:

试试这个

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 )

【讨论】:

  • 太棒了!我以前没有使用过 ddply,也没有向 geoms 提供不同的数据。这打开了一个充满可能性的世界。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-11
  • 1970-01-01
  • 1970-01-01
  • 2015-04-01
  • 2017-04-21
相关资源
最近更新 更多