Ggplot2 Bar Plot, No Space Between Bottom of Geom and X Axis Keep Space Above

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

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)))

Remove space between plotted data and the axes

Update: See @divibisan's answer for further possibilities in the latest versions of ggplot2.


From ?scale_x_continuous about the expand-argument:

Vector of range expansion constants used to add some padding around
the data, to ensure that they are placed some distance away from the
axes. The defaults are to expand the scale by 5% on each side for
continuous variables, and by 0.6 units on each side for discrete
variables.

The problem is thus solved by adding expand = c(0,0) to scale_x_continuous and scale_y_continuous. This also removes the need for adding the panel.margin parameter.

The code:

ggplot(data = uniq) + 
geom_area(aes(x = year, y = uniq.p, fill = uniq.loc), stat = "identity", position = "stack") +
scale_x_continuous(limits = c(1986,2014), expand = c(0, 0)) +
scale_y_continuous(limits = c(0,101), expand = c(0, 0)) +
theme_bw() +
theme(panel.grid = element_blank(),
panel.border = element_blank())

The result:
plot area chart with no gaps

Space between bar chart with day/week/month in x axis in R

You could add colour = "white" for the border around the bars:

library(tidyverse)

df <- tibble(x = rep(1:100, 5))

df |>
ggplot(aes(x)) +
geom_histogram()
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Sample Image

df |> 
ggplot(aes(x)) +
geom_histogram(colour = "white")
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Sample Image

Created on 2022-05-17 by the reprex package (v2.0.1)

Adding some space between the x-axis and the bars, in ggplot

You could use the limits in scale_y_continuous to indicate what range you want to show. NA in the limits is 'use the default'. Now, normally that would take the away both bars because they are out-of-bounds (oob), but you could fix that by setting the oob argument of the scales to squish, which is a function found in the scales package.

library(scales)

ggplot(df, aes(x,y)) +
geom_col() +
scale_y_continuous(limits = c(1, NA), oob = squish)

Sample Image

How to increase the space between the bars in a bar plot in ggplot2?

You can always play with the width parameter, as shown below:

df <- data.frame(x=factor(LETTERS[1:4]), y=sample(1:100, 4))
library(ggplot2)
ggplot(data=df, aes(x=x, y=y, width=.5)) +
geom_bar(stat="identity", position="identity") +
opts(title="width = .5") + labs(x="", y="") +
theme_bw()

Compare with the following other settings for width:

alt text

So far, so good. Now, suppose we have two factors. In case you would like to play with evenly spaced juxtaposed bars (like when using space together with beside=TRUE in barplot()), it's not so easy using geom_bar(position="dodge"): you can change bar width, but not add space in between adjacent bars (and I didn't find a convenient solution on Google). I ended up with something like that:

df <- data.frame(g=gl(2, 1, labels=letters[1:2]), y=sample(1:100, 4))
x.seq <- c(1,2,4,5)
ggplot(data=transform(df, x=x.seq), aes(x=x, y=y, width=.85)) +
geom_bar(stat="identity", aes(fill=g)) + labs(x="", y="") +
scale_x_discrete(breaks = NA) +
geom_text(aes(x=c(sum(x.seq[1:2])/2, sum(x.seq[3:4])/2), y=0,
label=c("X","Y")), vjust=1.2, size=8)

The vector used for the $x$-axis is "injected" in the data.frame, so that so you change the outer spacing if you want, while width allows to control for inner spacing. Labels for the $x$-axis might be enhanced by using scale_x_discrete().

alt text

How to remove space between axis & area-plot in ggplot2 with date

You have to specify expand = c(0,0) in scale_x_date because your x-axis is not in a continuous (numeric) format but a date format:

ggplot(data = MWE, 
aes(x = Periode, y = VValue , color)) +
geom_line(color = "Blue", size = 1) +
scale_x_date(limits = c(as.Date("2012-01-01"), NA), expand = c(0,0))

Sample Image

R geom_tile on theme_void has blank space above and below plot

You could try to specify axis limits and suppress automatic axis expansion. Setting axis limits is more straightforward for factors: if needed, convert numeric tile coordinates to character (as.character) or factor (as.factor). Also remove the legend.

Example of stripped heatmap:

data.frame(
x = c('col_1','col_2','col_1','col_2'),
y = c('row_1','row_1','row_2','row_2'),
value = runif(4)
) %>%
ggplot() +
geom_tile(aes(x, y, fill = value)) +
scale_x_discrete(limits = c('col_1','col_2'), expand = c(0,0)) +
scale_y_discrete(limits = c('row_1','row_2'), expand = c(0,0)) +
scale_fill_continuous(guide = 'none') +
theme_void()

edit: for composite charts, there's also cowplot



Related Topics



Leave a reply



Submit