Change legend header in ggplot2 for continuous data

Hello, I have the following map produced by ggplot2 and I would like to change the headers associated with the continuous variables in the legend:

I would like for the legend to say "Max Temp" and "Concentration (ug/L)", instead of the current headers based on the column names. I tried using scale_linetype_manual but it doesn't recognize my values. I tried scale_linetype_continuous but I got an error saying Error: A continuous variable can not be mapped to linetype. Does anyone know of a way to change the legend headers of continuous data in ggplot?

Here is my current plot code:

TOT_map <- ggplot() + 
  geom_sf(data=gb.shp) + 
  geom_sf(data=sf_fp, 
          aes(col=Mean_Total_Conc_ug.L, 
              size=Max_Temp_C), 
          na.rm=F) +
  theme(axis.text.x = element_blank(),  # remove x-axis text
        axis.text.y = element_blank(), # remove y-axis text
        axis.ticks = element_blank(),  # remove axis ticks
        axis.title.x = element_blank(), # remove x-axis labels
        axis.title.y = element_blank(), # remove y-axis labels
        panel.background = element_blank(), 
        panel.grid.major = element_blank(),  #remove major-grid labels
        panel.grid.minor = element_blank(),  #remove minor-grid labels
        plot.background = element_blank()) +
  labs(title="Mean Total Algal Concentrations (ug/L)") +
  scale_linetype_continuous(limits = c("Max_Temp_C", "Mean_Total_Conc_ug.L"),
                            labels = c("Max Temp", "Concentration (ug/L)"))

Thank you so much!!

The default is for the legend names to refer to the variable or formula which is mapped to that aesthetic. You can change this either using the lab() function or the name parameter inside the scale_* function which controls that aesthetic.

scale_linetype_* would typically be used to control whether a line (eg geom_line) is solid, dotted, dashed, etc., so I suspect it's not relevant here.

You should be able to use either:

labs(color = "Concentration (ug/L)", size = "Max Temp")

or

scales_colour_continuous(name = "Concentration (ug/L)") +
scale_size_continuous(name = "Max Temp")
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.