【发布时间】:2020-02-03 16:26:21
【问题描述】:
我正在使用 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”的值自动分配)
【问题讨论】:
-
所以你会有 4 种颜色?比如 1 代表农村 + 男性、农村 + 女性等等。
-
是的,像这样。