R中的HairEyeColor条形图

时间:2014-10-16 04:02:31

标签: r

这与:HairEyeColor bar chart in R

有关

我正在使用以下代码生成类似的情节:

mm = melt(HairEyeColor)                                                                                                             
ggplot(mm)+geom_bar(aes(x=Hair, y=value, fill=Eye), stat='identity',position='dodge')+facet_grid(Sex~.)

enter image description here

我希望每个条形图在上部显示头发颜色,在下部显示眼睛颜色。如何才能做到这一点?我试图修改上一个问题中的代码但无法管理。谢谢你的帮助。

1 个答案:

答案 0 :(得分:3)

在这里,我认为这不是一个很好的可视化......

mm2 <- mm
mm2$value <- mm2$value/4

ggplot(mm)+geom_bar(aes(x=Hair, y=value, fill=Eye), stat='identity',position='dodge')+facet_grid(Sex~.) +
  geom_bar(data = mm2, mapping = aes(x=Hair, y=value, fill=Hair, group=Eye), stat = "identity", position = "dodge", colour = "white") +
  scale_fill_manual(values = sub("blond", "yellow", sub("hazel", "gold", tolower(sort(unique(c(levels(mm$Eye), levels(mm$Hair))))))))

更新

mm2 <- mm
mm2$value <- mm2$value/2

ggplot(mm)+geom_bar(aes(x=Hair, y=value, fill=Hair, group = Eye), stat='identity',position='dodge', colour = "white")+facet_grid(Sex~.) +
    geom_bar(data = mm2, mapping = aes(x=Hair, y=value, fill=Eye, group=Eye), stat = "identity", position = "dodge", colour = "white") +
    scale_fill_manual(values = sub("blond", "yellow", sub("hazel", "gold", tolower(sort(unique(c(levels(mm$Eye), levels(mm$Hair))))))))

enter image description here