每行都基于ggplot2中的不同数据帧 - 自动着色和图例 [英] multiple lines each based on a different dataframe in ggplot2 - automatic coloring and legend

查看:80
本文介绍了每行都基于ggplot2中的不同数据帧 - 自动着色和图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下数据框:

  df1 = data.frame(c11 = c(1:5), c12 = c(1:5))
df2 = data.frame(c21 = c(1:5),c22 =(c(1:5))^ 0.5)
df3 = data.frame (c31 = c(1:5),c32 =(c(1:5))^ 2)

我想在同一个plot / panel中将它们绘制成线。我可以通过

  p < -  ggplot()+ geom_line(data = df1,aes(x = c11,y = c12))+ 
geom_line(data = df2,aes(x = c21,y = c22))+
geom_line(data = df3,aes(x = c31,c32))

所有这些都是黑色的。如果我想让它们具有不同的颜色,我可以将该颜色明确指定为 geom_line()的参数。我的问题是,我可以指定一些颜色的列表,比如5种颜色,例如红色,蓝色,绿色,橙色和灰色,并使用该列表,以便我不必显式指定颜色作为geom_line的参数()在每行的情况下。如果plot p 包含2个geom_line()语句,则它们将分别为它们着色为红色和蓝色。如果它包含3个geom_line语句,它会将它们打成红色,蓝色和绿色。最后,我如何指定这些图的图例。即使我可以在 p 结尾处将颜色作为一个向量赋予颜色,那也很棒。请让我知道如果问题不清楚。



谢谢。

解决方案

如果您使用包含不同列的熔化data.frame来指定不同的美学效果,ggplot2的效果最佳。使用常见列名称融化更容易,所以我会从那里开始。以下是我要采取的步骤:


  • 重命名列

  • 我们将映射到颜色美学的一个新变量定义您的颜色矢量
  • 使用scale_colour_manual指定适当的比例



'

 名称(df1)<  -  c (x,y)
名称(df2)< -c(x,y)
名称(df3)< -c(x )

newData< - melt(list(df1 = df1,df2 = df2,df3 = df3),id.vars =x)

#指定你的颜色矢量
cols< - c(红色,蓝色,绿色,橙色,灰色)

#绘制数据并指定手动缩放
ggplot(newData,aes(x,value,color = L1))+
geom_line()+
scale_colour_manual(values = cols)

$ b

为清晰起见进行了编辑
$ b newData

 'data.frame':15 obs。 4个变量:
$ x:int 1 2 3 4 5 1 2 3 4 5 ...
$变量:因子w / 1等级y:1 1 1 1 1 1 1 1 1 1 ...
$ value:num 1 2 3 4 5 ...
$ L1:chrdf1df1df1df1...

而情节本身:


Suppose I have the following data frames:

df1 = data.frame(c11 = c(1:5), c12 = c(1:5))
df2 = data.frame(c21 = c(1:5), c22 = (c(1:5))^0.5)
df3 = data.frame(c31 = c(1:5), c32 = (c(1:5))^2)

I want to plot these as lines in the same plot/panel. I can do this by

p <- ggplot() + geom_line(data=df1, aes(x=c11, y = c12)) + 
     geom_line(data=df2, aes(x=c21,y=c22)) + 
     geom_line(data=df3, aes(x=c31, c32))

All these will be black. If I want them in a different color, I can specify the color explicitly as an argument to geom_line(). My question is can I specify a list of a few colors, say 5 colors, such as, red, blue, green, orange, gray, and use that list so that I do not have to explicitly specify the colors as an argument to geom_line() in case of each line. If the plot p contains 2 geom_line() statements then it will color them red and blue respectively. If it contains 3 geom_line statements, it will color them red, blue and green. Finally, how can I specify the legend for these plots. Even if I can give the colors as a vector at the end of p that would be great. Please let me know if the question is not clear.

Thanks.

解决方案

ggplot2 works best if you work with a melted data.frame that contains a different column to specify the different aesthetics. Melting is easier with common column names, so I'd start there. Here are the steps I'd take:

  • rename the columns
  • melt the data which adds a new variables that we'll map to the colour aesthetic
  • define your colour vector
  • Specify the appropriate scale with scale_colour_manual

'

names(df1) <- c("x", "y")
names(df2) <- c("x", "y")
names(df3) <- c("x", "y")

newData <- melt(list(df1 = df1, df2 = df2, df3 = df3), id.vars = "x")

#Specify your colour vector
cols <- c("red", "blue", "green", "orange", "gray")

#Plot data and specify the manual scale
ggplot(newData, aes(x, value, colour = L1)) + 
  geom_line() +
  scale_colour_manual(values = cols)

Edited for clarity

The structure of newData:

'data.frame':   15 obs. of  4 variables:
 $ x       : int  1 2 3 4 5 1 2 3 4 5 ...
 $ variable: Factor w/ 1 level "y": 1 1 1 1 1 1 1 1 1 1 ...
 $ value   : num  1 2 3 4 5 ...
 $ L1      : chr  "df1" "df1" "df1" "df1" ...

And the plot itself:

这篇关于每行都基于ggplot2中的不同数据帧 - 自动着色和图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆