3
df2 <- iris[c(5,1)]
df3 <- aggregate(df2$Sepal.Length, list(df2$Species), mean)
names(df3) <- c("x","y")

ggplot(df3, aes(x,y)) +
  geom_bar(aes(fill=x),stat="identity") +
  theme(axis.ticks=element_blank(), axis.text.x=element_blank())

enter image description here

I have successfully removed axis tick marks and labels from this plot. I am trying to get rid of the blank grey space underneath the bars. Zero should be the lower bound for the chart. I've been searching unsuccessfully for an adjustment function to either pull the bars down or to cut off the bottom grey portion. The hope is that ggplots are not hardwired with the extra space underneath.

Pierre L
  • 28,203
  • 6
  • 47
  • 69
  • 2
    Look at [this post](http://stackoverflow.com/questions/5768107/losing-the-grey-margin-padding-in-a-ggplot), the solution might work for you. (you probably just need `print(p + scale_y_continuous(expand = c(0, 0)))`) – NicE Aug 20 '15 at 13:48

1 Answers1

2

Add the following elements to your code (inspired by this Q & A):

theme_classic()
scale_x_discrete(expand=c(0,0))
scale_y_continuous(expand=c(0,0))

Instead of theme_classic(), you can also use theme_bw() which will add horizontal and vertical lines to the plot.

You code should then look like this:

ggplot(df3, aes(x,y)) +
  geom_bar(aes(fill=x),stat="identity") +
  scale_x_discrete(expand=c(0,0)) +
  scale_y_continuous(expand=c(0,0)) +
  theme_classic() +
  theme(axis.ticks=element_blank(), axis.text.x=element_blank())

this gives:

enter image description here

Community
  • 1
  • 1
Jaap
  • 81,064
  • 34
  • 182
  • 193
  • Is there a way to separate the left bat from the axis? I changed the ```scale_x_discrete(expand=c(0,0))``` to ```scale_x_discrete(expand=c(0.1,0))``` and it did work, but I am unsure whether this is the best way to proceed – Pablo Herreros Cantis May 13 '20 at 22:39
  • 1
    @PabloHerrerosCantis You can also achieve by setting the `width`-parameter of `geom_bar` to a value lower than `0.9`. – Jaap May 14 '20 at 18:03