Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 当面和几何图形线分别绘制时,合并并编辑多个图例_R_Ggplot2 - Fatal编程技术网

R 当面和几何图形线分别绘制时,合并并编辑多个图例

R 当面和几何图形线分别绘制时,合并并编辑多个图例,r,ggplot2,R,Ggplot2,根据指南,我使用以下代码绘制了下图。我确实将我的数据集拆分为一个包含所有绘图中的数据的数据集“control”,其余的数据集“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(linet

根据指南,我使用以下代码绘制了下图。我确实将我的数据集拆分为一个包含所有绘图中的数据的数据集“control”,其余的数据集“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\u fill\u 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")

由于您没有提供任何数据,我将使用
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))
其结果如下图所示

编辑

根据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"))
给出了这个图:

数据

1)


dfcontrol ggplot()+geom线(数据=dfnocontrol,aes(y=value,x=year,color=survivorship),size=1.5)+刻面包裹(~density,nrow=2)+geom线(数据=dfcontrol,aes(linetype=simulname),color=grey,size=1.5)。为什么要使用刻度填充手册?你试过缩放颜色手册吗?Legend position and visible u can custom in theme()@AntonCH我曾尝试使用“缩放颜色”手册,但我不需要更改曲线的颜色,我需要将控件移动到survivorship Legend下如果您使用空ggplot()和两条geom线生成代码,它将是一个Legend。ggplot()+geom线(数据=dfnocontrol,aes(y=value,x=year,color=survivorship),size=1.5)+刻面包裹(~density,nrow=2)+geom线(数据=dfcontrol,aes(linetype=simulname),color=grey‌​',大小=1。5@AntonCH,它不工作,我得到顺序错误(c(1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,:参数3不是矢量我将编辑我的答案。我尝试使用此模板,但我的图例中仍然没有控件,为什么?我已经添加了数据。您发布的数据中的
simulname
的哪个值是指绘图中的
control
?对不起,这是第一部分。现在我用另一种方法解决了这个问题,但效率不是很高,我只是复制了8次控件,并为每个块指定了一个密度值,它可以工作。这对我来说也可以,我不知道为什么它以前不工作。
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"))
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")
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")