如何在ggplot2中分隔图例以使它们不重叠?

时间:2017-04-03 14:32:40

标签: r ggplot2 legend

我无法弄清楚如何为我的geom_point和我的geom_line数据创建两个单独的图例。目前,我有一个描述不同线型含义的图例,但是当我尝试添加图例来描述不同点形状的含义(蓝色三角形和圆形)时,蓝色圆圈只是重叠线型图例。非常感谢能够帮助我为线型和点形状制作两个独立传说的人。下面是我的代码,我的数据和我当前图表的图片:

这是我的数据,称为“数据”:

           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

这是我的数据的输入:

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))

这是我的代码:

#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"))

这就是我的图表目前的样子:

enter image description here

1 个答案:

答案 0 :(得分:1)

如前所述,这里的问题是使用show_guide。但是,如果数据被重新格式化为长而不是宽,那么ggplot2通常也可以更好地运行,这样传说就会更自然地落在数据之外:

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