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

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

问题描述

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



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



我的目标是为一组组设置一个单一颜色的pallete,使得任何给定的组在所有图形中是相同的颜色。
在下面的例子中,这意味着 C组 Plot 1 中的相同颜色 Plot 2



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



我尝试了scale_fill_manual(和scal_color_manual,适当时)的几个变体。虽然颜色是从指定的pallete中选择的,但
我不能按组指定调色板,或用其他方法跳过对应于缺失组的颜色



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





<$ c $ p> #SAMPLE DATA:
DT1 DT2
#SAMPLE Color Pallette
ColorsDT
#根据组添加一个颜色列
DT1 [ColorsDT,Color:= i.Color]
DT2 [ColorsDT,Color:= i.Color]

#A Basic Plot
simplePlot< )
ggplot(DT,aes(x = Name,y = Value,fill = Group))+
geom_bar(stat =identity)+ xlab()+ ggtitle $ b#尝试的sevearl变体:
#+ scale_fill_manual(values = ColorsDT $ Color)


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


解决方案

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

  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)+
#指定颜色
scale_fill_manual(values = group.colors)

然后使用你的图:

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


$ b b

>



我认为你的方法的问题是颜色没有命名,所以 scale_fill_manual()不能将它们合并。比较:

  ColorsDT<  -  data.table(Group = LETTERS [1:5],Color = c(#333BFF #CC6600,#9633FF,#E2FF33,#E3DB71),key =Group)
ColorsDT
#Group Color
# 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
#ABCDE
#1:#333BFF#CC6600#9633FF#E2FF33#E3DB71


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.

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.

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.

# 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)

Then using your plots:

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

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

with:

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天全站免登陆