为多行分配颜色并在ggplot中添加图例

时间:2019-04-26 17:50:00

标签: r ggplot2

我使用ggplot成功地在一个绘图上创建了多个线图。但是,我很难调整每行的颜色,然后创建/格式化与这些颜色相对应的图例。首先,这是我正在使用的数据(包含在四个单独的数据框中):

newdata<-data.frame(RCP1pctCO2cumulative, Column) #length 140
newdata1<-data.frame(RCP8.5cumulative, Column1) #length 90
newdata2<-data.frame(RCP4.5cumulative, Column2) #length 90
newdata3<-data.frame(Historicalcumulative, Column3) #length 145

head(newdata) 

RCP1pctCO2cumulative   Column
layer.1          0.000000000 30.62975
layer.2          0.006974906 30.29012
layer.3          0.013907599 30.43212
layer.4          0.021697436 30.70810
layer.5          0.030232970 30.38155
layer.6          0.038998084 30.34130

head(newdata1)

RCP8.5cumulative  Column1
layer.1        0.4475691 31.94422
layer.2        0.4569296 31.93002
layer.3        0.4663113 31.98923
layer.4        0.4756628 32.16458
layer.5        0.4850761 32.20246
layer.6        0.4946258 32.16779

head(newdata2)

RCP4.5cumulative  Column2
layer.1        0.4487829 32.05137
layer.2        0.4584334 32.01951
layer.3        0.4680946 32.04347
layer.4        0.4777492 32.23928
layer.5        0.4875477 32.61044
layer.6        0.4974490 32.14446

head(newdata3)

Historicalcumulative  Column3
layer.1         0.0000000000 30.20725
layer.2         0.0009499752 30.30651
layer.3         0.0017818766 30.35118
layer.4         0.0025179833 30.13334
layer.5         0.0031186696 30.14842
layer.6         0.0036720898 29.87103

我尝试了以下操作:

gg<-ggplot(newdata, aes(x=RCP1pctCO2cumulative, y=Column)) + 
geom_smooth(), color="black")

gg + geom_smooth(data=newdata1, aes(x=RCP4.5cumulative, y=Column1)) + 
geom_smooth(data=newdata2, aes(x=RCP8.5cumulative, y=Column2)) + 
geom_smooth(data=newdata3, aes(x=Historicalcumulative, y=Column3)), 
color="red", size=3)

这会产生此错误:

Error: unexpected ',' in "gg + geom_smooth(data=newdata1, 
aes(x=RCP4.5cumulative, y=Column1)) + geom_smooth(data=newdata2, 
aes(x=RCP8.5cumulative, y=Column2)) + geom_smooth(data=newdata3, 
aes(x=Historicalcumulative, y="

我知道在某处的逗号放置不正确,但是我怀疑这不会解决尝试为每个线图分配特定颜色的总体问题。理想情况下,对于“ gg”,我希望该行为“绿色”,而对于第二,第三和第四行,我希望它们分别为“蓝色”,“红色”和“黑色”。然后,我想显示图例并设置其格式,以显示每种颜色代表的含义。

任何对此的帮助将非常有价值!看起来很简单,但我认为问题与每个线图需要放置“颜色”命令的位置有关,然后构造图例。

谢谢

2 个答案:

答案 0 :(得分:1)

从逗号问题开始(然后让我知道其余问题是否解决!)

gg<-ggplot(newdata, aes(x=RCP1pctCO2cumulative, y=Column)) + 
geom_smooth(color="#000000")

gg + geom_smooth(data=newdata1, aes(x=RCP4.5cumulative, y=Column1)) + 
geom_smooth(data=newdata2, aes(x=RCP8.5cumulative, y=Column2)) + 
geom_smooth(data=newdata3, aes(x=Historicalcumulative, y=Column3), 
color="red", size=3)

如果我误解了您的数据,下面是使用著名的鸢尾花数据的示例:

ggplot(data=iris, aes(x=Sepal.Length, y=Sepal.Width)) + 
geom_smooth(color="#000000") + 
geom_smooth(data=iris, aes(x=Petal.Length, y=Petal.Width), color="#FF0000", size=3)

要获得传奇,到目前为止,此answer中提供了最简单的方法。其要点是,使用melt中的reshape2,将所有值放入一个具有确定颜色的分类变量的数据框中。

答案 1 :(得分:1)

编辑:使用OP数据

如果您可以在ggplot中为来自多个表的数据设置图例,则可以轻松得多,如果您可以将它们与具有公共变量的表组合在一起,而源表在另一个变量中注明。

library(tidyverse)
# Convenience function to rename columns to something uniform
make_uniform <- function(df, sourcename) {
  df %>%
    rename(x = 2, y = 3) %>% # Rename 2nd and 3rd columns
    mutate(source = sourcename)
}

combined <- bind_rows(
  RCP1pctCO2cumulative %>% make_uniform("CO2"),
  RCP8.5cumulative     %>% make_uniform("RCP8.5"), 
  RCP4.5cumulative     %>% make_uniform("RCP4.5"), 
)

> combined
   rowname           x        y source
1  layer.1 0.000000000 30.62975    CO2
2  layer.2 0.006974906 30.29012    CO2
3  layer.3 0.013907599 30.43212    CO2
4  layer.4 0.021697436 30.70810    CO2
5  layer.5 0.030232970 30.38155    CO2
6  layer.6 0.038998084 30.34130    CO2
7  layer.1 0.447569100 31.94422 RCP8.5
8  layer.2 0.456929600 31.93002 RCP8.5
9  layer.3 0.466311300 31.98923 RCP8.5
10 layer.4 0.475662800 32.16458 RCP8.5
11 layer.5 0.485076100 32.20246 RCP8.5
12 layer.6 0.494625800 32.16779 RCP8.5
13 layer.1 0.448782900 32.05137 RCP4.5
14 layer.2 0.458433400 32.01951 RCP4.5
15 layer.3 0.468094600 32.04347 RCP4.5
16 layer.4 0.477749200 32.23928 RCP4.5
17 layer.5 0.487547700 32.61044 RCP4.5
18 layer.6 0.497449000 32.14446 RCP4.5

ggplot(combined, aes(x, y, color = source)) +
  geom_smooth()

enter image description here


从OP加载数据

RCP1pctCO2cumulative  <- read.table(
  header = T, 
  stringsAsFactors = F,
  text = "rowname RCP1pctCO2cumulative   Column
layer.1          0.000000000 30.62975
layer.2          0.006974906 30.29012
layer.3          0.013907599 30.43212
layer.4          0.021697436 30.70810
layer.5          0.030232970 30.38155
layer.6          0.038998084 30.34130")


RCP8.5cumulative  <- read.table(
  header = T, 
  stringsAsFactors = F,
  text = "rowname RCP8.5cumulative  Column1
layer.1        0.4475691 31.94422
layer.2        0.4569296 31.93002
layer.3        0.4663113 31.98923
layer.4        0.4756628 32.16458
layer.5        0.4850761 32.20246
layer.6        0.4946258 32.16779")



RCP4.5cumulative    <- read.table(
  header = T, 
  stringsAsFactors = F,
  text = "rowname RCP4.5cumulative  Column2
layer.1        0.4487829 32.05137
layer.2        0.4584334 32.01951
layer.3        0.4680946 32.04347
layer.4        0.4777492 32.23928
layer.5        0.4875477 32.61044
layer.6        0.4974490 32.14446")