-3

I have the following code, that generate the seperate plots. I want to get all these plots in one grid. How this can be done.? Is there alternative method in lattice to get the similar plots?

 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 Answers3

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

CMichael
  • 1,856
  • 16
  • 20
  • but it shows one line in the graphs...but there are 5 lines (one for each categories of v1....where each individual line is actually a plot of average value of v3 against each category of v2 ). And I want to get all these 5 lines in one graph – user3275911 Jan 11 '15 at 12:12
  • Could it possible to get a similar results in lattice?? – user3275911 Jan 11 '15 at 22:17
1

You have to add aes(group = v1) to 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()
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
0

I think this might be what you want. You need to work with factors a lot with ggplot2. You might try using geom="line" in the second stat_summary as well.

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()

yields: enter image description here

Mike Wise
  • 22,131
  • 8
  • 81
  • 104
  • there are five plots...if I remove the last line...it shows only one plot – user3275911 Jan 11 '15 at 10:07
  • 1
    So I edited my answer. I actually beat CMichael to it by a couple of minutes (I think), but he included a nice plot, which I didn't realize you could do. – Mike Wise Jan 11 '15 at 14:21