how make grouped bar chart using table of counts i.e.
v1 v2 v3 v4 v5 0 0 1 1 1 b 6 1 12 9 8 c 4 1 4 4 3 d 1 0 0 1 0 e 2 2 0 2 2
where x labels a-e , 5 grouped bars of each of a-e of given height of v1-v5 , colors corresponding v1-v5.
you need reshape data, here how can tidyr
, ggplot
:
library(tidyr); library(ggplot2); ggplot(gather(df, group, count, -x), aes(x = x, y = count, fill = group)) + geom_bar(stat = "identity", position = "dodge")
where added x column corresponds original rowname transformation purpose , gather
function transform original data frame wide long format:
x v1 v2 v3 v4 v5 1 0 0 1 1 1 2 b 6 1 12 9 8 3 c 4 1 4 4 3 4 d 1 0 0 1 0 5 e 2 2 0 2 2