0

I'm having trouble making a combined legend to show all four options for a plot that combines linetype and color scales. I have looked at this answer but, as you will discover upon running my plotting code, this does not seem to work - I get the classification correct but cannot make all four elements of the legend print. What am I doing wrong?

Thanks!

structure(list(Ocean.Regions = c("South Pacific", "South Pacific", 
"South Pacific", "South Pacific", "South Pacific", "South Pacific", 
"South Pacific", "South Pacific", "South Pacific", "South Pacific", 
"South Pacific", "South Pacific", "South Pacific", "South Pacific", 
"South Pacific", "South Pacific", "South Pacific", "South Pacific", 
"South Pacific", "South Pacific", "South Pacific", "South Pacific", 
"South Pacific", "South Pacific", "South Pacific", "South Pacific", 
"South Pacific", "South Pacific", "South Pacific", "South Pacific", 
"South Pacific", "South Pacific", "South Pacific", "South Pacific", 
"South Pacific", "South Pacific", "South Pacific", "South Pacific", 
"South Pacific", "South Pacific", "South Pacific", "South Pacific", 
"South Pacific", "South Pacific"), binned_alt = c(0, 0, 1000, 
1000, 2000, 2000, 3000, 3000, 4000, 4000, 5000, 5000, 6000, 6000, 
7000, 7000, 8000, 8000, 9000, 9000, 10000, 10000, 11000, 11000, 
12000, 0, 0, 1000, 1000, 2000, 3000, 4000, 4000, 5000, 7000, 
8000, 8000, 9000, 9000, 10000, 10000, 11000, 11000, 12000), Meteorological_Season_Factor = structure(c(1L, 
3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 
1L, 3L, 1L, 3L, 1L, 1L, 3L, 3L, 1L, 3L, 3L, 1L, 3L, 1L, 3L, 1L, 
1L, 1L, 1L, 3L, 3L, 1L, 3L, 1L, 1L, 3L, 3L), .Label = c("Winter", 
"Fall", "Summer", "Spring"), class = "factor"), Species = c("MEK", 
"MEK", "MEK", "MEK", "MEK", "MEK", "MEK", "MEK", "MEK", "MEK", 
"MEK", "MEK", "MEK", "MEK", "MEK", "MEK", "MEK", "MEK", "MEK", 
"MEK", "MEK", "MEK", "MEK", "MEK", "MEK", "MEK", "MEK", "MEK", 
"MEK", "MEK", "MEK", "MEK", "MEK", "MEK", "MEK", "MEK", "MEK", 
"MEK", "MEK", "MEK", "MEK", "MEK", "MEK", "MEK"), `25%` = c(10.25, 
23.95, 6.775, 12.45, 4.65, 5.675, 3.3, 5.5, 4.5, 5.275, 4.6, 
0.348456848923605, 4.35, 0.524861878453039, 3.475, 0.289655172413793, 
0.376811594202899, 0.289655172413793, 0.688259109311741, 0.524861878453039, 
4.15, 0.524861878453039, 0.524861878453039, 0.688259109311741, 
0.688259109311741, 13.4, 22.7, 12.45, 5.92241379310345, 5.4, 
1.84364640883978, 5.2, 2.61724137931034, 5.9, 0.289655172413793, 
0.289655172413793, 0.376811594202899, 11.1, 0.524861878453039, 
10.1, 0.524861878453039, 0.524861878453039, 0.688259109311741, 
0.688259109311741), `50%` = c(13.4, 28.45, 10.7, 17, 8.4, 9.85, 
5.9, 7.25, 5.9, 5.65, 7.15, 1.01243093922652, 6, 0.524861878453039, 
4.75, 0.524861878453039, 2.8, 0.289655172413793, 3.5, 0.524861878453039, 
7.5, 0.524861878453039, 0.524861878453039, 0.688259109311741, 
0.688259109311741, 14.2, 28.4, 17, 10.75, 6.5, 3.16243093922652, 
5.2, 4.9448275862069, 5.9, 0.289655172413793, 0.289655172413793, 
0.376811594202899, 11.1, 0.524861878453039, 10.3, 0.524861878453039, 
0.524861878453039, 0.688259109311741, 0.688259109311741), `75%` = c(16, 
33.9, 17.85, 19.1, 10.15, 16.2, 12.325, 10.3, 9.05, 7.175, 7.425, 
3.775, 7.6, 0.524861878453039, 5.875, 0.524861878453039, 5, 0.524861878453039, 
9, 0.524861878453039, 9.675, 1.31864640883978, 0.524861878453039, 
1.69412955465587, 0.688259109311741, 14.5, 34.2, 19.1, 16.15, 
13.2, 4.48121546961326, 5.2, 7.27241379310345, 5.9, 0.289655172413793, 
0.407258525433416, 0.376811594202899, 11.1, 0.524861878453039, 
10.5, 2.11243093922652, 0.524861878453039, 0.688259109311741, 
0.688259109311741), Treatment = structure(c(2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Clean Marine", "All Data"
), class = "factor")), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -44L))

And here's the plot code

library(ggplot2)
library(tidyverse)
library(ggthemes)

SOtestplot <- test %>%
  filter(Species == "MEK", binned_alt <= 8000) %>%
  ggplot(aes(y = binned_alt/1000, x = `50%`, col = Meteorological_Season_Factor, linetype = Treatment)) +
  geom_path(size = 1.2) + 
  theme_tufte(base_size = 22) +
  geom_errorbarh(aes(xmin =`25%`, xmax = `75%`), height = 0) + 
  theme(axis.title.x = element_text(vjust=-0.5),
        axis.title.y = element_text(vjust=1.5),
        panel.grid.major = element_line(colour = "grey80"),
        axis.line = element_line(size = 0.5, colour = "black")) +
  scale_color_manual(name = "Treatment & Season",
                     values = c("cornflowerblue", "goldenrod3", "cornflowerblue", "goldenrod3"),
                     labels = c("Summer Clean Marine",
                                "Winter Clean Marine",
                                "Summer All Data",
                                "Winter All Data")) +
  scale_linetype_manual(name = "Treatment & Season",
                        values = c( "solid", "dashed", "solid", "dashed"),
                        labels = c("Summer Clean Marine",
                                   "Winter Clean Marine",
                                   "Summer All Data",
                                   "Winter All Data")) +
  xlab("MEK (ppt)") +
  ylab("Altitude (km)")

1 Answer 1

1

Try this. Make a hash of the two fields you're using (mutate(hybrid = paste(Meteorological_Season_Factor, Treatment))), and then use that for both color and line type (aes(...col = hybrid, linetype = hybrid)), and then use named vectors for your values in both scale functions.

Is that what you're looking for? (P.S. I believe I used the labels and values you used in the order in which you used them... maybe that was the only problem is that you'd transposed a couple of them!)

SOtestplot <- test %>%
  mutate(hybrid = paste(Meteorological_Season_Factor, Treatment)) %>% 
  filter(Species == "MEK", binned_alt <= 8000) %>%
  ggplot(aes(y = binned_alt/1000, x = `50%`, col = hybrid, linetype = hybrid)) +
  geom_path(size = 1.2) + 
  theme_tufte(base_size = 22) +
  geom_errorbarh(aes(xmin =`25%`, xmax = `75%`), height = 0) + 
  theme(axis.title.x = element_text(vjust=-0.5),
        axis.title.y = element_text(vjust=1.5),
        panel.grid.major = element_line(colour = "grey80"),
        axis.line = element_line(size = 0.5, colour = "black")) +
  scale_color_manual(name = "Treatment & Season",
                     values = c(
                       `Summer Clean Marine` = "cornflowerblue",
                       `Winter Clean Marine` = "goldenrod3",
                       `Summer All Data` = "cornflowerblue",
                       `Winter All Data` = "goldenrod3"
                     )) +
  scale_linetype_manual(name = "Treatment & Season",
                        values = c(
                          `Summer Clean Marine` = "solid",
                          `Winter Clean Marine` = "dashed",
                          `Summer All Data` = "solid",
                          `Winter All Data` = "dashed")
                        ) +
  xlab("MEK (ppt)") +
  ylab("Altitude (km)")
4
  • Similarly, could use unite("hybrid", c("Meteorological_Season_Factor", "Treatment"), sep = " ") %>%
    – Jon Spring
    Commented May 17, 2019 at 22:09
  • 1
    This is a very elegant solution. Thank you! I'm still not sure why what I did wasn't working but I'm fairly sure you are right and I somehow messed up the order of the lbaels. Regardless, this is better in this case, because there's no doubt. Thank you. Commented May 17, 2019 at 22:17
  • You bet! So glad I could help.
    – Benjamin
    Commented May 17, 2019 at 22:21
  • And good luck with your analysis! It looked really interesting.
    – Benjamin
    Commented May 18, 2019 at 22:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.