4

I try to create a barplot, that uses besides and stacked at the same time. I have something that is similar to what I want with facet:

Similar to the searched feature

tmp <- morley
tmp$loc <- paste("No", tmp$Run %/% 2, sep="")
tmp$group <- as.logical(tmp$Run %% 2)
tmp$year <- tmp$Expt + 2000
tmp$value <- tmp$Speed
tmp <- subset(tmp, loc %in% c("No1", "No2", "No3"))

ggplot(tmp, aes(x=loc, y=value, fill=group)) + 
  geom_bar(stat ='identity', position ='stack') + 
  facet_grid (~year)

I would want that without facets and 2 legends (Green: No1, Red: No2, Blue: No3 and TRUE: 0% transparency, FALSE 40% transparency) with the years on the x-axis, the locations beside and the groups stacked. Also a legend with 6 entries No1 true, No1 false, No2 true... would be okay.

Is there a way to do this?

eipi10
  • 91,525
  • 24
  • 209
  • 285
Frank
  • 51
  • 4
  • I think the faceting is likely a reasonable approach here. You can move the year labels to the bottom axis to get more of the look you want. See, e.g., [this answer](https://stackoverflow.com/questions/18165863/multirow-axis-labels-with-nested-grouping-variables/36337286#36337286) – aosmith Nov 07 '17 at 16:35
  • Sounds like you want to map `fill` to `loc` instead of `group` and map `alpha` to `group` to complete the look you want. You can set the desired fill colors and transparency via the appropriate `scale_*` functions for `alpha` and `fill`. – aosmith Nov 07 '17 at 16:38

1 Answers1

3
ggplot(tmp, aes(x=loc, y=value, fill=loc, alpha=group)) + 
  geom_bar(stat ='identity', position ='stack') +
  scale_alpha_manual(values=c('TRUE'=1, 'FALSE'=0.6)) +
  facet_grid(~year, switch='x') +
  theme(axis.title.x=element_blank(), strip.placement='outside')

And then adjust themes and fill scale appropriately.

enter image description here

MrGumble
  • 5,631
  • 1
  • 18
  • 33
  • Wow that was fast and alpha=group is exactly what I searched for. The problem is, that I made my example too small and simple. (Sorry sent too soon with enter, it is my first question on stackoverflow) With more years and locations I need break the facets so that only every 4th or something like that has a name. The following would be too big tmp <- morley tmp$loc <- paste("No", tmp$Expt, sep="") tmp$group <- as.logical(tmp$Run %% 2) tmp$value <- tmp$Speed tmp2 <- tmp tmp$year <- tmp$Run + 2000 tmp2$year <- 2021 - tmp$Run tmp <- rbind(tmp,tmp2) – Frank Nov 07 '17 at 17:11
  • If your example was too small and simple, post a new question with the more advanced stuff, once you've worked on it a bit. Incidentally, is this some devilish attempt to achieve a 3D glare look on your bars? – MrGumble Nov 07 '17 at 17:13
  • Dear MrGumble, thank you. No 3D stuff. I just have several locations (3-5) with 60 timeslots (5 years * 12 month) and now I need to distinguish between adult and child. I hope noone needs female/male in the same diagram ;). I will play a bit and try if it works this way. If not I will post a better question. – Frank Nov 07 '17 at 17:25