简体   繁体   中英

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:

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:

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

"","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:

"","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:

"","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() . This means that you are mapping color to a constant aesthetic. ggplot will assign a default color to that mapping. 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. 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). 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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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