Bioinformatician - 3 years ago 167
R Question

# Make a grouped bar chart of this data

I was searching on this forum but every grouped bar chart I found was based on the grouping on a column. However I already used Java to combine my data, but I don't know how to plot this data in a grouped bar chart:
How I create the matrix (I wil omit all code to keep it simple):

``````MAT2 = matrix(data, ncol=2)
MAT2 = cbind(MAT2,kolommen)
colnames(MAT2) <- c("mpk6","mpk3","group")
``````

which gives me:

``````       mpk6 mpk3 group
[1,] "9"  "0"  "membrane"
[2,] "91" "64" "plastid"
[3,] "12" "0"  "extracellular region"
[4,] "74" "21" "thylakoid"
[5,] "6"  "0"  "vacuole"
[6,] "21" "33" "cytosol"
[7,] "4"  "0"  "plasma membrane"
[8,] "9"  "12" "peroxisome"
[9,] "0"  "0"  "mitochondrion"
[10,] "22" "13" "nucleolus"
[11,] "0"  "0"  "ribosome"
[12,] "27" "31" "cell wall"
``````

But when I plot this using barplot I get the following result:

The bars are grouped based on mpk6 and mpk3 but I want to group for "membrane","plastid","extracellular region" etc..:

I dont know if it possible but if it is it would be nice to just remove the bars of a height 0, so that there isn't that much whitespace in the graph(but maybe this will ruine the whole idea of the grouped bar chart, so if there are suggestions of the best option they are welcome!)

Answer Source

Assuming your data is in a data.frame called `A` or it can be coerced to a data.frame, here is my first guess using `tidyr`, `dplyr` and `ggplot2` libraries :

``````A <- gather(A,mpk,n,mpk6:mpk3)

ggplot(A %>% filter(n>0) %>% mutate(group=droplevels(group)),
aes(x=group, y=n, group=mpk, fill=mpk)) +
geom_bar(position='dodge', stat='identity') +
coord_flip()
``````

The whitespace is no longer visible, but the width of each bar is fixed regardless of how many different mpk-n measurements are associated with it

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download