0
votes

I cannot figure out how to make two separate legends for my geom_point and my geom_line data. Currently, I have a legend describing what the different linetypes mean, but when I try to add a legend to describe what the different point shapes mean (blue triangles and circles), blue circles simply overlap the linetype legend. Many thanks to anyone who can help me make two separate legends for linetype and point shape. Below is my code, my data, and a picture of my current graph:

Here's my data, called "Data":

           site_name                    Watershed Fish_IBI_Tool2_percent     Fish_IBI_Tool7_percent Exceptional_2_percent Poor_2_percent
1           Piersons Six Mile Creek & Schutz Lake              12.888889                     NA              42.22222              0
2         Wassermann Six Mile Creek & Schutz Lake             -17.111111                     NA              42.22222              0
3             Church Six Mile Creek & Schutz Lake                     NA                     NA                    NA             NA
4            Steiger Six Mile Creek & Schutz Lake             -35.777778                     NA              42.22222              0
5             Zumbra Six Mile Creek & Schutz Lake               2.666667                     NA              42.22222              0
6              Stone Six Mile Creek & Schutz Lake                     NA                     NA                    NA             NA
7        East Auburn Six Mile Creek & Schutz Lake             -35.333333                     NA              42.22222              0
8        West Auburn Six Mile Creek & Schutz Lake             -35.333333                     NA              42.22222              0
9             Turbid Six Mile Creek & Schutz Lake                     NA                     NA                    NA             NA
10            Schutz Six Mile Creek & Schutz Lake             -49.333333                     NA              42.22222              0
11           Brownie              Minnehaha Creek                     NA                     NA                    NA             NA
12           Calhoun              Minnehaha Creek              -6.888889                     NA              42.22222              0
13             Cedar              Minnehaha Creek               6.666667                     NA              42.22222              0
14           Harriet              Minnehaha Creek              -6.222222                     NA              42.22222              0
15          Hiawatha              Minnehaha Creek                     NA                     NA                    NA             NA
16 Lake of the Isles              Minnehaha Creek                     NA                7.50000                    NA             NA
17           Nokomis              Minnehaha Creek              -4.888889                     NA              42.22222              0
18                                Minnehaha Creek                     NA                     NA                    NA             NA
19              Taft              Minnehaha Creek                     NA                     NA                    NA             NA
20               Mud Six Mile Creek & Schutz Lake                     NA               13.61111                    NA             NA
21            Parley Six Mile Creek & Schutz Lake                     NA               45.83333                    NA             NA
   Degraded_2_percent Exceptional_4_percent Poor_4_percent Degraded_4_percent Poor_7_percent Degraded_7_percent
1           -51.11111                    NA             NA                 NA             NA                 NA
2           -51.11111                    NA             NA                 NA             NA                 NA
3                  NA              55.26316              0          -76.31579             NA                 NA
4           -51.11111                    NA             NA                 NA             NA                 NA
5           -51.11111                    NA             NA                 NA             NA                 NA
6                  NA              55.26316              0          -76.31579             NA                 NA
7           -51.11111                    NA             NA                 NA             NA                 NA
8           -51.11111                    NA             NA                 NA             NA                 NA
9                  NA              55.26316              0          -76.31579             NA                 NA
10          -51.11111                    NA             NA                 NA             NA                 NA
11                 NA              55.26316              0          -76.31579             NA                 NA
12          -51.11111                    NA             NA                 NA             NA                 NA
13          -51.11111                    NA             NA                 NA             NA                 NA
14          -51.11111                    NA             NA                 NA             NA                 NA
15                 NA              55.26316              0          -76.31579             NA                 NA
16                 NA                    NA             NA                 NA              0          -52.77778
17          -51.11111                    NA             NA                 NA             NA                 NA
18                 NA                    NA             NA                 NA              0          -52.77778
19                 NA              55.26316              0          -76.31579             NA                 NA
20                 NA                    NA             NA                 NA              0          -52.77778
21                 NA                    NA             NA                 NA              0          -52.77778

Here's a dput of my Data:

structure(list(site_name = structure(c(10L, 13L, 18L, 14L, 12L, 
19L, 16L, 17L, 20L, 15L, 5L, 4L, 1L, 3L, 6L, 8L, 2L, 9L, 7L, 
22L, 21L), .Label = c("Cedar", "Nokomis", "Harriet", "Calhoun", 
"Brownie", "Hiawatha", "Taft", "Lake of the Isles", " ", "Piersons", 
"Kelser's Pond", "Zumbra", "Wassermann", "Steiger", "Schutz", 
"East Auburn", "West Auburn", "Church", "Stone", "Turbid", "Parley", 
"Mud"), class = "factor"), Watershed = c("Six Mile Creek & Schutz Lake", 
"Six Mile Creek & Schutz Lake", "Six Mile Creek & Schutz Lake", 
"Six Mile Creek & Schutz Lake", "Six Mile Creek & Schutz Lake", 
"Six Mile Creek & Schutz Lake", "Six Mile Creek & Schutz Lake", 
"Six Mile Creek & Schutz Lake", "Six Mile Creek & Schutz Lake", 
"Six Mile Creek & Schutz Lake", "Minnehaha Creek", "Minnehaha Creek", 
"Minnehaha Creek", "Minnehaha Creek", "Minnehaha Creek", "Minnehaha Creek", 
"Minnehaha Creek", "Minnehaha Creek", "Minnehaha Creek", "Six Mile Creek & Schutz Lake", 
"Six Mile Creek & Schutz Lake"), Fish_IBI_Tool2_percent = c(12.88888889, 
-17.11111111, NA, -35.77777778, 2.666666667, NA, -35.33333333, 
-35.33333333, NA, -49.33333333, NA, -6.888888889, 6.666666667, 
-6.222222222, NA, NA, -4.888888889, NA, NA, NA, NA), Fish_IBI_Tool7_percent = c(NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 7.5, 
NA, NA, NA, 13.61111111, 45.83333333), Exceptional_2_percent = c(42.22222222, 
42.22222222, NA, 42.22222222, 42.22222222, NA, 42.22222222, 42.22222222, 
NA, 42.22222222, NA, 42.22222222, 42.22222222, 42.22222222, NA, 
NA, 42.22222222, NA, NA, NA, NA), Poor_2_percent = c(0L, 0L, 
NA, 0L, 0L, NA, 0L, 0L, NA, 0L, NA, 0L, 0L, 0L, NA, NA, 0L, NA, 
NA, NA, NA), Degraded_2_percent = c(-51.11111111, -51.11111111, 
NA, -51.11111111, -51.11111111, NA, -51.11111111, -51.11111111, 
NA, -51.11111111, NA, -51.11111111, -51.11111111, -51.11111111, 
NA, NA, -51.11111111, NA, NA, NA, NA), Exceptional_4_percent = c(NA, 
NA, 55.26315789, NA, NA, 55.26315789, NA, NA, 55.26315789, NA, 
55.26315789, NA, NA, NA, 55.26315789, NA, NA, NA, 55.26315789, 
NA, NA), Poor_4_percent = c(NA, NA, 0L, NA, NA, 0L, NA, NA, 0L, 
NA, 0L, NA, NA, NA, 0L, NA, NA, NA, 0L, NA, NA), Degraded_4_percent = c(NA, 
NA, -76.31578947, NA, NA, -76.31578947, NA, NA, -76.31578947, 
NA, -76.31578947, NA, NA, NA, -76.31578947, NA, NA, NA, -76.31578947, 
NA, NA), Poor_7_percent = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, 0L, NA, 0L, NA, 0L, 0L), Degraded_7_percent = c(NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, -52.77777778, 
NA, -52.77777778, NA, -52.77777778, -52.77777778)), .Names = c("site_name", 
"Watershed", "Fish_IBI_Tool2_percent", "Fish_IBI_Tool7_percent", 
"Exceptional_2_percent", "Poor_2_percent", "Degraded_2_percent", 
"Exceptional_4_percent", "Poor_4_percent", "Degraded_4_percent", 
"Poor_7_percent", "Degraded_7_percent"), class = "data.frame", row.names = c(NA, 
-21L))

Here's my code:

#Import, fix up, and subset Data data.frame
Data = read.csv("Lakes_data_for_R2.csv", 
            stringsAsFactors=FALSE)
colnames(Data)[1] <- "site_name"

#Order site_name
 Data$site_name <- factor(Data$site_name, levels = c("Cedar",
                                                "Nokomis",
                                                "Harriet",
                                                "Calhoun",
                                                "Brownie",
                                                "Hiawatha",
                                                "Taft",
                                                "Lake of the Isles",
                                                " ",
                                                "Piersons",
                                                "Kelser's Pond",
                                                "Zumbra",
                                                "Wassermann",
                                                "Steiger",
                                                "Schutz",
                                                "East Auburn",
                                                "West Auburn",
                                                "Church",
                                                "Stone",
                                                "Turbid",
                                                "Parley",
                                                "Mud"))

#Load ggplot
library(ggplot2)

#Make plot
ggplot() +
  geom_point(data = Data, aes(x = site_name, y = Fish_IBI_Tool2, shape = "tool2"), size = 5, color = "blue", show_guide = TRUE) +
  geom_point(data = Data, aes(x = site_name, y = Fish_IBI_Tool7, shape = "tool7"), size = 5, color = "blue", show_guide = TRUE) +

  geom_line(data = Data, aes(x = site_name, y = Exceptional_2_percent, group = 1, linetype = "Exceptional", color = "Exceptional"), size = 1) +
  geom_line(data = Data, aes(x = site_name, y = Poor_2_percent, group = 1, linetype = "Poor", color = "Poor"), size = 1) +
  geom_line(data = Data, aes(x = site_name, y = Degraded_2_percent, group = 1, linetype = "Degraded", color = "Degraded"), size = 1) +
  geom_line(data = Data, aes(x = site_name, y = Exceptional_4_percent, group = 1, linetype = "Exceptional", color = "Exceptional"), size = 1) +
  geom_line(data = Data, aes(x = site_name, y = Poor_4_percent, group = 1, linetype = "Poor", color = "Poor"), size = 1) +
  geom_line(data = Data, aes(x = site_name, y = Degraded_4_percent, group = 1, linetype = "Degraded", color = "Degraded"), size = 1) +
  geom_line(data = Data, aes(x = site_name, y = Poor_7_percent, group = 1, linetype = "Poor", color = "Poor"), size = 1) +
  geom_line(data = Data, aes(x = site_name, y = Degraded_7_percent, group = 1, linetype = "Degraded", color = "Degraded"), size = 1) +

  scale_linetype_manual(values = c("dashed", "twodash", "solid"), breaks = c("Degraded", "Poor", "Exceptional"), name = "Legend") +
  scale_color_manual(values = c("red", "springgreen4", "red"), breaks = c("Degraded", "Poor", "Exceptional"), name = "Legend") +
  scale_shape_manual(values = c(17, 16), breaks = c("Tool 2", "Tool 7")) +

  ylab ("Fish IBI (% difference from Poor threshold)") + xlab("") +
  facet_grid(~Watershed, scale = "free", space = "free") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1, color = "black"),
    axis.text.y = element_text(color = "black"),
    panel.grid.major.x = element_blank(),
    panel.grid.major.y = element_blank(),
    panel.grid.minor = element_blank(), 
    axis.line.x = element_line(color = "black"),
    axis.line.y = element_line(color = "black"),
    panel.background = element_rect(fill = "white"),
    legend.key=element_blank(),
    legend.title = element_blank(),
    text = element_text(size=18),
    panel.spacing = unit(2, "lines"))

And here's what my graph currently looks like:

enter image description here

1
could you post a dput of your data? Also show_guide is deprecated. What version of ggplot are you using?Vandenman
@Vandenman thanks for the speedy reply! Just posted dput(Data) output. Let me know if this helps.snalven
@Vandenman oops-- I didn't realize there was a difference between show.legend and show_guide. They seem to do the same thing here though... I will use show.legend from now on.snalven
If you want to change the labels in the legend for your shape legend, you need to use labels in scale_shape_manual, not breaks. Or create the desired labels using, e.g., shape = "Tool 2" instead of shape = "tool2".aosmith
That's due to show_guide = TRUE. Take that out of your geom_point layers.aosmith

1 Answers

1
votes

As has been stated, the issue here is the use of show_guide. However, it's also perhaps fair to say that ggplot2 often works better if the data is reformatted to be long rather than wide, such that the legends then fall more naturally out of the data:

library(tidyr)
data_long <- gather(Data, variable, value, -c(site_name, Watershed)) %>%
  separate(variable, paste0("var", 1:4), fill = "right")


ggplot(mapping = aes(x = site_name, y = value)) +
  geom_point(aes(shape = var3), data = data_long %>% filter(var1 == "Fish"),
    colour = "blue", size = 5) +
  geom_line(aes(linetype = var1, colour = var1,
    group = interaction(var1, var2)),
    data = data_long %>% filter(var1 != "Fish"), size = 1) +
  scale_linetype_manual("", values = c(Degraded = "dashed", Exceptional = "twodash",
    Poor = "solid")) +
  scale_color_manual("", values = c(Degraded = "red", Exceptional = "springgreen4",
    Poor = "red")) +
  scale_shape_manual("", values = c(Tool2 = 17, Tool7 = 16)) +

  ylab("Fish IBI (% difference from Poor threshold)") +
  xlab("") +
  facet_grid(~Watershed, scale = "free", space = "free") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1, color = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), 
        panel.background = element_rect(fill = "white"),
        axis.line.x = element_line(color = "black"),
        axis.line.y = element_line(color = "black"),
        legend.key = element_blank(),
        text = element_text(size=18),
        panel.spacing = unit(2, "lines"))

Sample plot