如何为R中具有多个图层的条形图设置颜色?

时间:2020-02-03 16:26:21

标签: r ggplot2

我正在使用ggplot2从以下数据中使用三个变量“ stage”,“ sex”和“ total”来创建条形图:

    residtraj             sex    stage          perc total
   <chr>                 <fct>  <fct>         <dbl> <int>
 1 born rural live rural female No cancer   0.00725     1
 2 born rural live rural female Early stage 0.02        1
 3 born rural live rural female Late stage  0.0462      3
 4 born rural live rural male   No cancer   0.00625     2
 5 born rural live rural male   Early stage 0.0323      4
 6 born rural live rural male   Late stage  0.0602     13
 7 born rural live urban female No cancer   0.138      19
 8 born rural live urban female Early stage 0.12        6
 9 born rural live urban female Late stage  0.215      14
10 born rural live urban male   No cancer   0.194      62

我的代码为:

plot <- ggplot(data, aes(sex, total, fill=residtraj)) +
  geom_bar(stat="identity", position="dodge") +
  facet_wrap(~stage) +
  geom_text(aes(label=paste(total,"(",percent(perc),")")),
            position=position_dodge(width=1), hjust=0.5, vjust=-1, size=2.5) +
  ylab("count(%)")+ ggtitle("Distribution of residence by sex and stage")

我的问题是,如何根据“ residtraj”和“ sex”设置不同的颜色? (现在,仅根据“ residtraj”的值自动分配颜色)

1 个答案:

答案 0 :(得分:2)

您可以使用interactionaes的填充参数中加入“ residtraj”和“ sex”:

library(ggplot2)
ggplot(df, aes(sex, total, fill=interaction(residtraj,sex))) +
  geom_bar(stat="identity", position="dodge") +
  facet_wrap(~stage)

enter image description here

数据

structure(list(Row = 1:10, residtraj = c("bornruralliverural", 
"bornruralliverural", "bornruralliverural", "bornruralliverural", 
"bornruralliverural", "bornruralliverural", "bornruralliveurban", 
"bornruralliveurban", "bornruralliveurban", "bornruralliveurban"
), sex = c("female", "female", "female", "male", "male", "male", 
"female", "female", "female", "male"), stage = c("Nocancer", 
"Earlystage", "Latestage", "Nocancer", "Earlystage", "Latestage", 
"Nocancer", "Earlystage", "Latestage", "Nocancer"), perc = c(0.00725, 
0.02, 0.0462, 0.00625, 0.0323, 0.0602, 0.138, 0.12, 0.215, 0.194
), total = c(1L, 1L, 3L, 2L, 4L, 13L, 19L, 6L, 14L, 62L)), row.names = c(NA, 
-10L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x55efd77ad350>)