3

I created a barplot using ggplot. My data frame:

variable <- c(AFD, AFD, FC, FC, NDT, NDT)
AOI<- c(R,Z,R,Z,R,Z)
mean <- c(219,228,30,130,8381,40164)
se <- c(5,5,1,4,367,1363)

df <- data.frame(variable , AOI, mean, se )

This is my code for the plot:

ggplot(data=Summary_RegBed_ZvsR_Hyp1_lang[Summary_RegBed_ZvsR_Hyp1_lang$variable == "FC", ], aes(x=variable, y=mean, fill=AOI)) +   
scale_x_discrete(labels = "Anzahl Fixationen")+
ylab("Absolute Anzahl")+
xlab("Anzahl Fixationen")+
scale_fill_grey(start = 0.35, end= 0.8,
              name = "Area of Interest",
              breaks=c("R", "Z"),
              labels=c("Rand", "Zentrum"))+
geom_bar(stat="identity", position=position_dodge(), width = .6)+
geom_errorbar(aes(ymin=mean-se, ymax=mean+se),
            width=.1,                    
            position=position_dodge(.6))+
scale_y_continuous(limit=c(0,140), breaks = c(0,25, 50, 75, 100, 125))+
theme_bw() +
theme(text=element_text(family="Times", face="bold", size=12))+
theme(axis.text.x=element_blank(),
    axis.ticks.x=element_blank()) +
geom_signif(annotation="***", y_position=140, xmin=0.85, xmax=1.15, tip_length=0.008)

This is the resulting plot This is the resulting plot

My question is: How can I reduce the space between the bars and the y-axis and the bars and the right edge of the plot? I need to plot several plots next to each other and this just wastes my space. I tried to use scale_x_discrete with breaks or limits, but none of it worked. Any suggestions? Thanks in advance!

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58
StephieMall
  • 31
  • 1
  • 4
  • They are already beside eachother how do you wanna reduce the space? – M-- Jun 26 '17 at 20:14
  • Sorry, I didn't express myself well. I don't want to reduce the space between the bars, but the space between the left bar and the y-axis and the space between the right bar and the right edge of the plot. I want to "shrink" the x-axis. – StephieMall Jun 26 '17 at 20:19
  • Please read [How to make a great reproducible example in R?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – M-- Jun 26 '17 at 20:22

1 Answers1

1

You can set the expand to zero (right and left) to achieve this:

 p +  
    scale_x_discrete( expand = c(0, 0))
M--
  • 25,431
  • 8
  • 61
  • 93
  • Thank you for your answer! But when I add the expand command to my code, it still results in the same plot as before. – StephieMall Jun 27 '17 at 09:31
  • @StephieMall provide a reproducible example as I said in the comments below your question so I can make sure that the solution works for your special case. – M-- Jun 27 '17 at 13:56