简体   繁体   English

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

[英]Assigning colors to multiple lines and adding a legend in ggplot

I successfully created multiple line plots on a single plot using ggplot. 我使用ggplot成功在单个图上创建了多个线图。 However, I am having difficulty adjusting the colors for each line, and then creating/formatting a legend corresponding to these colors. 但是,我很难调整每行的颜色,然后创建/格式化与这些颜色相对应的图例。 First, here is the data that I am working with (contained in four separate data frames): 首先,这是我正在使用的数据(包含在四个单独的数据框中):

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

I tried the following: 我尝试了以下方法:

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)

This yields this error: 这将产生此错误:

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

I know that there is a comma placed incorrectly somewhere, but I suspect that will not correct the overall problem of trying to assign specific colors to each line plot. 我知道在某处错误地放置了一个逗号,但是我怀疑这不会解决尝试为每个线图分配特定颜色的总体问题。 Ideally, for "gg", I would like that line to be "green", while for the second, third and fourth lines, I want those to be "blue", "red" and "black", respectively. 理想情况下,对于“ gg”,我希望该行为“绿色”,而对于第二,第三和第四行,我希望它们分别为“蓝色”,“红色”和“黑色”。 Then, I want to show and be able to format this in a legend, showing what each color represents. 然后,我想显示图例并设置其格式,以显示每种颜色代表的含义。

Any help with this would be extremely valuable! 任何与此有关的帮助将非常宝贵! It seems straight-forward, but I think the problem is related to where the "color" command needs to be placed for each line plot, and then constructing a legend. 看起来很简单,但我认为问题与每个线图需要放置“颜色”命令的位置有关,然后构造图例。

Thanks, 谢谢,

Starting with the comma issue (and then let me know if the rest works out!) 从逗号问题开始(然后让我知道其余的解决方法!)

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)

In case I misunderstood your data, here is an example using the famous Iris data: 万一我误解了您的数据,下面是使用著名的Iris数据的示例:

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)

To get a legend, by far the easiest way is presented in this answer . 为了得到一个传说,到目前为止,在这个答案中给出了最简单的方法。 This gist of it is, use melt from reshape2 to get all values into one dataframe with a categorical variable that determines color. 它这个要点是,使用meltreshape2得到的所有值到一个数据帧与确定颜色分类变量。

Edit: using OP data 编辑:使用OP数据

Getting a legend set up in ggplot for data from multiple tables will be much easier if you can combine them into one table with common variables, where the source table is noted in another variable. 如果您可以在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()

在此处输入图片说明


loading data from OP 从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")

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

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