带有分区主标签和子标签的堆叠条形图

时间:2014-01-09 14:55:03

标签: r stack bar-chart

我是R.的新手。我想根据以下数据创建堆叠条形图。我想将这些数据绘制成堆积的条形图,其描述了x轴上每种基因型和种族组合的疾病评分百分比,例如, 76R-race 1,rmc-race 1.同样如何简化x轴,通过将每个种族分成76R和rmc组合而不是标记每个组合,即代替用76R-race 1标记每个杆,rmc-race 1等,如何将第1轮标记为主轴,以76R和rmc为子轴,依此类推。

disease.nov <- data.frame(disease.score = c(0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 2, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7, 5, 5, 4, 4, 6, 5, 8, 5, 5, 5, 6, 4, 7, 5, 8, 6, 1, 2, 2, 4, 5, 8, 5, 6, 7, 4, 4, 4, 2, 3, 5, 6, 7, 7, 5, 2, 6, 6, 6, 4, 5, 8, 7, 5, 2, 5, 6,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0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
                      genotype=gl(2,52,624, labels=c("76R","rmc")),     
                      race=gl(6,104,624, labels=c("race 1","race 2","race 3","WAC 7673","WAC 7591","control")))

1 个答案:

答案 0 :(得分:0)

我不确定这是否是你要找的:

library(ggplot2)
library(scales)

ggplot(disease.nov, aes(x=genotype, fill=factor(disease.score))) + 
  geom_bar(position="fill") + 
  facet_wrap(~ race) + 
  scale_y_continuous(labels = percent_format())

产生:

enter image description here