ggplot scale_fill_manual无法按正确的顺序分配颜色

时间:2017-05-19 17:16:56

标签: r ggplot2

我希望变量的顺序对应于特定的颜色顺序。所以我明确指定颜色的顺序值对应于scale_fill_manual()中的break,如下面的脚本所示:

figure <- ggplot(df, aes(factor(groups), values, fill = parameter)) + 
  theme_bw(base_size = 20, base_family = "Helvetica") + 
  geom_bar(stat="identity", position = "dodge") + 
  labs(x = xlabel_name, y = ylabel_name)

figure + scale_fill_manual(values = c("red", "blue", "green", "yellow", "brown", "black"),
                           breaks = c('COLI_index', 'P_less_30k', 'employ_rat',  'NonWt_Pct', 'P_75k_200k', 'P_more_200')
)

基本上,我期待:

red:   COLI_index
blue:  P_less_30k
green: employ_rat
yellow:NonWt_Pct
brown: P_75k_200k
black: P_more_200

但在我得到的数字中,每个变量的颜色顺序为:

    red:   COLI_index
    blue:  employ_rat 
    green: NonWt_Pct
    yellow:P_75k_200k
    brown: P_less_30k
    black: P_more_200

顺便说一下,我正在做一个这样的分组条形图: enter image description here

任何部分错误的提示?

源数据框(df)如下:

            groups  parameter   values
NonWt_Pct   Lowest  NonWt_Pct   33.5
employ_rat  Lowest  employ_rat  80.5
P_less_30k  Lowest  P_less_30k  15.0
P_75k_200k  Lowest  P_75k_200k  41.7
P_more_200  Lowest  P_more_200  12.1
COLI_index  Lowest  COLI_index  49.2
NonWt_Pct1  Second  NonWt_Pct   53.7
employ_rat1 Second  employ_rat  78.2
P_less_30k1 Second  P_less_30k  26.9
P_75k_200k1 Second  P_75k_200k  28.3
P_more_2001 Second  P_more_200  7.6
COLI_index1 Second  COLI_index  68.3
NonWt_Pct2  Third   NonWt_Pct   76.3
employ_rat2 Third   employ_rat  69.8
P_less_30k2 Third   P_less_30k  44.7
P_75k_200k2 Third   P_75k_200k  15.8
P_more_2002 Third   P_more_200  6.4
COLI_index2 Third   COLI_index  83.4
NonWt_Pct3  Highest NonWt_Pct   91.4
employ_rat3 Highest employ_rat  59.0
P_less_30k3 Highest P_less_30k  60.6
P_75k_200k3 Highest P_75k_200k  9.8
P_more_2003 Highest P_more_200  3.5
COLI_index3 Highest COLI_index  91.6

0 个答案:

没有答案