Skip to content
Prev 386611 / 398502 Next

space between bars of a group in ggplot2

Hi,

Keep your emails on the list, please.

here are some artificial data
dput(dat)
structure(list(x = c("a", "a", "a", "b", "b", "b"), y = c(1,
1, 1, 3, 3, 3), cond = c("x1", "x2", "x3", "y1", "y2", "y3")), row.names = 
c(NA,
6L), class = "data.frame")

And possible result
hist <- ggplot(dat, aes(x=x, y=y, fill=cond, stat="identity"))
hist + geom_col(width=0.4, position = position_dodge(width=0.5))

You could change colour scheme
https://stackoverflow.com/questions/18229835/how-do-i-manually-set-geom-bar-fill-color-in-ggplot

hist + geom_col(width=0.4, position = position_dodge(width=0.5))+
scale_fill_manual(values=rep(c("red", "blue"), each=3))

Cheers
Petr


From: azam jaafari <azamjaafari at yahoo.com>
Sent: Wednesday, December 16, 2020 10:57 AM
To: PIKAL Petr <petr.pikal at precheza.cz>
Subject: Re: [R] space between bars of a group in ggplot2

Hi Petr

Thank you for reply. I used this link 
(https://stackoverflow.com/questions/6085238/adding-space-between-bars-in-ggplot2) 
for separating bars, but it doesnt work for my purpose. since I have same 
levels for one group, with this code, the bars are connected together: as a 
bar for one group. While I want to add some space between three bars belong to 
one group.

I really appreciate if you can help me

Sincerely
Azam



On Tuesday, December 15, 2020, 4:11:16 PM GMT+3:30, PIKAL Petr
<petr.pikal at precheza.cz> wrote:
Hi

Your code is not reproducible, however you could follow this advice.

https://stackoverflow.com/questions/6085238/adding-space-between-bars-in-ggplot2

Cheers
Petr
Adding space between bars in ggplot2
I'd like to add spaces between bars in ggplot2. This page offers one solution: 
http://www.streamreader.org/stats...