Specific spaces between bars in a barplot - ggplot2 - R
Asked Answered
S

3

6

I have a simple bargraph like the following

a<-data.frame(x=c("total","male","female","low education",
            "mid education","high education","working","not working"),
        y=c(80,30,50,20,40,20,65,35))
a$x<-as.character(a$x)
a$x<-factor(a$x,levels=unique(a$x))


ggplot(a,aes(x,y)) + 
geom_bar(stat="identity",fill="orange",width=0.4) +
coord_flip() +
theme_bw()

enter image description here Now , because the levels of the x axis (flipped and now seems like y ) have a relation with each other e.g male and female represent sex breakdown , working and not working represent another breakdown etc., I want the axis to leave some space between each breakdown in order to point out these breakdowns.

I have tried some things with scale_x_discrete and its parameter break but it seems that this is not the way it goes . Any ideas ?

Saturnalia answered 7/5, 2015 at 12:2 Comment(1)
Good answers below. One other approach is to color the bars that you want differentiated by setting up a factor for their fill color. For example, male and female would be gender in the factor, working and non-working would be employed, etc. and then fill = factor will serve to tell bar-clusters apart.Cesura
C
8

I don't know of a way to set different distances between bars in a barplot. However, you can add bars with height 0 and no label between the groups as follows:

a<-data.frame(x=c("total","a","male","female","b","low education",
                  "mid education","high education","c","working","not working"),
              y=c(80,0,30,50,0,20,40,20,0,65,35))
a$x<-factor(a$x,levels=unique(a$x))


ggplot(a,aes(x,y)) + 
   geom_bar(stat="identity",fill="orange",width=0.4) +
   coord_flip() +
   theme_bw() +
   scale_x_discrete(breaks=a$x[nchar(as.character(a$x))!=1])

Some remarks:

  • a$x is a character from the start, so there is no need to call as.character on it.
  • It only works, if the each "empty" bar has a different label. That's why I chose three different letters.
  • scale_x_discrete is used to suppress the labels and tick marks.

The result looks as follows: enter image description here

Carbonation answered 7/5, 2015 at 12:41 Comment(1)
thank you for your answer and your feedback . You way of dribbling the problem is funny and efficient.Saturnalia
C
2
a<-data.frame(x=c("total","male","female","low education","mid education","high education","working","not working"),y=c(80,30,50,20,40,20,65,35))
a$x<-as.character(a$x)
a$x<-factor(a$x,levels=unique(a$x))

a$rel = c("a", "a", "a", "b", "b", "b", "c", "c") # set groups
ggplot(a, aes(rel, y, fill = factor(x))) + 
  geom_bar(stat = "identity", width = 0.5, position = position_dodge(0.7))
Chuckle answered 7/5, 2015 at 12:30 Comment(1)
I don't know if it works for you but it doesn't produce the expected result for me .Saturnalia
T
0

If you are specifying scale_x_discrete (or y) you can simply add in a "" wherever you want a space to appear in the limits and labels statements. This is similar to the first answer but you don't have to add zero values to your dataset.

e.g. This dataset only has 8 bars but they are grouped into two groups of four.

scale_x_discrete( limits=c("BMayC","UMayC","BMayN","UMayN","","BJuneC","UJuneC","BJuneN","UJuneN"), labels=c("BMayC","UMayC", "BMayN","UMayN","","BJuneC","UJuneC","BJuneN","UJuneN"))

Torpid answered 4/5, 2016 at 14:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.