Adding Some Space Between the X-Axis and the Bars, in Ggplot

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

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)

How to increase the space between grouped bars in ggplot2?

What's about?
1. Use geom_col instead of geom_bar as recommended.
2. specify suitable position_dodge(0.5) and width=0.5 and 3. remove unnecessary code.

ggplot(d, aes(x=Measure, y=mean, fill=Condition)) + 
geom_col(colour="black",width=0.5,
position=position_dodge(0.5)) +
geom_errorbar(aes(ymin=mean-se, ymax=mean+se),
position=position_dodge(0.5), width=.25)+
scale_x_discrete(limits = c("Interest", "Value","Effort","Difficulty","Competence","JOL")) +
scale_y_continuous(breaks=seq(0,7,by =1),limits = c(0,7), expand = c(0,0))+
scale_fill_manual(values=c("#ffcc00ff","#ffffff"), name = "Condition") +
labs(x="", y = "Rating (0-7)")+
theme_minimal() +
theme(axis.line = element_line(color="black"),
axis.ticks = element_line(color="black"),
panel.border = element_blank())

Sample Image

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

Adding space between bars in ggplot2

Is this what you want?

hist + geom_bar(width=0.4, position = position_dodge(width=0.5))
  • width in geom_bar determines the width of the bar.
  • width in position_dodge determines the position of each bar.

Probably you can easily understand their behavior after you play with them for a while.

Sample Image

Spacing between x-axis groups bigger than within group spacing geom_col

Try using a combination of position=position_dodge(width=...) and width=...

For example:

ggplot(df, aes(fill = cyl, x = am, y = mpg)) +
geom_col(position = position_dodge(width = 0.9), width=0.8) +
theme_bw()

The width() command gives the displayed width of individual bars, while the position(width=) gives the space that is reserved for the bars.

The difference between the two values gives the space between bars within a group, while 1 - position_dodge(width=) gives the space between the groups.

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:

Sample Image

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

Sample Image

Increase space between bars in ggplot

You can adjust the width outside of the position_dodge as well (in geom_bar),

ggplot(data=df, aes(x=Gene, y=FC, fill=expt, group=expt)) +
geom_bar(colour="black", stat="identity", position = position_dodge(width = 0.8), width=0.5) +
geom_errorbar(aes(ymax = FC + se, ymin = FC, group=expt),
position = position_dodge(width = 0.8), width = 0.25)

Sample Image

or

dodge <- position_dodge(width = 0.5)

ggplot(data=df, aes(x=Gene, y=FC, fill=expt, group=expt)) +
geom_bar(colour="black", stat="identity", position=dodge, width=0.5) +
geom_errorbar(aes(ymax = FC + se, ymin = FC, group=expt),
position = dodge, width = 0.25)

Sample Image



Related Topics



Leave a reply



Submit