ggplot2:修复因子级别的颜色

Chr*_*ris 12 r colors ggplot2

我正在开发一个更大的项目,我正在ggplot2中创建几个图.这些图涉及绘制几个不同类别的几种不同结果(想想:国家,物种,类型).我想完全修复离散类型到颜色的映射,使得Type = A始终显示为红色,Type = B始终显示为蓝色,依此类推所有绘图,而不管其他因素是什么.我知道scale_fill_manual()我可以在哪里手动提供颜色值,然后使用drop = FALSE它有助于处理未使用的因子水平.但是,我发现这非常麻烦,因为每个绘图都需要一些手动工作来处理以正确方式排序因子,排序颜色值以匹配因子排序,丢弃未使用的级别等.

我正在寻找的是一种方式,我可以映射一次和全局因素水平到特定的颜色(A =绿色,B =蓝色,C =红色,......)然后只是绘制任何我喜欢的绘图和ggplot挑选正确的颜色.

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

# 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")
Run Code Online (Sandbox Code Playgroud)

Kri*_*ing 14

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

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)
Run Code Online (Sandbox Code Playgroud)

  • 啊!所以scale_fill_manual()*可以*处理命名向量!那正是我想要的。谢谢克里斯托弗。 (3认同)

ali*_*ire 13

如果您愿意,可以定义自己的自定义比例.如果你看一下来源scale_fill_manual,

scale_fill_manual
#> function (..., values) 
#> {
#>     manual_scale("fill", values, ...)
#> }
#> <environment: namespace:ggplot2>
Run Code Online (Sandbox Code Playgroud)

它实际上非常简单:

library(ggplot2)

scale_fill_chris <- function(...){
    ggplot2:::manual_scale(
        'fill', 
        values = setNames(c('green', 'blue', 'red', 'orange'), LETTERS[1:4]), 
        ...
    )
}

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_col() + 
    scale_fill_chris()
Run Code Online (Sandbox Code Playgroud)

df2 <- data.frame(Value = c(40, 20, 60), 
                  Type = c("A", "B", "D"))

ggplot(df2, aes(x = Type, y = Value, fill = Type)) + 
    geom_col() + 
    scale_fill_chris()
Run Code Online (Sandbox Code Playgroud)

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_col() + 
    scale_fill_chris()
Run Code Online (Sandbox Code Playgroud)


小智 5

另一种选择是drop = F通过如下定义默认色阶来设置默认值:

scale_colour_discrete <- function(...)
  scale_colour_manual(..., drop = F)
scale_fill_discrete <- function(...)
  scale_fill_manual(..., drop = F)
Run Code Online (Sandbox Code Playgroud)

这样颜色对于不同的因素总是一致的。