-1

I tried to create a multiple grouped bar chart but I haven't figured out how to get the right layout. I have created one which shows at least the right thing but the bars are vertical instead of horizontal.

Code:

sex <- c('F', 'F', 'F', 'F', 'F','M','M','M','M')
type <-c('XL1','XL1','XL1','XL2','XL2','XL2','XL2','XL2','XL2')
ID <- c(1,2,3,4,5,6,7,8,9)
smoke <- c('yes','yes','no','yes','no','yes','no','no','yes')
improved <- c(12,9.8,13,4,7,9,6.5,12.2,-1) 
dat <- as.data.frame(cbind(sex,type,ID,smoke,improved)) 

base <- ggplot(dat, aes(fill = interaction(sex,type,smoke))) +
        geom_bar(position = 'dodge',stat='identity') 
base + aes(x = factor(ID), y=improved) + 
       facet_grid(.~type+sex, scales = "free_x", space = "free")

The problem is that I have another grouping variable except type and sex and all three have in the real dataset long labels. I could rotate them by 90 degrees but I prefer to rotate the whole graph by 90 degrees and flip_coord() doesn't work... The solution mentioned at the end of this thread isn't really nice. Also because the grouping variables are on the right and not on the left.

I'm grateful for any advice. Thanks,

Anja

Community
  • 1
  • 1

1 Answers1

0

I am confused why coord_flip() won't work?

base <- ggplot(dat, aes(fill = interaction(sex,type,smoke))) + geom_bar(position = 'dodge',stat='identity') 
base <- base + aes(x = factor(ID), y=improved) +  facet_grid(.~type+sex, scales = "free_x", space = "free")
base <- base + coord_flip()

You can adjust the legend position by using the following:

base <- base + theme(legend.position = "bottom")

Which gives: rotated

Or maybe something like this would be more appropriate, two columns for the legend:

base <- base + theme(legend.position = "bottom") + guides(fill = guide_legend(ncol=2))

Which gives: rotated_col_legend

nathaneastwood
  • 3,664
  • 24
  • 41
  • Maybe I forgot to mention that coord_flip() works but I can't figure out how to plot the pictures below each other. So I would need a option as par(mfrow=c(3,1)). Because I would have at least 15 charts side by side and I want to compare the height of the charts easily. And the width of the charts should be the same then. – Anja Bertsche Sep 09 '14 at 08:00
  • Can you not use `facet_wrap` then? `facet_wrap(~type+sex, ncol = 1, nrow = 3)` – nathaneastwood Sep 09 '14 at 08:23
  • Then I got the error message: "Error in layout_base(data, vars, drop = drop) : At least one layer must contain all variables used for facetting" – Anja Bertsche Sep 09 '14 at 08:28
  • Hmm, this worked for me: `base <- ggplot(dat, aes(fill = interaction(sex,type,smoke))) + geom_bar(position = 'dodge',stat='identity')` `base <- base + aes(x = factor(ID), y=improved) + facet_wrap(~type+sex, ncol = 1, nrow = 3)` `base <- base + coord_flip()` `base` – nathaneastwood Sep 09 '14 at 08:47
  • Yes, but the option with free scale for ID not, or? So there are all 9 persons listed and just a few have bars. That is also not really nice. Could it really be, that there is no option? Or is there maybe a way with the data set in "long" format? – Anja Bertsche Sep 09 '14 at 11:06
  • I don't think you can use free scales with `coord_flip()`. I just get an error message. `Error in facet_render.wrap(plot$facet, panel, plot$coordinates, plot_theme(plot), : ggplot2 does not currently support free scales with a non-cartesian coord or coord_flip.` Sorry I can't be a better help. I only started learning ggplot last week. – nathaneastwood Sep 09 '14 at 12:15