I am searching a way to combine two plots, box plot and point plot, grouped by multiple variables using ggplot
in r. Two plots use different y variables.
Here are two sets of code, which are working separately. For the second code, I do not use "geom_point" directly, but use stat_summary to average point values.
I would like to combine two plots to have one plot. Thank you very much for any suggestion, in advance! :)
dput(head(mydf_sample))
structure(list(season = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("",
"Fall", "Spring", "Summer", "Winter"), class = "factor"), meteo2 =
structure(c(3L,
3L, 3L, 2L, 3L, 3L), .Label = c("", "E", "N", "S", "W"), class = "factor"),
meteo = structure(c(7L, 7L, 7L, 7L, 8L, 8L), .Label = c("",
"<40", "<50", "<60", "<70", "<75", "<80", "80+"), class = "factor"),
hour = 0:5, var1 = c(41.16666667, 25.05, 22.45, 26.31666667,
25.2, 19.65)), .Names = c("season", "meteo2", "meteo", "hour", "var1"), row.names = c(NA, 6L), class = "data.frame")
## box ##
a<-ggplot(data = mydf, aes(x = hour, y = var1)) + stat_summary(fun.y = mean, geom = "line",
aes(hour = 1)) +
geom_boxplot(colour="blue")+
facet_grid(season~meteo2)+
labs(list(title="", x="hour", y="var1"))+
theme_bw()
a
## averaged point ##
b<-ggplot(data=mydf, aes(x=hour,y=meteo))+
stat_summary(fun.y=mean, geom=c("point"), position = position_dodge(width =1), size=2)+
stat_summary(geom = "errorbar", fun.data = mean_se, position = "dodge")+
facet_grid(season~meteo2)+
labs(list(title="", x="hour", y="meteo"))+
theme_bw()
b