Merge and edit multiple legends when facets and ge

2019-08-21 07:58发布

问题:

Following this guide I have plotted the following graph using the following code. I did split my dataset into one that contains the data that goes in all plots 'control', and the rest '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)

I have tried many ways to have only one legend, or to edit the existing two legend but nothing seems to work. scale_fill_manual() seems to be ignored, even though I don't get any error message. I was forced to use linetype to make the 'control' appear in the legend. How can I merge these two legends?

edit: these are the data for control

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

and data for the rest

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:

Since you provided no data, I will give you an example using the economics data set.

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

We will need two data sets for this task. Variable unemploy will serve as our 'control' (6th column). All variables will be scaled.

First data set:

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

Second data set:

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

Now we can plot:

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

To which the result is the plot below.

EDIT

With the data provided by the OP the following code

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

gives this plot:

DATA

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


标签: r ggplot2