cNinja
cNinja

Reputation: 145

annotation bars in ggplot2

In ggplot2 I want to plot annotation bars such that the order of the bar color matches other column. See this image. Here for bar "A" colors are according to column y (True or False) and same for bar "B". I tried following but it doe't work.

> (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()

Upvotes: 1

Views: 457

Answers (1)

Jason
Jason

Reputation: 581

Use geom_tile instead. http://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

Upvotes: 1

Related Questions