如何将颜色分配给ggplot2中具有稳定映射的分类变量? [英] How to assign colors to categorical variables in ggplot2 that have stable mapping?

查看:1184
本文介绍了如何将颜色分配给ggplot2中具有稳定映射的分类变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我的问题:



将颜色分配给ggplot2中具有稳定映射的分类变量的好方法是什么?我需要在具有不同子集和不同数量分类变量的一组图表中使用一致的颜色。

例如,

  plot1 < -  ggplot(data, aes(xData,yData,color = categoricaldData))+ geom_line()

其中 categoricalData 有5个级别。



然后

  plot2<  -  ggplot(data.subset,aes(xData.subset,yData.subset,
color = categoricaldData.subset))+ geom_line()

其中 categoricalData.subset 有3个关卡。



然而,两组中的特定关卡最终都会有不同的颜色,这使得难以一起阅读图表。



我需要在数据框中创建颜色矢量?还是有另一种方法来分配特定的颜色类别?

解决方案

对于OP中的确切示例,我同意Thierry的答案是最好的。但是,我认为指出另一种方法很有用,因为当您尝试在多个数据帧中保持一致的颜色方案时,这些颜色方案通过对单个大型数据框进行子集化而非 。在多个数据框架中管理因素级别可能会变得单调乏味,因为如果它们是从单独的文件中提取的,并且并非所有因子级别都出现在每个文件中。

解决这个问题的一种方法是创建一个自定义手动颜色比例如下:

  #Some测试数据
dat < - data.frame( x = runif(10),y = runif(10),
grp = rep(LETTERS [1:5],each = 2),stringsAsFactors = TRUE)

#创建一个自定义颜色比例
库(RColorBrewer)
myColors< - brewer.pal(5,Set1)
名称(myColors)< - levels(dat $ grp)
colScale < - scale_colour_manual(name =grp,values = myColors)

然后添加色阶根据需要将数据导入到图:

 #一个绘制所有数据
p < - ggplot(dat,aes( x,y,color = grp))+ geom_point()
p1 < - p + colScale

#仅有四个级别的第二个绘图
p2 < - p%+%droplevels(subset(dat [4:10,]))+ colScale

第一个图像如下所示:



,第二个图像如下所示:





通过这种方式,您不需要记住或检查每个数据框以查看它们是否具有适当的级别。


I've been getting up to speed with R in the last month.

Here is my question:

What is a good way to assign colors to categorical variables in ggplot2 that have stable mapping? I need consistent colors across a set of graphs that have different subsets and different number of categorical variables.

For example,

plot1 <- ggplot(data, aes(xData, yData,color=categoricaldData)) + geom_line()

where categoricalData has 5 levels.

And then

plot2 <- ggplot(data.subset, aes(xData.subset, yData.subset, 
                                 color=categoricaldData.subset)) + geom_line()

where categoricalData.subset has 3 levels.

However, a particular level that is in both sets will end up with a different color, which makes it harder to read the graphs together.

Do I need to create a vector of colors in the data frame? Or is there another way to assigns specific colors to categories?

解决方案

For simple situations like the exact example in the OP, I agree that Thierry's answer is the best. However, I think it's useful to point out another approach that becomes easier when you're trying to maintain consistent color schemes across multiple data frames that are not all obtained by subsetting a single large data frame. Managing the factors levels in multiple data frames can become tedious if they are being pulled from separate files and not all factor levels appear in each file.

One way to address this is to create a custom manual colour scale as follows:

#Some test data
dat <- data.frame(x=runif(10),y=runif(10),
        grp = rep(LETTERS[1:5],each = 2),stringsAsFactors = TRUE)

#Create a custom color scale
library(RColorBrewer)
myColors <- brewer.pal(5,"Set1")
names(myColors) <- levels(dat$grp)
colScale <- scale_colour_manual(name = "grp",values = myColors)

and then add the color scale onto the plot as needed:

#One plot with all the data
p <- ggplot(dat,aes(x,y,colour = grp)) + geom_point()
p1 <- p + colScale

#A second plot with only four of the levels
p2 <- p %+% droplevels(subset(dat[4:10,])) + colScale

The first plot looks like this:

and the second plot looks like this:

This way you don't need to remember or check each data frame to see that they have the appropriate levels.

这篇关于如何将颜色分配给ggplot2中具有稳定映射的分类变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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