为 ggplot2 手动设置组颜色 [英] Manually setting group colors for ggplot2

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

问题描述

我有一个数据集,我必须从中绘制几十个图.数据由 30 个组和每组内的多个测量值组成.

I have a data set from which I have to make several dozen plots. The data consists of 30 Groups and several measurements within each group.

大多数绘图不会一次使用所有组.

Most plots will not use all the groups at once.

我的目标是为一组组提供一个单一的颜色调色板,这样任何给定的组在所有图形中都是相同的颜色.在下面的示例中,这意味着 C 组Plot 1Plot 2 中的颜色相同.

My goal is to have one single color pallete for the set of groups such that any given group is the same color across all graphs. In the example below, this would mean that Group C is the same color in Plot 1 and in Plot 2.

我的问题是如何解决这个问题.

My question is how to go about this.

我尝试了 scale_fill_manual(和 scal_color_manual,在适当的时候)的几种变体.虽然颜色是从指定的调色板中选择的,我似乎无法按组索引调色板,或以其他方式跳过"与缺失组对应的颜色

I've tried several variations of scale_fill_manual (and scal_color_manual, when appropriate). While the colors are chosen from the designated pallete, I cannot seem to index the pallette by the group, or some other way to "skip" over the colors corresponding to a missing group

我还想尝试将颜色信息添加为正在绘制的数据列,但我不确定如何使用此列作为组的颜色.

I also thought to try to add the color information as a column of the data being plotted, but I'm unsure how to then use this column as the color for the group.

library(ggplot2)
library(data.table)

# SAMPLE DATA: 
DT1 <- data.table(Name=c("C_sub1", "A_sub1", "A_sub2"), Value=c(2,5,3), Group=c("C", "A", "A"), key="Group")
DT2 <- data.table(Name=c("A_sub1", "B_sub1", "C_sub1", "C_sub2"), Value=c(4,3,6,3), Group=c("A", "B", "C", "C"), key="Group")

# SAMPLE Color Pallette  
ColorsDT <-  data.table(Group=LETTERS[1:5], Color=c("#333BFF", "#CC6600", "#9633FF", "#E2FF33", "#E3DB71"), key="Group")

# Add a column for Color, according to the Group
DT1[ColorsDT, Color := i.Color]
DT2[ColorsDT, Color := i.Color]

# A Basic Plot
simplePlot <- function(DT, tit) 
  ggplot(DT ,aes(x=Name, y=Value, fill=Group)) +
   geom_bar(stat="identity") + xlab("") + ggtitle(tit)
    # Tried sevearl variations of: 
    #  + scale_fill_manual(values=ColorsDT$Color)


# Plot Them
grid.arrange(ncol=2,  simplePlot(DT1, tit="Plot 1"),  simplePlot(DT2, tit="Plot 2"))

推荐答案

您可以将您的每个组与一种颜色相关联,然后传递给函数:

You can associate each of your groups with a colour, then pass to the function:

group.colors <- c(A = "#333BFF", B = "#CC6600", C ="#9633FF", D = "#E2FF33", E = "#E3DB71")

simplePlot <- function(DT, tit) 
  ggplot(DT ,aes(x=Name, y=Value, fill=Group)) +
  geom_bar(stat="identity") + xlab("") + ggtitle(tit) +
  #Specify colours
  scale_fill_manual(values=group.colors)

然后使用您的绘图:

grid.arrange(ncol=2,  simplePlot(DT1, tit="Plot 1"), 
  simplePlot(DT2, tit="Plot 2"))

我认为您的方法的问题在于颜色没有命名,所以 scale_fill_manual() 无法将它们关联起来.比较:

I think the issue with your approach was that the colours weren't named, so scale_fill_manual() can't assoicate them. Compare:

ColorsDT <-  data.table(Group=LETTERS[1:5], Color=c("#333BFF", "#CC6600", "#9633FF", "#E2FF33", "#E3DB71"), key="Group")
ColorsDT
#   Group   Color
#1:     A #333BFF
#2:     B #CC6600
#3:     C #9633FF
#4:     D #E2FF33
#5:     E #E3DB71

与:

ColorsDT.name <-  data.table(A = "#333BFF", B = "#CC6600", C = "#9633FF", D = "#E2FF33", E =  "#E3DB71")
ColorsDT.name
#          A       B       C       D       E
# 1: #333BFF #CC6600 #9633FF #E2FF33 #E3DB71

这篇关于为 ggplot2 手动设置组颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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