分别绘制构面和geom_line时,合并和编辑多个图例

时间:2017-11-16 11:02:57

标签: r ggplot2

按照this指南,我使用以下代码绘制了以下图表enter image description here。我确实将我的数据集拆分为一个包含所有图表中的数据的控件,其余的'控制'以及其他' dfnocontrol'。

ggplot(dfnocontrol,aes(y=value,x=year)) + geom_line(data=dfnocontrol,
  aes(color=survivorship),size=1.5) + facet_wrap(~density,nrow=2) +
  geom_line(data=dfcontrol,aes(linetype=simulname),color='grey',size=1.5)

我尝试了很多方法只有一个图例,或者编辑现有的两个图例,但似乎没有任何效果。 scale_fill_manual()似乎被忽略了,即使我没有收到任何错误消息。我被迫使用线型来进行“控制”。出现在图例中。我怎样才能合并这两个传说?

编辑:这些是控制数据

structure(list(year = 1:2, psize = structure(c(6L, 6L), .Label = c("all plants", 
"all plants no-seedl", "seedlings", "SmallerT10", "SmallerT10 no-seedl", 
"LargerT10", "10-30", "30-50", "50+"), class = "factor"), value = c(392.884450281975, 
392.76842677951), simulname = structure(c(1L, 1L), .Label = c("control", 
"d02s70", "d02s80", "d02s90", "d05s70", "d05s80", "d05s90", "d07s70", 
"d07s80", "d07s90", "d1s70", "d1s80", "d1s90", "d2s70", "d2s80", 
"d2s90", "d3s70", "d3s80", "d3s90", "d4s70", "d4s80", "d4s90", 
"d5s70", "d5s80", "d5s90"), class = "factor"), survivorship = structure(c(1L, 
1L), .Label = c("control", "s70", "s80", "s90"), class = "factor")), .Names = c("year", 
"psize", "value", "simulname", "survivorship"), row.names = 2501:2502, class = "data.frame")

以及其余的数据

structure(list(year = c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
    1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
    1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
    1L, 2L, 1L, 2L, 1L, 2L), psize = structure(c(6L, 6L, 6L, 6L, 
    6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), .Label = c("all plants", 
    "all plants no-seedl", "seedlings", "SmallerT10", "SmallerT10 no-seedl", 
    "LargerT10", "10-30", "30-50", "50+"), class = "factor"), value = c(391.933827876557, 
    390.784233661738, 391.931768654094, 390.777949423224, 391.930831801103, 
    390.775125884957, 391.904131913644, 390.671681105517, 391.903377880798, 
    390.669377819171, 391.902842713777, 390.667498067697, 391.874743014214, 
    390.557893743236, 391.874006362415, 390.555639401299, 391.8735511448, 
    390.554149478021, 391.84367266143, 390.443618794749, 391.843064602404, 
    390.442149462261, 391.842594963982, 390.440725187945, 391.72267802326, 
    388.998242801555, 391.722309813432, 388.996838950063, 391.721745089041, 
    388.995715149179, 384.967818982887, 383.215849576989, 384.967407490871, 
    383.214728664341, 384.96689031843, 383.213390281481, 391.897592532656, 
    389.445606459513, 391.897234485415, 389.444632515097, 391.89681267375, 
    389.443358475326, 391.402389493961, 388.987279260992, 391.401979078947, 
    388.985920091544, 391.401583421483, 388.984891027315), simulname = structure(c(2L, 
    2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 
    10L, 11L, 11L, 12L, 12L, 13L, 13L, 14L, 14L, 15L, 15L, 16L, 16L, 
    17L, 17L, 18L, 18L, 19L, 19L, 20L, 20L, 21L, 21L, 22L, 22L, 23L, 
    23L, 24L, 24L, 25L, 25L), .Label = c("control", "d02s70", "d02s80", 
    "d02s90", "d05s70", "d05s80", "d05s90", "d07s70", "d07s80", "d07s90", 
    "d1s70", "d1s80", "d1s90", "d2s70", "d2s80", "d2s90", "d3s70", 
    "d3s80", "d3s90", "d4s70", "d4s80", "d4s90", "d5s70", "d5s80", 
    "d5s90"), class = "factor"), density = structure(c(2L, 2L, 2L, 
    2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 
    5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 
    7L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L), .Label = c("control", 
    "d02", "d05", "d07", "d1", "d2", "d3", "d4", "d5"), class = "factor"), 
        survivorship = structure(c(2L, 2L, 3L, 3L, 4L, 4L, 2L, 2L, 
        3L, 3L, 4L, 4L, 2L, 2L, 3L, 3L, 4L, 4L, 2L, 2L, 3L, 3L, 4L, 
        4L, 2L, 2L, 3L, 3L, 4L, 4L, 2L, 2L, 3L, 3L, 4L, 4L, 2L, 2L, 
        3L, 3L, 4L, 4L, 2L, 2L, 3L, 3L, 4L, 4L), .Label = c("control", 
        "s70", "s80", "s90"), class = "factor")), .Names = c("year", 
    "psize", "value", "simulname", "density", "survivorship"), row.names = c(6081L, 
    6082L, 9845L, 9846L, 14345L, 14346L, 17985L, 17986L, 21797L, 
    21798L, 26297L, 26298L, 30567L, 30568L, 34528L, 34529L, 38744L, 
    38745L, 43144L, 43145L, 47519L, 47520L, 51983L, 51984L, 56483L, 
    56484L, 60983L, 60984L, 65483L, 65484L, 69983L, 69984L, 74483L, 
    74484L, 78983L, 78984L, 83483L, 83484L, 87983L, 87984L, 92483L, 
    92484L, 96983L, 96984L, 101483L, 101484L, 105983L, 105984L), class = "data.frame")

1 个答案:

答案 0 :(得分:1)

由于您没有提供数据,我将使用economics数据集为您举例。

library(wesanderson) # for the colours
library(tidyverse)
data("economics")

此任务需要两个数据集。变量unemploy将作为我们的“控件”(第6列)。所有变量都将被缩放。

第一组数据:

economics_gathered <- economics[, 1:5] %>% # exclude unemploy 
modify_if(is.numeric, scale) %>% 
gather(key, value, -date)

第二组数据:

economics_control <- economics[, c(1, 6)] %>% 
dplyr::rename(control = unemploy) %>% 
gather(some_other_key, value, 2) %>% 
mutate(value = scale(value))

现在我们可以绘制:

ggplot() + 
geom_line(data = economics_control, aes(x = date, y = value, col = some_other_key)) +
geom_line(data = economics_gathered, aes(date, value, col = key)) +
scale_colour_manual(values = c("grey", wes_palette("GrandBudapest"))) +
facet_wrap(~key, scales = "free_y")

结果如下图所示。

enter image description here

修改

OP提供的数据如下代码

ggplot() +
geom_line(data = dfcontrol, aes(year, value, col = survivorship), size = 1.5) +
geom_line(data = dfnocontrol, aes(year, value, col = survivorship), size = 1.5) +
facet_wrap( ~ density, nrow = 2) +
scale_colour_manual(values = c("grey", "forestgreen", "red", "blue"))

给出了这个情节:

enter image description here

数据

<强> 1)

dfcontrol <- structure(list(year = 1:2, psize = structure(c(6L, 6L), .Label = c("all plants", 
"all plants no-seedl", "seedlings", "SmallerT10", "SmallerT10 no-seedl", 
"LargerT10", "10-30", "30-50", "50+"), class = "factor"), value = c(392.884450281975, 
392.76842677951), simulname = structure(c(1L, 1L), .Label = c("control", 
"d02s70", "d02s80", "d02s90", "d05s70", "d05s80", "d05s90", "d07s70", 
"d07s80", "d07s90", "d1s70", "d1s80", "d1s90", "d2s70", "d2s80", 
"d2s90", "d3s70", "d3s80", "d3s90", "d4s70", "d4s80", "d4s90", 
"d5s70", "d5s80", "d5s90"), class = "factor"), survivorship = structure(c(1L, 
1L), .Label = c("control", "s70", "s80", "s90"), class = "factor")), .Names = c("year", 
"psize", "value", "simulname", "survivorship"), row.names = 2501:2502, class = "data.frame")

<强> 2)

dfnocontrol <- structure(list(year = c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L), psize = structure(c(6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), .Label = c("all plants", 
"all plants no-seedl", "seedlings", "SmallerT10", "SmallerT10 no-seedl", 
"LargerT10", "10-30", "30-50", "50+"), class = "factor"), value = c(391.933827876557, 
390.784233661738, 391.931768654094, 390.777949423224, 391.930831801103, 
390.775125884957, 391.904131913644, 390.671681105517, 391.903377880798, 
390.669377819171, 391.902842713777, 390.667498067697, 391.874743014214, 
390.557893743236, 391.874006362415, 390.555639401299, 391.8735511448, 
390.554149478021, 391.84367266143, 390.443618794749, 391.843064602404, 
390.442149462261, 391.842594963982, 390.440725187945, 391.72267802326, 
388.998242801555, 391.722309813432, 388.996838950063, 391.721745089041, 
388.995715149179, 384.967818982887, 383.215849576989, 384.967407490871, 
383.214728664341, 384.96689031843, 383.213390281481, 391.897592532656, 
389.445606459513, 391.897234485415, 389.444632515097, 391.89681267375, 
389.443358475326, 391.402389493961, 388.987279260992, 391.401979078947, 
388.985920091544, 391.401583421483, 388.984891027315), simulname = structure(c(2L, 
2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 
10L, 11L, 11L, 12L, 12L, 13L, 13L, 14L, 14L, 15L, 15L, 16L, 16L, 
17L, 17L, 18L, 18L, 19L, 19L, 20L, 20L, 21L, 21L, 22L, 22L, 23L, 
23L, 24L, 24L, 25L, 25L), .Label = c("control", "d02s70", "d02s80", 
"d02s90", "d05s70", "d05s80", "d05s90", "d07s70", "d07s80", "d07s90", 
"d1s70", "d1s80", "d1s90", "d2s70", "d2s80", "d2s90", "d3s70", 
"d3s80", "d3s90", "d4s70", "d4s80", "d4s90", "d5s70", "d5s80", 
"d5s90"), class = "factor"), density = structure(c(2L, 2L, 2L, 
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 
5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 
7L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L), .Label = c("control", 
"d02", "d05", "d07", "d1", "d2", "d3", "d4", "d5"), class = "factor"), 
    survivorship = structure(c(2L, 2L, 3L, 3L, 4L, 4L, 2L, 2L, 
    3L, 3L, 4L, 4L, 2L, 2L, 3L, 3L, 4L, 4L, 2L, 2L, 3L, 3L, 4L, 
    4L, 2L, 2L, 3L, 3L, 4L, 4L, 2L, 2L, 3L, 3L, 4L, 4L, 2L, 2L, 
    3L, 3L, 4L, 4L, 2L, 2L, 3L, 3L, 4L, 4L), .Label = c("control", 
    "s70", "s80", "s90"), class = "factor")), .Names = c("year", 
"psize", "value", "simulname", "density", "survivorship"), row.names = c(6081L, 
6082L, 9845L, 9846L, 14345L, 14346L, 17985L, 17986L, 21797L, 
21798L, 26297L, 26298L, 30567L, 30568L, 34528L, 34529L, 38744L, 
38745L, 43144L, 43145L, 47519L, 47520L, 51983L, 51984L, 56483L, 
56484L, 60983L, 60984L, 65483L, 65484L, 69983L, 69984L, 74483L, 
74484L, 78983L, 78984L, 83483L, 83484L, 87983L, 87984L, 92483L, 
92484L, 96983L, 96984L, 101483L, 101484L, 105983L, 105984L), class = "data.frame")