ggplot2中的注释栏

时间:2018-02-02 19:53:47

标签: r plot ggplot2 bar-chart

在ggplot2中,我想绘制注释条,使条形颜色的顺序与其他列匹配。 See this image。这里是酒吧" A"颜色根据列y(真或假)和bar" B"相同。我试过跟随,但它没有工作。

> (df <- data.frame(x=rep(1:5, 2), y=rep(c(T,F), 5), z=c(rep("A",5), rep("B",5))))
   x     y z
1  1  TRUE A
2  2 FALSE A
3  3  TRUE A
4  4 FALSE A
5  5  TRUE A
6  1 FALSE B
7  2  TRUE B
8  3 FALSE B
9  4  TRUE B
10 5 FALSE B
> ggplot(data=df, aes(x=z, y=x, fill=y)) + geom_bar(stat="identity")+ coord_flip()

1 个答案:

答案 0 :(得分:1)

请改用geom_tilehttp://ggplot2.tidyverse.org/reference/geom_tile.html

ggplot(data=df, aes(x=z, y=x, fill=y)) +  
 geom_tile(stat="identity",width=0.5)+ coord_flip()

enter image description here

相关问题