Home > OS >  Creating legend and defining colours for facetwrap with multiple geoms
Creating legend and defining colours for facetwrap with multiple geoms

Time:02-16

I'm creating a faceted plot of each sampling point, and trying to show four variables - a bar (species abundance at each sampling point), a line for water depth at the sampling point, another line for river height (measured elsewhere), and symbols for treatment intervention.

My problem is I can't seem to get the legend to show all the variables. I've included colour in the aesthetics for the geom_point, so that shows up, but I can't get the geom_lines into the legend. How can I get all four geoms represented in the legend?

Also, I'd like a third y-axis, as currently the grey line (river height) isn't represented on the y-axis at all. I've researched this and don't think there is away of adding a third y-axis, but if I'm missing something would be grateful for advice on that too.

This is the code (now updated based on comments)

ggplot()  
  geom_bar(data = dat2_site, aes(y=count_l_site, x=date, fill=f_grp), position="stack",stat="identity")  
  geom_line(data = dat2_site, aes(y=2*water_depth, x=date))  
  geom_point(data = subset(dat2_site, control_previous_week=='yes'), aes(y=-5, x=date), size = 3, shape = 17, colour = 'black', )  
  geom_line(data = dat_twyford, aes(x = date, y=50*value), linetype = 1)  
  facet_wrap(~site,ncol=3)  
  theme_bw()  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(), strip.background = element_blank(), axis.line = element_line(colour = "black"), panel.border = element_rect(colour = 'black', fill = NA))  
  scale_y_continuous(expand = c(0,0), limits=c(-5, 100), sec.axis = sec_axis(~./2, name = 'Water depth (cm)'))  
  scale_x_date(date_breaks = "1 month", date_labels =  "%b")  
  theme(axis.text=element_text(size=14),axis.title=element_text(size=12),legend.text = element_text(size=8)) 
  labs(x="Date",y="Number of larvae per litre", colour = 'Control') 
  scale_fill_manual(values = cols_f_grps) 
  theme(legend.position = 'bottom') 
  theme(legend.text.align = 0)

enter image description here

CodePudding user response:

Legend function works by taking from aesthetics.

CodePudding user response:

You shouldn't use the data argument in the ggplot2 function if you plan on utilizing different data frames to populate your plot. Instead move the data argument to each geom that it relies on, and it should work. I can't provide an example of the output as your data is not provided and your code is therefore not reproducible, but it would look something like this:

ggplot(aes(x=date))  
  geom_bar(data = dat2_site, aes(y=count_l_site, fill=f_grp), position="stack",stat="identity") 
  geom_line(data = dat2_site, aes(y=2*water_depth), colour = 'blue')   
  geom_point(data = subset(dat2_site, control_previous_week=='yes'), aes(y=-5, shape = control_previous_week, colour = 'yes'), size = 3, shape = 17, colour = 'black')  
  geom_line(data = dat_twyford, aes(x = date, y=50*value, colour = value), linetype = 1)

Note that the last line you had the colour argument used twice, and that within the aes() function you have to name the variable being mapped without quotes.

  • Related