16

I have a table which I would like to make into a plot using ggplot2 and I've been unsuccessful so far. I have prepared a simplified table that looks like this

df1<-data.frame(Loc=c(rep("L1",5),rep("L2",3),rep("L3",4)),
Type=c(rep("T1",3),rep("T2",2),"T1","T2","T2","T1","T1","T2","T2"),
       y2009=rep("A",12),y2010=c("A","B","A","A","A","A","B","B","A","A","B","B"),
       y2011=c("B","B","B","A","B",rep("B",4),"A","B","B"))
df1

Loc has 3 locations.Each location has 2 types of samples T1 or T2. They start in 2009 as A and over time some becomes B. So, by 2011, there are lots of B.

This is the figure I have so far

ggplot(df1,aes(x=Type)) + geom_bar()+facet_grid(~Loc)
ggplot(df1,aes(x=y2009,fill=Type)) + geom_bar(position="dodge")+facet_grid(~Loc)

enter image description here enter image description here

I am not quite sure how to get counts from three factors.

I would like a figure similar to below which I roughly drafted in paint. The facets are locations and I have made the bars only for Loc1 as example. enter image description here

mindlessgreen
  • 11,059
  • 16
  • 68
  • 113
  • This will help you make a start http://www.cookbook-r.com/Graphs/, and you might be interested in stacked barplots and facets in particular/ – Matt Weller Jan 06 '14 at 15:55
  • you are almost there, look into `melt` from `reshape2` – Ananta Jan 06 '14 at 16:11
  • hmmm.. all columns in the table are factors. Does melt do anything to factors? I thought it only did something if a column with numbers or integers are present. – mindlessgreen Jan 06 '14 at 16:14
  • melt is specifically to add one more column of factors, and removing those columns – Ananta Jan 06 '14 at 16:16
  • @Ananta I may be mistaken, but I don't think this is going to be very easy. – joran Jan 06 '14 at 16:18
  • @joran doesn't `lattice` have some facilities for an x-axis hierarchy like this? – Justin Jan 06 '14 at 16:25
  • 1
    @Justin Possibly, it's been so long since I've delved into lattice. Henrik showed one route (though there are still some issues with it) and [this](http://stackoverflow.com/a/11620735/324364) might have some other general ideas. – joran Jan 06 '14 at 16:29
  • @joran, yeah, but have to use var, value interaction and might need to add one level 2009.B manually, as there is no such combination, if he wants to show A and B for each year, let's see where he goes – Ananta Jan 06 '14 at 16:29
  • @Ananta I agree with the general strategy, just think it's going to be a little work. [This](http://stackoverflow.com/q/16279295/324364) will also probably be useful to get the axis labels right. – joran Jan 06 '14 at 16:32

1 Answers1

18

Try multi-level facets:

df2 <- melt(df1, id.vars=c("Loc", "Type"))
ggplot(data=df2, aes(x=value, fill=Type)) + 
  geom_bar() + facet_wrap(~ Loc + variable, nrow=1)

enter image description here

Or alternatively, facet_grid, which I think looks better but doesn't quite match your sketch:

df2 <- melt(df1, id.vars=c("Loc", "Type"))
ggplot(data=df2, aes(x=value, fill=Type)) + 
  geom_bar() + facet_grid(Loc ~ variable)

enter image description here

Finally, borrowing from this post, you could try to better distinguish the locations by color (clearly color scheme could use some work, but you get the point):

df2 <- melt(df1, id.vars=c("Loc", "Type"))
ggplot(data=df2, aes(x=value, fill=Type)) + 
  geom_rect(aes(fill=Loc),xmin =-Inf,xmax=Inf,ymin=-Inf,ymax=Inf,alpha = 0.1) +
  geom_bar() +
  facet_wrap(~ Loc + variable, nrow=1)

enter image description here

If you want to actually have separate panels for each location, I think you'll have to use generate your own grid viewports and grobs. There was a package ggextra that did stuff like this, but it doesn't seem to be available for the most recent R versions.

Community
  • 1
  • 1
BrodieG
  • 51,669
  • 9
  • 93
  • 146