0
votes

this is my first question here, i try to describe my problem the best possible. (i am sorry, if i am not successful with that)

i want to make a barplot in ggplot while using facet_grid.

plot1<-ggplot(data2,aes(x=fc4mean,y=Fraktion,fill=methode))+
  facet_grid(~Standort, scales="free_y",)+
    geom_barh(stat="identity",aes(fill=methode), position=position_dodge2(),width = 0.3)+
  theme_bw()+
  theme(axis.text.x = element_text(hjust = 1))+
  scale_fill_brewer(palette = "Greys",direction=-1)+
  scale_color_brewer("Black")+
  theme(legend.title=element_blank())+
  ylab("fraction")+
  guides(colour = guide_legend(reverse=T))+
  geom_errorbar(aes(xmin=fc4mean-fc4sd, xmax=fc4mean+fc4sd),position=position_dodge2(),width=0.3)

it works, but the standard settings make the bars huge and ugly. so i decided to set bar widths to 0.3. the problem is that then the groups of bars are far away from each other. so i tried to reduce the spacing. enter image description hereenter image description here

i tried a lot of different approaches, for example workarounds with

position=position_dodge()

or

scale_y_discrete(expand = c(0, 2))+

to remove spacing between the 2 factor ticks on the y-axis. this worked but then i have massive empty space at the top and the bottom of the panels. enter image description here

i tried to condense the plot with

theme(plot.margin = unit(c(5,0,5,0), "cm"))

which did not help at all. as i am not willing to give up and merge the plot in paint, i hope somebody has a nice idea how to deal with the width of the bars, the spacing between the bars, and the spacing of the panels in facet grid.

hoping for some nice input, cheers :)

1
Isn't this simply an aspect ratio issue? If you resize the window in which your plot appears, can you get a decent looking plot?teunbrand
hello teunbrand, thank you for your comment. i can do that. but then the plot will have very "interesting" layout (very flat and long), so i thought it would be better the get rid of the unneccessary spacings within facet panels.Hans Schmidt
Two points: (1) I think it is more of an aspect ratio issue here, and (2) check the syntax of your expand= call. You should adjust to scale_y_discrete(expand=expansion(c(0,2))) and you'll see the expansion take effect. Play around with that. I think what you're looking for is taking the original widths (where your bars are close together for "fraction") and then ADDING to the y axis via expansion on both sides. It will create whitespace above and below the bars (so it's not flat and long), but keep the bars together. Try fiddling in that way and then playing with your aspect window.chemdork123
hello chemdork123, you were both right, it took me a while to play around, but in the end, the aspect ratio in combination with all other tools to adjust the bars and the window was the key. thank you a lot.Hans Schmidt
No problem. If you solved the problem, it's best to post the answer and "accept" it yourself so that others can be helped in the future and the site does not get cluttered with unanswerd (but really answered) questions.chemdork123

1 Answers

0
votes

with the help of your comments, i was able to create a decent plot by using the commands

scale_y_discrete(expand=expansion(c(0,2)))
position=position_dodge()
width = ()

and by adjusting the aspect ratio of the plot windows. thank you