7

I am trying to make a better version of an R base plot with ggplot2. Not only to have a common legend but also because I like the ggplot2 styles and customization. My data consists of 3 seperate datasets that contain the same two groups of observations for several (but different) treatments. Hence I want to generate 3 separate plots in 1 graph with a common legend however with different factor levels. To illustrate my point the first image here is what I have generated with R base so far: r base plot closest plot realisation

I tried to generate a ggplot2 plot with dummy data that has exactly the same structure as my data:

foo<-data.frame(c(letters,letters),c(rep('T1',26),rep('T2',26)),
runif(52),rep(c(rep('Ori1',12),rep('Ori2',8),rep('ori3',6)),2))
names(foo)<-c('Treatment','Type','Count','Origin')

a<-ggplot(foo,aes(x = factor(Treatment),y = Count))
a+ facet_grid(Origin~., scales="free_y", space="free") + 
geom_bar(stat="identity",aes(fill=factor(foo$Type)),position="dodge")
+theme_bw()+theme(axis.text.x=element_text(angle=60,hjust=1))+coord_flip()

Which gives me the following undesirable result. failed ggplot2 image

I am aware of the stack overflow topics Removing Unused Factors from a Facet in ggplot2 and How can I remove empty factors from ggplot2 facets? however, they do not deal with the clustered bar graphs I try to realise here and I feel they are the problem, however do not now how to solve it. All pointers are welcome.

Community
  • 1
  • 1
FM Kerckhof
  • 1,270
  • 1
  • 14
  • 31
  • 3
    I believe that until this is fixed (it's complicated), you are stuck with omitting `coord_flip` and using `facet_wrap(~Origin, scales="free_x")` instead. – joran Apr 08 '13 at 19:54
  • @joran Thank you for the comment but it doesn't solve the issue... the `coord_flip` doesn't really contribute to the problem here, and I just included it because I like the plot to be formatted this way. The main issue is how to preserve the `position="dodge"` throughout the facets. Maybe I should make clear that I don't really want to _reproduce_ the base graph as such, I just want the essence of the plots to be usable in a ggplot2 style. – FM Kerckhof Apr 08 '13 at 20:03
  • 2
    I don't think you actually tried what I proposed. The point is that at the moment `coord_flip` doesn't always play nicely with `scales = "free"`. This is a known issue. – joran Apr 08 '13 at 20:08
  • @joran you are right, I just removed coord_flip from the original code but did not change facet_wrap(Origin~., scales="free_x") to facet_wrap(~Origin, scales="free_x") while I did change the "free_y" to "free_x" I forgot to correct the formula. Thanks for the help! – FM Kerckhof Apr 08 '13 at 20:40

1 Answers1

9

To illustrate my comment:

a<-ggplot(foo,aes(x = factor(Treatment),y = Count))
a+ facet_wrap(~Origin, scales="free_x") + 
    geom_bar(stat="identity",aes(fill=factor(Type)),position="dodge") + 
    theme_bw() + 
    theme(axis.text.x=element_text(angle=60,hjust=1))

enter image description here

Note that if you add coord_flip and switch to free_y you get a specific error about coord_flip not working with some types of free scales, which is the source of you problem.

joran
  • 169,992
  • 32
  • 429
  • 468
  • 1
    Why is there a `foo$` in `fill=factor(foo$Type)`. It's not necessary right? It actually changes the plot quite a bit from what I was thinking: `ggplot(data = foo, aes(x=factor(Treatment), y=Count)) + geom_bar(stat="identity", aes(fill=factor(Type)), position="dodge") + theme_bw() + facet_grid(~ Origin, scales="free", space="free")` – Arun Apr 08 '13 at 20:17
  • 1
    @Arun You're right, I was just copy+pasting their code, and was focused on dropping the unused factors. – joran Apr 08 '13 at 20:21
  • thank you both! Apparently Origin~. in the original facet.wrap did not generate what I wanted, but I also got an error with just using Type in stead of foo$Type (should be the same, right?). @Arun: your comment gave me exactly what I wanted. @joran: thanks for pinpointing this common error with `coord_flip`... guess I'll stick to the normal stacked barplot then. – FM Kerckhof Apr 08 '13 at 20:42
  • @joran is there anyway to keep the size of the bars constant when one of your facets only has one factor? I have two facets with the same number of factors and a third facet with only one factor which turns out with a fat bargraph. – Herman Toothrot Apr 11 '18 at 12:54