Modify legend and line type for hline added to faceted grid

I created a faceted grid. I added horizontal lines from a related data frame to each facet. The added line in each facet has a different value. I have nearly everything I want for the plot, but the legend for the added horizontal lines. I want the lines to all have same formatting, dashed or dotted, I don't care. I want a legend for the lines with just the name for the lines: "Mean Value, 2012-2016". I have tried changing the aes() and linetype inside the geom_hline() and I've also tried using scale_linetype_manual( name = ...) and other arguments. I can't create the legend for the lines at all. When I add text to the linetype inside the geom_hline(), it doesn't let me change the line to dashed or anything else.

Here are the data for the facet:

dput(tmx2)
structure(list(Year = c(2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
2017L, 2017L, 2017L, 2017L, 2017L, 2018L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2019L, 2019L, 
2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 
2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 
2020L, 2020L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 
2021L, 2021L, 2021L, 2021L), Category = c("Max", "Max", "Max", 
"Max", "Max", "Max", "Max", "Max", "Max", "Max", "Max", "Max", 
"Max", "Max", "Max", "Max", "Max", "Max", "Max", "Max", "Max", 
"Max", "Max", "Max", "Max", "Max", "Max", "Max", "Max", "Max", 
"Max", "Max", "Max", "Max", "Max", "Max", "Max", "Max", "Max", 
"Max", "Max", "Max", "Max", "Max", "Max", "Max", "Max", "Max", 
"Max", "Max", "Max", "Max", "Max", "Max", "Max"), County = c("Camden_NC", 
"Dorchester_MD", "Franklin_PA", "Guilford_NC", "Halifax_VA", 
"Lancaster_PA", "Northampton_VA", "Pasquotank_NC", "Rutherford_NC", 
"Suffolk_NY", "Wake_NC", "Camden_NC", "Dorchester_MD", "Franklin_PA", 
"Guilford_NC", "Halifax_VA", "Lancaster_PA", "Northampton_VA", 
"Pasquotank_NC", "Rutherford_NC", "Suffolk_NY", "Wake_NC", "Camden_NC", 
"Dorchester_MD", "Franklin_PA", "Guilford_NC", "Halifax_VA", 
"Lancaster_PA", "Northampton_VA", "Pasquotank_NC", "Rutherford_NC", 
"Suffolk_NY", "Wake_NC", "Camden_NC", "Dorchester_MD", "Franklin_PA", 
"Guilford_NC", "Halifax_VA", "Lancaster_PA", "Northampton_VA", 
"Pasquotank_NC", "Rutherford_NC", "Suffolk_NY", "Wake_NC", "Camden_NC", 
"Dorchester_MD", "Franklin_PA", "Guilford_NC", "Halifax_VA", 
"Lancaster_PA", "Northampton_VA", "Pasquotank_NC", "Rutherford_NC", 
"Suffolk_NY", "Wake_NC"), mean_val = c(26.6, 24.7, 22.9, 26.1, 
26.2, 22.8, 24.6, 26.8, 26, 19.8, 26.7, 26.2, 24.4, 21.8, 26.8, 
26.3, 21.9, 24.5, 26.4, 26.5, 19.2, 27, 27.5, 25.5, 22.8, 26.8, 
26.8, 23, 25.6, 27.6, 26.8, 19.5, 27.3, 24.2, 22.9, 20.9, 24.2, 
24.2, 21.1, 22.6, 24.5, 24.6, 19.3, 25, 25.6, 24.1, 22.5, 25.6, 
25.7, 22.9, 24.1, 25.8, 25.7, 20.4, 26.3)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -55L), groups = structure(list(
    Year = 2017:2021, Category = c("Max", "Max", "Max", "Max", 
    "Max"), .rows = structure(list(1:11, 12:22, 23:33, 34:44, 
        45:55), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -5L), .drop = TRUE))

Here are the data for the geom_hline():

dput(onefivx)
structure(list(County = c("Camden_NC", "Dorchester_MD", "Franklin_PA", 
"Guilford_NC", "Halifax_VA", "Lancaster_PA", "Northampton_VA", 
"Pasquotank_NC", "Rutherford_NC", "Suffolk_NY", "Wake_NC"), mean_value = c(25.6, 
23.98, 22.32, 25.92, 25.78, 22.58, 23.68, 25.74, 26.32, 19.88, 
26.24)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-11L))

And here is code the produces everything but the legend:

gtxl2 <- ggplot(tmx2, aes(x=Year,y=mean_val, colour=factor(County))) + 
  labs(title = "Mean Daily Maximum Temperature During Growing Season (April - June)", y="Celsius", 
       colour="County") + ylim(0,28) + geom_point(size=2.5) +
  facet_grid(rows=vars(County)) +
  geom_hline(data=onefivx, aes(yintercept=mean_value), linetype=5) + #"Mean Seasonal Max., 2012-2016") +
  scale_linetype_manual(name='test' )

Is this what you're after?

ggplot(tmx, aes(
  x = Year,
  y = mean_val,
  colour = factor(County)
)) +
  labs(title = "Mean Daily Maximum Temperature During Growing Season (April - June)", y =
         "Celsius",
       colour = "County") + ylim(0, 28) + geom_point(size = 2.5) +
  facet_grid(rows = vars(County)) +
  geom_hline(data = onefivx,
             aes(yintercept = mean_value, lty = "Mean Value, 2012-2016")) + #"Mean Seasonal Max., 2012-2016") +
  scale_linetype_manual(name = NULL, values = 5)

Hi, Jack. Yes you answered my question. Thank you. I probably should have waited before asking because now I have a new wrinkle: more hlines added on and how to get their colors in the legend. Guess I'll mess around with that and maybe ask another question before.

Best,
John

I think {ggplot2} struggles with multiple different geoms coloured in using different columns.

As a hint - shape = 21 uses fill rather than colour, which might let you get both on there.

Best of luck!

2 Likes

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.