0

I am designing a bar plot with ggplot2 package. The only problem is that I cannot get rid of the space between the bars and the x-axis. I know that this formula should resolve the problem:

scale_y_continuous(expand = c(0, 0)) function 

But it seems that the element for the error bar is overwriting it and gives always this space.

here my code:

p<-ggplot(data=tableaumergectrlmut, aes(x=ID, y=meanNSAFbait,  fill=Condition)) + 
geom_bar(stat="identity", position=position_dodge())+
scale_y_continuous(expand = c(0,0))+ 
geom_errorbar(aes(ymin=meanNSAFbait-SDNSAFbait, 
  ymax=meanNSAFbait+SDNSAFbait, width=0.25), position=position_dodge(.9))
Paul Z
  • 33
  • 1
  • 1
  • 6
  • Could you make a reproducible example? Probably you can demonstrate this on a built-in data set, but if not you should share your data with `dput()` or via simulation. [See here for tips on making reproducible examples](http://stackoverflow.com/q/5963269/903061). – Gregor Thomas Sep 14 '16 at 17:44
  • Also, it's nice if examples are *minimal* - helps remove things that distract from the actual problem. If the custom colors, labels, angled text, theme customizations, etc., aren't part of the problem, remove them from your question. – Gregor Thomas Sep 14 '16 at 17:45
  • By using expand = c(0,0) you set the limits of your y axis to the maximum and minimum y value in your dataset. If your errorbars are perhaps so large that they go below zero (weird but possible), the minimum will thus be this value (e.g. -1), and not zero. Perhaps try and add ,limits=c(0,0) to scale_y_continuous – Wave Sep 14 '16 at 17:47

1 Answers1

0

Using some example data to generate a plot that (I think) shows the problem you're having.

library(ggplot2)

df <- data.frame(val = c(10, 20, 100, 5), name = LETTERS[1:4])

ggplot(df, aes(x = name, y = val, fill = name)) +
  geom_bar(stat = "identity")

enter image description here

There is a gap from the zero point on the y axis (bottom of the bars) and where the x axis labels are.

You can remove this using scale_y_discrete or scale_y_continuous, depending on the nature of your data, and setting expand to c(0,0):

ggplot(df, aes(x = name, y = val, fill = name)) +
  geom_bar(stat = "identity") +
  scale_y_discrete(expand = c(0,0)) + 
  scale_x_discrete(expand = c(0,0))

This gives the plot:

enter image description here

Note I've also removed the gap along the y axis, simply remove the scale_x_discrete line to add this gap back in.

Since error bars are an issue, here are a few examples:

ggplot(df, aes(x = name, y = val, fill = name)) +
  geom_bar(stat = "identity") +
  geom_errorbar(aes(ymin = val - 10,
                    ymax = val + 10))

enter image description here

You can use scale to remove the padding down to the error bar:

ggplot(df, aes(x = name, y = val, fill = name)) +
  geom_bar(stat = "identity") +
  geom_errorbar(aes(ymin = val - 10,
                    ymax = val + 10)) +
  scale_y_continuous(expand = c(0,0))

enter image description here

Or you can use coord_cartesian to give a hard cutoff:

ggplot(df, aes(x = name, y = val, fill = name)) +
  geom_bar(stat = "identity") +
  geom_errorbar(aes(ymin = val - 10,
                    ymax = val + 10)) +
  scale_y_continuous(expand = c(0,0)) +
  coord_cartesian(ylim = c(0, max(df$val) + 10))

enter image description here

Eric Watt
  • 3,180
  • 9
  • 21
  • Yes, thank you for the plot you show as an example. I can obtain this result as well, but as soon as I add the error bar, the spacing appears again. – Paul Z Sep 14 '16 at 18:04
  • I've added some with error bars. Does this highlight (and solve) the issue you're having? – Eric Watt Sep 14 '16 at 18:10
  • Yes, this highlights completely my issue. I tried to add again scale_y_continuous(expand = c(0,0)) at the end as you did and it finally works as expected! Thank you very much. – Paul Z Sep 14 '16 at 18:17