How to produce non-standard plot type with ggplot?

Go To StackoverFlow.com

15

I would like to make a plot with ggplot that looks very close to this (found here):

enter image description here

However, instead of frequency I would like to plot ranges for conditions. Here is a sketch of the plot I want to produce with 5 conditions:

enter image description here

My data is arranged as a start and end coordinate for the range. For example, for condition 5 the start of the range is 30 and the end of the range is 40 (I've labeled this about the graph for clarity). My data is from a file in the form:

id      start   end
1       20      35
2       60      75    
3       10      30
4       80      90
5       30      40

I have about 100 start and end values that I would like to plot in this manner on one graph. The final plot should be only two colors.

UPDATE:

For future reference, Justin's solution produces this:

enter image description here

2012-04-04 19:21
by drbunsen
very nicely specified question - Ben Bolker 2012-04-04 21:17


13

Something like this:

library(ggplot2)
library(reshape)
dat <- data.frame(lets=letters[1:5], low=1:5, mid=3:7, high=10:14)
dat.melt <- melt(dat, id.vars='lets')


ggplot(dat.melt, aes(x=lets, y=value, fill=variable)) + 
  geom_bar(stat='identity') + 
  scale_fill_manual(breaks=c('low','mid','high'), values=c('blue','red','blue')) +
  coord_flip()

But fairly dependent on your data...

2012-04-04 19:45
by Justin
Wow, this is exactly what I am looking for. I'm still having trouble melting my data into the correct data structure for your example. How can I melt my data like dat.melt? I tried melt(id, start, end), but that didn't work. I've edited my question to show the structure of my data. Thanks - drbunsen 2012-04-04 20:08
melt(yourdata, id.vars='id') will give you a structure like what I have. You can also replace the first color with NA so that only the shaded section in your picture is colored - Justin 2012-04-04 20:14
Awesome, thanks so much. This was exactly what I was looking for - drbunsen 2012-04-04 21:12
to reproduce the figure in the original post, use: dat <- data.frame(lets=1:5, low=c(20,60,10,80,30), mid=c(15,15,20,10,10), high=c(65,25,70,10,60))David LeBauer 2012-04-04 22:08
Ads