0

I am trying to plot streamflow by season and I have a common legend for all the seasons. However, I am having problems in where to locate the legend and how to do that. I am using the following code:

#By Seasons

#Spring
Primavera_Data <- subset(DSF, month>=3 & month <=5)
Primavera_Streamflow_Day <- Primavera_Data %>% group_by(Station,day) %>% summarise(Primavera_Data_Streamflow = mean(DailyMeanStreamflow, na.rm=TRUE))
plot1 <- ggplot(Primavera_Streamflow_Day, aes(x=day, y=Primavera_Data_Streamflow, group = Station, colour = Station)) + 
  geom_line(size = 1,show.legend = FALSE)  + 
  geom_point(size=1.5, shape=21, fill="white",show.legend = FALSE) + 
  labs(y ="Q [m3/s/day]", x = "day", title = "Spring Mean Daily Streamflow (March 1 to May 31)", size = 50) +
  theme(plot.title = element_text(size=16), axis.text.y = element_text(size=11), axis.text.x = element_text(angle = 90, size=11))+
  scale_x_continuous (breaks=seq(min(Primavera_Data$day),max(Primavera_Data$day),by=2))


#Summer
Summer_Data <- subset(DSF, month>=6 & month <=8)
Summer_Streamflow_Day <- Summer_Data %>% group_by(Station,day) %>% summarise(Summer_Data_Streamflow = mean(DailyMeanStreamflow, na.rm=TRUE))
plot2 <- ggplot(Summer_Streamflow_Day, aes(x=day, y=Summer_Data_Streamflow, group = Station, colour = Station)) + 
  geom_line(size = 1,show.legend = FALSE)  + 
  geom_point(size=1.5, shape=21, fill="white",show.legend = FALSE) + 
  labs(y ="Q [m3/s/day]", x = "day", title = "Summer Mean Daily Streamflow (June 1 to August 31)", size = 50) +
  theme(plot.title = element_text(size=16), axis.text.y = element_text(size=11), axis.text.x = element_text(angle = 90, size=11)) +  
  scale_x_continuous (breaks=seq(min(Summer_Data$day),max(Summer_Data$day),by=2))
 


#Fall
Fall_Data <- subset(DSF, month>=9 & month <=11)
Fall_Streamflow_Day <- Fall_Data %>% group_by(Station,day) %>% summarise(Fall_Data_Streamflow = mean(DailyMeanStreamflow, na.rm=TRUE))
plot3 <- ggplot(Fall_Streamflow_Day, aes(x=day, y=Fall_Data_Streamflow, group = Station, colour = Station)) + 
  geom_line(size = 1,show.legend = FALSE)  + 
  geom_point(size=1.5, shape=21, fill="white",show.legend = FALSE) + 
  labs(y ="Q [m3/s/day]", x = "day", title = "Fall Mean Daily Streamflow (September 1 to November 30)", size = 50) +
  theme(plot.title = element_text(size=16), axis.text.y = element_text(size=11), axis.text.x = element_text(angle = 90, size=11)) +
  scale_x_continuous (breaks=seq(min(Fall_Data$day),max(Fall_Data$day),by=2))



#Winter
Winter_Data <- subset(DSF, month %in% c(1,2,12))
unique(Winter_Data$month)
Winter_Streamflow_Day <- Winter_Data %>% group_by(Station,day) %>% summarise(Winter_Data_Streamflow = mean(DailyMeanStreamflow, na.rm=TRUE))
plot4 <- ggplot(Winter_Streamflow_Day, aes(x=day, y=Winter_Data_Streamflow, group = Station, colour = Station)) + 
  geom_line(size = 1)  + 
  geom_point(size=1.5, shape=21, fill="white",show.legend = FALSE) + 
  labs(y ="Q [m3/s/day]", x = "day", title = "Winter Mean Daily Streamflow (December 1 to February 28)", size = 50) +
  theme(plot.title = element_text(size=16), axis.text.y = element_text(size=11), axis.text.x = element_text(angle = 90, size=11))+
  scale_x_continuous (breaks=seq(min(Winter_Data$day),max(Winter_Data$day),by=2))+
theme(legend.position="right")


grid.arrange(grobs=list(plot1, plot2, plot3, plot4), ncol = 1, nrow = 4,legend)

f <- arrangeGrob(plot1, plot2, plot3, plot4, ncol = 1, nrow = 4, top = "Mean Daily Streamflow by Season", legend)
ggsave(f,filename = "Mean_Daily_Streamflow_by_Season.png",width=22,height=11,units="in",dpi=500)
dev.off()

Doing this, I got the following image:

enter image description here

Any idea in how to improve the location of the legend in the graph? I have also tried in the bottom, and it not looks better!

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
Sss
  • 427
  • 2
  • 8
  • maybe try with two `arrangeGrob` calls: i.e. `arrangeGrob(arrangeGrob(plot1, plot2, plot3, plot4, ncol = 1, top = "Mean Daily Streamflow by Season"), legend, nrow=1, widths=c(5,1))` ; untested as no reproducible example // may need to tweak the widths. ps it seems like you could do this all a bit easier using facets by creating a season indicator – user20650 Sep 24 '20 at 23:23
  • .. in fact re the first suggestion ^^, you can probably get a bit more control using `layout_matrix` – user20650 Sep 24 '20 at 23:44
  • Can you make a reproducible problem https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example? Also check out `hydroTSM` and `openair` packages for these types of plot – Tung Sep 25 '20 at 01:04
  • Could you please add a sample of your data using `dput()`? There are other ways to solve your issue! – Duck Sep 25 '20 at 01:24

0 Answers0