简体   繁体   English

向R中的多个数据框的ggplot线图添加图例

[英]Adding a legend to ggplot linegraph of multiple dataframes in R

I want to add a legend to a ggplot linegraph of multiple dataframes in R. I read in several .csv files (see below) to dataframes, then plot them in R using the following code: 我想在R中的多个数据框的ggplot线图中添加图例。我将多个.csv文件(请参见下文)读入数据框,然后使用以下代码在R中绘制它们:

ggplot(data=ridge_u_s_summary,aes(x = times,y=means))+
  geom_errorbar(data=ridge_u_s_summary,aes(ymin=means-sd,ymax=means+sd),colour="red")+
  geom_line(aes(y=means),colour="red")+
  geom_line(data = valley_u_s_summary,aes(x=times,y=means))+
  theme_classic()+
  geom_errorbar(data=valley_u_s_summary,aes(ymin=means-sd,ymax=means+sd))+
  geom_line(data = edge_u_s_summary,aes(x=times,y=means),colour="blue")+
  geom_errorbar(data=edge_u_s_summary,aes(ymin=means-sd,ymax=means+sd),colour="blue")+
  labs(x="Time",y="Temperature in understory during January to April")

In order to add a legend to this, I have tried 为了添加一个图例,我尝试了

Habitat <- c("Edge")
ggplot(data=edge_u_summary,aes(x = times,y=means,color=Habitat))+
  geom_errorbar(data=edge_u_summary,aes(ymin=means-sd,ymax=means+sd),colour="red")+
  geom_line(aes(y=means),colour="red")+
  geom_line(data = ridge_u_summary,aes(x=times,y=means))+
  geom_errorbar(data=ridge_u_summary,aes(ymin=means-sd,ymax=means+sd))+
  geom_line(data = valley_u_summary,aes(x=times,y=means),colour="blue")+
  geom_errorbar(data=valley_u_summary,aes(ymin=means-sd,ymax=means+sd),colour="blue")+
  labs(x="Time",y="Temperature in understory all year")

This gets me almost what I want- except it changed the color of the plot for some reason, and when I attempt to add the other variables to the legend as R gives the error: 这几乎可以得到我想要的-除了由于某种原因改变了绘图的颜色,以及当我尝试将其他变量添加到图例时,因为R给出了错误:

"Error: Aesthetics must be either length 1 or the same as the data (25): x, y, colour"

The data looks something like this: 数据看起来像这样:

edge_u_summary: edge_u_summary:

"","times","means","sd"
"1",1,23.8256278538813,1.08250700768145
"12",2,23.1031294452347,1.24911533974507
"19",3,23.4439617486339,1.1282693292488
"20",4,22.7613086770982,1.36975338509067
"21",5,23.2025956284153,1.20295088366619
"22",6,22.5241109530583,1.44166092397246
"23",7,23.1135422343324,1.26558498566579
"24",8,23.2541963015647,1.47021441335091
"25",9,25.1409536784741,1.58548666490834
"2",10,26.7320085166785,2.59539671383782
"3",11,27.6513358778626,2.5977168687079
"4",12,28.586775106082,2.87211533419383
"5",13,28.5709923664122,2.79577350516269
"6",14,29.5764872521246,3.26115806616442
"7",15,28.4104832104832,2.70542254623265
"8",16,28.3307583274274,2.5157045864679
"9",17,26.9131787175989,1.8371272211906
"10",18,26.3352232459249,1.71211772244667
"11",19,25.2531787175989,1.12498732015416
"13",20,24.652728561304,1.24019473277585
"14",21,24.4688591703057,0.99064927009491
"15",22,23.9340425531915,1.16463318058244
"16",23,24.0581058951965,0.987431147782684
"17",24,23.4854609929078,1.19542300490208
"18",25,23.6280334728033,0.977115814231141

ridge_u_summary: ridge_u_summary:

"","times","means","sd"
"1",1,23.5870433511934,0.937068380991415
"12",2,23.0331950207469,1.1488061017937
"19",3,23.3496395963479,1.02861071580118
"20",4,22.682918395574,1.28095122645134
"21",5,23.1064616862839,1.11960341933558
"22",6,22.4135546334716,1.36699461456579
"23",7,22.9523592814371,1.19411131617966
"24",8,22.5736514522822,1.44525141078227
"25",9,24.0131017964072,1.20022890842346
"2",10,24.4016586040083,1.61518041063122
"3",11,25.6837805462386,1.49361866061128
"4",12,26.0852107809261,2.0335844646935
"5",13,26.5968368080518,1.80035938711879
"6",14,26.7841959972395,2.24353092250421
"7",15,26.6535363222249,1.7733853965812
"8",16,26.5494824016563,1.97883869059574
"9",17,25.9478177458034,1.47079061322174
"10",18,25.5847212663455,1.67334903054897
"11",19,24.9841852651788,1.13952974451756
"13",20,24.505099931082,1.34188808323866
"14",21,24.3428365730742,0.964465091164753
"15",22,23.8500345065562,1.1826679089188
"16",23,23.9638415366146,0.934203639374674
"17",24,23.4138716356108,1.13425784998453
"18",25,23.7044528659403,0.977776310460964

Valley_u_summary: Valley_u_summary:

"","times","means","sd"
"1",1,23.8609523809524,0.941137931036436
"12",2,23.6167464114833,1.36368015448422
"19",3,23.6931023210199,1.09423013467475
"20",4,23.2622807017544,1.40370217291909
"21",5,23.444655116051,1.13978244673363
"22",6,22.9831738437002,1.37794014701823
"23",7,23.2909416748126,1.17933203457316
"24",8,23.2392657621708,1.35015918020873
"25",9,24.302705345502,1.07391752061927
"2",10,24.8187699680511,1.39386829705572
"3",11,25.7716009129442,1.36677999460727
"4",12,26.1712689545092,1.97824543923682
"5",13,26.5006523157208,1.62284855146781
"6",14,26.6692430278884,2.08567545174535
"7",15,26.502446183953,1.55882287684552
"8",16,26.28759936407,1.72625533400646
"9",17,25.8817558746736,1.24691066538137
"10",18,25.5448412698413,1.45728060915926
"11",19,25.1434910277325,1.08377131492575
"13",20,24.8136616362192,1.32224607164181
"14",21,24.6306135770235,1.00789058764099
"15",22,24.3593328038125,1.32214170798361
"16",23,24.2933093994778,1.02334054495071
"17",24,23.9780778395552,1.34031144743681
"18",25,24.1241503976862,1.16464232030193

The data appear scrambled due to a sorting artifact, but are okay. 数据由于排序伪像而出现混乱,但是可以。

The following works 以下作品

edge_u_s_summary <- read.table(text = '"times","means","sd"
"1",1,23.8256278538813,1.08250700768145
"12",2,23.1031294452347,1.24911533974507
"19",3,23.4439617486339,1.1282693292488
"20",4,22.7613086770982,1.36975338509067
"21",5,23.2025956284153,1.20295088366619
"22",6,22.5241109530583,1.44166092397246
"23",7,23.1135422343324,1.26558498566579
"24",8,23.2541963015647,1.47021441335091
"25",9,25.1409536784741,1.58548666490834
"2",10,26.7320085166785,2.59539671383782
"3",11,27.6513358778626,2.5977168687079
"4",12,28.586775106082,2.87211533419383
"5",13,28.5709923664122,2.79577350516269
"6",14,29.5764872521246,3.26115806616442
"7",15,28.4104832104832,2.70542254623265
"8",16,28.3307583274274,2.5157045864679
"9",17,26.9131787175989,1.8371272211906
"10",18,26.3352232459249,1.71211772244667
"11",19,25.2531787175989,1.12498732015416
"13",20,24.652728561304,1.24019473277585
"14",21,24.4688591703057,0.99064927009491
"15",22,23.9340425531915,1.16463318058244
"16",23,24.0581058951965,0.987431147782684
"17",24,23.4854609929078,1.19542300490208
"18",25,23.6280334728033,0.977115814231141', sep = ",", header = T)

ridge_u_s_summary <- read.table(text = '"times","means","sd"
"1",1,23.5870433511934,0.937068380991415
"12",2,23.0331950207469,1.1488061017937
"19",3,23.3496395963479,1.02861071580118
"20",4,22.682918395574,1.28095122645134
"21",5,23.1064616862839,1.11960341933558
"22",6,22.4135546334716,1.36699461456579
"23",7,22.9523592814371,1.19411131617966
"24",8,22.5736514522822,1.44525141078227
"25",9,24.0131017964072,1.20022890842346
"2",10,24.4016586040083,1.61518041063122
"3",11,25.6837805462386,1.49361866061128
"4",12,26.0852107809261,2.0335844646935
"5",13,26.5968368080518,1.80035938711879
"6",14,26.7841959972395,2.24353092250421
"7",15,26.6535363222249,1.7733853965812
"8",16,26.5494824016563,1.97883869059574
"9",17,25.9478177458034,1.47079061322174
"10",18,25.5847212663455,1.67334903054897
"11",19,24.9841852651788,1.13952974451756
"13",20,24.505099931082,1.34188808323866
"14",21,24.3428365730742,0.964465091164753
"15",22,23.8500345065562,1.1826679089188
"16",23,23.9638415366146,0.934203639374674
"17",24,23.4138716356108,1.13425784998453
"18",25,23.7044528659403,0.977776310460964', sep = ",", header = T)

valley_u_s_summary <- read.table(text = '"times","means","sd"
"1",1,23.8609523809524,0.941137931036436
"12",2,23.6167464114833,1.36368015448422
"19",3,23.6931023210199,1.09423013467475
"20",4,23.2622807017544,1.40370217291909
"21",5,23.444655116051,1.13978244673363
"22",6,22.9831738437002,1.37794014701823
"23",7,23.2909416748126,1.17933203457316
"24",8,23.2392657621708,1.35015918020873
"25",9,24.302705345502,1.07391752061927
"2",10,24.8187699680511,1.39386829705572
"3",11,25.7716009129442,1.36677999460727
"4",12,26.1712689545092,1.97824543923682
"5",13,26.5006523157208,1.62284855146781
"6",14,26.6692430278884,2.08567545174535
"7",15,26.502446183953,1.55882287684552
"8",16,26.28759936407,1.72625533400646
"9",17,25.8817558746736,1.24691066538137
"10",18,25.5448412698413,1.45728060915926
"11",19,25.1434910277325,1.08377131492575
"13",20,24.8136616362192,1.32224607164181
"14",21,24.6306135770235,1.00789058764099
"15",22,24.3593328038125,1.32214170798361
"16",23,24.2933093994778,1.02334054495071
"17",24,23.9780778395552,1.34031144743681
"18",25,24.1241503976862,1.16464232030193', sep = ",", header = T)



ggplot(data=ridge_u_s_summary,aes(x = times,y=means))+
  geom_errorbar(data=ridge_u_s_summary,aes(ymin=means-sd,ymax=means+sd,colour="ridge"))+
  geom_line(aes(y=means,colour="ridge"))+
  geom_line(data = valley_u_s_summary,aes(x=times,y=means, color = "valley")) +
  geom_errorbar(data=valley_u_s_summary,aes(ymin=means-sd,ymax=means+sd, color = "valley"))+
  geom_line(data = edge_u_s_summary,aes(x=times,y=means,colour="edge"))+
  geom_errorbar(data=edge_u_s_summary,aes(ymin=means-sd,ymax=means+sd,colour="edge"))+
  labs(x="Time",y="Temperature in understory during January to April") +
  theme_classic() +
  scale_color_manual(values = c("red", "green", "blue"), name = "Legend")

As to your first question about changing color. 至于关于变色的第一个问题。 That is happening because you have included color = Habitat inside aes() . 发生这种情况是因为您在aes()包含了color = Habitat This means that you are mapping color to a constant aesthetic. 这意味着您正在将颜色映射到恒定的美感。 ggplot will assign a default color to that mapping. ggplot将为该映射分配默认颜色。 That's why your color changed to the default scheme. 这就是为什么您的颜色更改为默认方案。

To change the actual values you can use scale_color_manual and specify the values you want as shown above. 要更改实际值,可以使用scale_color_manual并指定所需的值,如上所示。 Basically, what it's doing is mapping the color aesthetic to user specified values instead of using default values. 基本上,它的工作是将颜色美感映射到用户指定的值,而不是使用默认值。 Note that you'll have to specify as many values as the number of aesthetic mappings you have used (3 in this case). 请注意,您必须指定与使用的美学贴图数量一样多的值(本例中为3)。 You can also modify other aspects of the legend using that option. 您也可以使用该选项修改图例的其他方面。

在此处输入图片说明

There is an easier way of accomplishing what you want by binding your data frames together 通过将数据帧绑定在一起,可以更轻松地完成所需的操作

edge_u_s_summary$variable <- "edge"
valley_u_s_summary$variable <- "valley"
ridge_u_s_summary$variable <- "ridge"
df <- rbind(edge_u_s_summary, valley_u_s_summary, ridge_u_s_summary)
ggplot(df, aes(times, means, color = variable)) + 
    geom_errorbar(aes(ymin = means - sd, ymax = means + sd)) + 
    geom_line() + 
    theme_classic() +
    scale_color_manual(values = c("red", "green", "blue"), name = "Legend")

You'll get a similar plot: 您会得到类似的情节:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM