4

次に例を示します。

eg <- data.frame(x = c(1:50, 50:1),  
                 y = c(1:50, 1:50) + rnorm(100),  
                 g = rep(c("a","b"), each=50))  

qplot(x, y, data = eg) +  
  facet_wrap(~ g) +  
  geom_smooth()  

ファセット固有のスムースだけでなく、両方のファセットで全体的なスムースをプロットできるようにしたいと考えています。

編集:これが1つの方法です。

my.smooth <- gam(y ~ s(x), data = eg)
my.data <- data.frame(x = 1:50)                                           
my.data$y <- predict(my.smooth, newdata = my.data) 

qplot(x, y, data = eg) + 
    facet_wrap(~ g) + 
    geom_smooth() + 
    geom_smooth(data = my.data)

助けてくれてありがとう!

アンドリュー

4

1 に答える 1

6

巧妙なトリック:ファセット変数を NULL に設定する

library(ggplot2)
eg <- data.frame(x = c(1:50, 50:1),  
                 y = c(1:50, 1:50) + rnorm(100),  
                 g = rep(c("a","b"), each=50))  

p <- qplot(x, y, data = eg) +  
  facet_wrap(~ g) +  
  geom_smooth()

p + geom_smooth(data=within(eg, g <- NULL), fill="red")

または、必要に応じて次を使用しますfacet_grid(..., margins=TRUE)

p + facet_grid(.~g, margins=TRUE)
于 2011-12-02T10:12:08.927 に答える