0

below a reproducible example of my grouped bar plot. I am struggling with the space / padding between the groups. I already found some question on stackoverflow like this and here.
I also played around with the geom_bar(position= .. argument to be either "dodge" or dodge_position(0.5) and also with the width of my bars. However I dont want to increase the width of my bars anyfurther. Is there any solution like changing the size of the discrete x axis to reduce the labeled spaces in figure 1? Update: Reduced the code to make it more minimal for the minimal reproducible example. The Problem remains the same

library(ggplot2)
library(ggthemes)
library(dplyr)

algorithm <- c(rep("0_DT",2),rep("1_RF",2),rep("2_MLP",2))
target <- rep(c("Some Data","Some Other Data"),3)
value <- runif(6,85,95) # Simulated Accuracies
CI_lower  <- value - 5
CI_upper  <- value + 5
data <- data.frame(target,algorithm,value,CI_lower,CI_upper)
ggplot(data, aes(fill=algorithm, y=value, x=target)) +
  geom_bar(position=position_dodge(0.75), stat="identity",  width = 0.65)+ theme_classic()+
  scale_fill_manual("Algorithm",
                    values = alpha(c("0_DT" = "#20639B", "1_RF" = "#3CAEA3", "2_MLP" = "#F6D55C"),0.8),
                    labels=c("DT","RF","MLP"))+
  scale_y_continuous("Accuracy in %",limits = c(0,100),oob = rescale_none,
                     # breaks= sort(c(seq(0,90,10),h)),
                     breaks= seq(0,100,10),
                     expand = c(0,0)) 

And here is my Barchart Figure 1

enter image description here

Björn
  • 1,610
  • 2
  • 17
  • 37
  • Have you tried `position_dodge` or `position_dodge2` arguments? `preserve` and `padding` might help – Peter Apr 21 '20 at 10:25
  • Hi thanks for your comment. I just ried different combinations of `position=position_dodge2(0.4,preserve="single",padding=.05)` changing padding to 0, however the distance between the bars and the y axis and between the two groups of bars remains allways the same. – Björn Apr 21 '20 at 10:42
  • Found something to reduce the space to the left (y-Axis) and right with `scale_x_discrete(expand=c(0.3,0.1))` [here](https://ggplot2.tidyverse.org/reference/scale_discrete.html). However space between groups of bars is still a bit high – Björn Apr 21 '20 at 10:48

1 Answers1

2

I may be misunderstanding you, but you should be able to get the result you want by tweaking the position argument:

ggplot(data, aes(fill = algorithm, y = value, x = target)) +
  geom_bar(position = position_dodge(1), stat = "identity",  width = 0.65) + 
  theme_classic() +
  scale_fill_manual("Algorithm", 
                    values = alpha(c("0_DT"  = "#20639B", 
                                     "1_RF"  = "#3CAEA3", 
                                     "2_MLP" = "#F6D55C"), 0.8),
                    labels = c("DT", "RF", "MLP")) +
  scale_y_continuous("Accuracy in %",limits = c(0, 100), oob = rescale_none,
                     breaks= seq(0, 100, 10),
                     expand = c(0, 0)) 

enter image description here

If you want to keep the apparent width of the bars but also reduce the spaces between bars, then you will have to increase the width but change the shape of the over all plot by changing the window dimensions or the plot margins:

ggplot(data, aes(fill = algorithm, y = value, x = target)) +
  geom_bar(position = position_dodge(1), stat = "identity",  width = 0.8) + 
  theme_classic() +
  scale_fill_manual("Algorithm", 
                    values = alpha(c("0_DT"  = "#20639B", 
                                     "1_RF"  = "#3CAEA3", 
                                     "2_MLP" = "#F6D55C"), 0.8),
                    labels = c("DT", "RF", "MLP")) +
  scale_y_continuous("Accuracy in %",limits = c(0, 100), oob = rescale_none,
                     breaks= seq(0, 100, 10),
                     expand = c(0, 0)) +
 theme(plot.margin = unit(c(10, 50, 10, 50), "points"))

enter image description here

This may not be exactly what you want, but some combination of position_dodge, width and plot.margin will get you the spacing you are looking for.


EDIT

Following the OP's comments, it looks like the final solution should looks something like this, which is done with width = 0.9, position_dodge(0.9) and plot.margin = unit(c(10, 50, 10, 50), "points")):

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • Hi thank you for your answer (upvoted). I think I am satisfied now. Either with your solution or with the scale_x_discrete expand version. The second version of the figure you posted comes already pretty close to what I inteded. However the space between the three algorithms in one group should be zero. So all 3 bars for `Some Data` should have 0 distance to each other, then a small(er) gap and then the three Algorithms for `Some Other Data`. – Björn Apr 21 '20 at 11:57
  • Perfect. Thank you very much – Björn Apr 21 '20 at 15:09