How to seperate overlapping bars in ggplot2?

6.6k Views Asked by At

I have a very unfortunate looking bar graph:

enter image description here

I'm generating this using the following R code:

library(ggplot2)

dat <- read.table("output.tab", sep="\t")
ggplot(data=dat, aes(x=reorder(V1, -V2), y=V2, fill=V1)) +
        geom_bar(stat="identity", color="black", width=10) +
        theme(axis.text.x=element_text(angle=90, hjust=1), legend.position="none")

I don't know how to separate the bars. I've tried changing ggplot's aes width, I tried setting position="dodge", and a bunch of other things. Most of these still give me the following error:

position_stack requires non-overlapping x intervals

If anyone could help find a way to separate them it would be greatly appreciated.

1

There are 1 best solutions below

0
On

If you have a wall-sized area to print it on, try:

library(ggplot2)
dat <- data.frame(V1 = paste0(1:270,sample(letters, 270, replace=TRUE)),V2 =runif(270)*10)
dim(dat)

png("thatswhatshesaid.png",width=5000,height=250, units = "px")
ggplot(data=dat, aes(x=reorder(V1, -V2), y=V2, fill=V1)) +
        geom_bar(stat="identity", color="black", width=1) +
        theme(axis.text.x=element_text(angle=90, hjust=1), legend.position="none")
dev.off()

Obviously can't fit it here, but it doesn't overlap: sobig http://tinypic.com/r/1sl3py/8

But I question the usefulness of such a graph. Perhaps you can break the graph into components by some subcategory. Cramming 270 elements into a chart is seldom useful.