ggplot2:将颜色固定为因子水平 [英] ggplot2: Fix colors to factor levels

查看:88
本文介绍了ggplot2:将颜色固定为因子水平的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个较大的项目,为此我在ggplot2中创建了多个绘图。这些图涉及在几个不同的谨慎类别(例如:国家,物种,类型)上绘制几个不同的结果。我想完全解决离散类型到颜色的映射问题,使得在所有图中,无论是否存在其他因素,Type = A始终以红色显示,Type = B始终以蓝色显示,依此类推。我知道 scale_fill_manual()的地方,我可以手动提供颜色值,然后使用 drop = FALSE 进行处理具有未使用的因子水平。但是,我发现这非常麻烦,因为每个图都需要一些手动工作才能以正确的方式处理因素,对颜色值进行分类以匹配因素排序,降低未使用的水平等。

I'm working on a larger project for which I am creating several plots in ggplot2. The plots are concerned with plotting several different outcomes across several different discreet categories (think: countries, species, types). I would like to completely fix the mapping of discrete types to colors such that Type=A is always displayed in red, Type=B is always displayed in blue, and so on across all plots irrespective of what other factors are present. I know about scale_fill_manual() where I can provide color values manually and then work with drop = FALSE which helps in dealing with unused factor levels. However, I find this extremely cumbersome since every plot will need some manual work to deal with sorting the factors in the right way, sorting color values to match factor sorting, dropping unused levels, etc.

我正在寻找的是一种可以将一次和全局因子级别映射到特定颜色(A =绿色,B =蓝色,C =红色,...)和然后只要画出我喜欢的颜色,然后ggplot选择正确的颜色即可。

What I am looking for is a way where I can map once and globally factor levels to specific colors (A=green, B=blue, C=red, ...) and then just go about plotting whatever I please and ggplot picking the right colors.

这里有一些代码来说明这一点。

Here is some code to illustrate the point.

# Full set with 4 categories
df1 <- data.frame(Value = c(40, 20, 10, 60), 
                  Type = c("A", "B", "C", "D"))

ggplot(df1, aes(x = Type, y = Value, fill = Type)) + geom_bar(stat = "identity")


# Colors change complete because only 3 factor levels are present
df2 <- data.frame(Value = c(40, 20, 60), 
                  Type = c("A", "B", "D"))

ggplot(df2, aes(x = Type, y = Value, fill = Type)) + geom_bar(stat = "identity")


# Colors change because factor is sorted differently
df3 <- data.frame(Value = c(40, 20, 10, 60), 
                  Type = c("A", "B", "C", "D"))
df3$Type <- factor(df3$Type, levels = c("D", "C", "B", "A"), ordered = TRUE)

ggplot(df3, aes(x = Type, y = Value, fill = Type)) + geom_bar(stat = "identity")


推荐答案

您可以进行自定义绘图功能(包括 scale_fill_manual 和合理的默认颜色),以避免重复代码:

You could make a custom plot function (including scale_fill_manual and reasonable default colours) in order to avoid repeating code:

library(ggplot2)
custom_plot <- function(.data,
  colours = c("A" = "green", "B" = "blue", "C" = "red", "D" = "grey"))  {
  ggplot(.data, aes(x=Type, y=Value, fill= Type)) + geom_bar(stat="identity") +
   scale_fill_manual(values = colours)
}

df1 <- data.frame(Value=c(40, 20, 10, 60), Type=c("A", "B", "C", "D"))
df2 <- data.frame(Value=c(40, 20, 60), Type=c("A", "B", "D"))
df3 <- data.frame(Value=c(40, 20, 10, 60), Type=c("A", "B", "C", "D"))
df3$Type <- factor(df3$Type, levels=c("D", "C", "B", "A"), ordered=TRUE)

custom_plot(df1)
custom_plot(df2)
custom_plot(df3)

这篇关于ggplot2:将颜色固定为因子水平的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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