0

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
Asperi
  • 228,894
  • 20
  • 464
  • 690
user2928318
  • 549
  • 3
  • 7
  • 20
  • You can visit [this](https://stackoverflow.com/q/21468380/6123824) and [this](https://stackoverflow.com/q/10493084/6123824). – UseR10085 Apr 16 '20 at 06:29
  • Thanks, but I have a trouble in combining geom_boxplot and stat_summary which use different y values. – user2928318 Apr 16 '20 at 06:43
  • Your data set whatever you have provided is very small showing `geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?`. – UseR10085 Apr 16 '20 at 06:47
  • What do you mean by combine plots? Have different kinds of data in a single plot or arrange two plots into one output file? – L_W Apr 16 '20 at 07:33
  • @Bappa, in that case, I would group by "season" only. – user2928318 Apr 17 '20 at 01:29
  • @L_W, I would prefer a single plot combining a boxplot and a point plot having two y axis . – user2928318 Apr 17 '20 at 01:29
  • Two y-axes are hardly ever a good idea, as they allow for misinterpretation and manipulation. See for example this link to get some ideas for alternatives. https://blog.datawrapper.de/dualaxis/ – L_W Apr 17 '20 at 08:59

0 Answers0