Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 bar plot, no space between bottom of geom and x axis keep space above

Tags:

r

ggplot2

When I plot a bar graph in ggplot2 I would like to reduce the space between the bottom of the bars and the x-axis to 0, yet keep the space above the bars and the plot box. I have a hack to do it below. It's dirty and I want to be clean again. Is there a way to achieve this behavior without the dirty little hack?

Default (desired space above but don't want space below bars):

ggplot(mtcars, aes(x=as.factor(carb))) + 
    geom_bar()

enter image description here

Use expand (undesired 0 space above but got the 0 space below bars):

ggplot(mtcars, aes(x=as.factor(carb))) + 
    geom_bar() + 
    scale_y_continuous(expand = c(0,0)) 

enter image description here

Dirty Hack (I like it but its.. well, dirty):

ggplot(mtcars, aes(x=as.factor(carb))) + 
    geom_bar() + 
    scale_y_continuous(expand = c(0,0)) +
    geom_text(aes(x=1, y=10.3, label="Stretch it"), vjust=-1)

enter image description here

like image 617
Tyler Rinker Avatar asked Nov 26 '13 14:11

Tyler Rinker


People also ask

How do I increase the space between grouped bars in ggplot2?

For grouped bars, there is no space between bars within each group by default. However, you can add some space between bars within a group, by making the width smaller and setting the value for position_dodge to be larger than width.

How do I add a space between bars in R?

To set space between bars in Bar Plot drawn using barplot() function, pass the required spacing value for space parameter in the function call. space parameter is optional and can accept a single value or a vector to set different space between bars in the bar plot.

What is Geom_col Ggplot?

There are two types of bar charts: geom_bar() and geom_col() . geom_bar() makes the height of the bar proportional to the number of cases in each group (or if the weight aesthetic is supplied, the sum of the weights). If you want the heights of the bars to represent values in the data, use geom_col() instead.

How do you control which layers come closer to the top of your plot and which closer to the bottom in ggplot2?

How do you control which layers come closer to the top of your plot and which - closer to the bottom in GGPlot2? You control this by rearranging the order in which you add the layers to your plot in your code. How many variables (columns of a data frame) are required to create a histogram?


3 Answers

I might be missing what you really want, but without using geom_text hack you can still set the limits

ggplot(mtcars, aes(x = as.factor(carb))) + 
    geom_bar() + 
    scale_y_continuous(expand = c(0, 0), limits = c(0, 10.3)) 

# marginally cleaner
like image 141
user1317221_G Avatar answered Oct 20 '22 12:10

user1317221_G


The R documentation includes a new convenience function called expansion for the expand argument as the expand_scale() became deprecated as of ggplot2 v3.3.0 release.

ggplot(mtcars) +
  geom_bar(aes(x = factor(carb))) + 
  scale_y_continuous(expand = expansion(mult = c(0, .1)))
like image 33
bee5911 Avatar answered Oct 20 '22 12:10

bee5911


You can expand the limits manually, e.g. with expand_limits(y=10.1), or use this trick to add an invisible layer with scaled up data,

ggplot(mtcars, aes(x=as.factor(carb))) + 
    geom_bar() + 
    scale_y_continuous(expand = c(0,0)) +
    geom_blank(aes(y=1.1*..count..), stat="bin")
like image 19
baptiste Avatar answered Oct 20 '22 10:10

baptiste