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())
Adjusting distance between groups of bars in ggplot2
I think this is easiest to achieve if you use x = Time
and fill = Group
. Something like:
dodge <- position_dodge(width = 0.5)
ggplot(df, aes(x = Time, y = mean, fill = Group)) +
geom_bar(width = 0.5, stat="identity", position = dodge) +
geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd),
position = dodge, size = 1, shape = 1, width = 0.2) +
scale_fill_manual(values = c("#333333", "#FF0000", "#0000FF")) +
theme_bw() +
theme(panel.grid.major = element_blank())
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)
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)
Adding space between bars in ggplot2
Is this what you want?
hist + geom_bar(width=0.4, position = position_dodge(width=0.5))
width
ingeom_bar
determines the width of the bar.width
inposition_dodge
determines the position of each bar.
Probably you can easily understand their behavior after you play with them for a while.
ggplot2 : How to reduce the width AND the space between bars with geom_bar
I would adjust the plot's aspect ratio, and have ggplot
automatically assign the right width for the bars and the gap between them:
ggplot(iris, aes(Species, Petal.Length)) +
geom_bar(stat="summary", width=0.4) +
theme(aspect.ratio = 2/1)
Produces this:
Related Topics
Create All Possible Combiations of 0,1, or 2 "1"S of a Binary Vector of Length N
Generating a Very Large Matrix of String Combinations Using Combn() and Bigmemory Package
Add Missing Value in Column with Value from Row Above
Format Latitude and Longitude Axis Labels in Ggplot
R: Save Multiple Plots from a File List into a Single File (Png or PDF or Other Format)
Raster Image Goes Below Base Layer, While Markers Stay Above: Xindex Is Ignored
How to Jitter Two Ggplot Geoms in the Same Way
Change Paper Size and Orientation in an Rmarkdown PDF
Merging Data Frames with Different Number of Rows and Different Columns
Use a Custom Icon in Plotly's Pie Chart in R
Add Na Value to Ggplot Legend for Continuous Data Map
Dplyr Filter() with SQL-Like %Wildcard%
Understanding Dates/Times (Posixc and Posixct) in R