在ggplot2中的一个窗口上绘制

时间:2015-01-11 09:42:20

标签: r ggplot2 lattice

我有以下代码,生成单独的图。我想在一个网格中得到所有这些图。如何做到这一点。在格子中是否有替代方法来获得类似的图?

 v1 <- rep(c(2,4,6,8,10), each = 6)  
 v2 <- rep(1:3,10)  
 v3 <-runif(30,0.01,0.3)
 combined_data <- data.frame(v1,v2,v3)  
 library(ggplot2)
 ggplot(combined_data,aes(x=v2,y=v3))+
 stat_summary(fun.y=mean,geom="line",color="blue",linetype=2)+
 stat_summary(fun.y=mean,geom="point", pch=1,size=3)+
 scale_x_continuous(breaks=combined_data$v2)+
 facet_grid(~v1)+
 theme_bw()

3 个答案:

答案 0 :(得分:1)

 v1 <- rep(c(2,4,6,8,10), each = 6)  
 v2 <- rep(1:3,10)  
 v3 <-runif(30,0.01,0.3)
 combined_data <- data.frame(v1,v2,v3)  
 library(ggplot2)
 ggplot(combined_data,aes(x=v2,y=v3))+
 stat_summary(fun.y=mean,geom="line",aes(color=as.factor(v1)),linetype=2)+
 stat_summary(fun.y=mean,geom="point",pch=1,size=3,aes(color=as.factor(v1)))+
 scale_x_continuous(breaks=combined_data$v2)+
 #facet_grid(~v1)+
 theme_bw()

enter image description here

答案 1 :(得分:1)

您必须将aes(group = v1)添加到stat_summary

library(ggplot2)
ggplot(combined_data,aes(x = v2, y = v3))+
  stat_summary(fun.y = mean, geom = "line", color = "blue", linetype = 2,
               aes(group = v1))+
  stat_summary(fun.y = mean, geom = "point", pch = 1, size = 3,
               aes(group = v1))+
  scale_x_continuous(breaks = v2)+
  theme_bw()

答案 2 :(得分:0)

我认为这可能就是你想要的。你需要使用ggplot2来处理很多因素。您也可以尝试在第二个stat_summary中使用geom =“line”。

v1 <- rep(c(2,4,6,8,10), each = 6)  
v2 <- rep(1:3,10)  
v3 <-runif(30,0.01,0.3)
combined_data <- data.frame(v1,v2,v3)  
combined_data$f1 = as.factor(v1)
library(ggplot2)
ggplot(combined_data,aes(x=v2,y=v3,color=f1))+
  stat_summary(fun.y=mean,geom="line",color="blue",linetype=2)+
  stat_summary(fun.y=mean,geom="point", pch=1,size=3)+
  scale_x_continuous(breaks=combined_data$v2)+
  #facet_grid(~v1)+
  theme_bw()

收率: enter image description here