Specific gaps between strips in the line cabinet - ggplot2 - R

I have a simple bargraph like

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, since the x-axis levels (inverted and now seem y) are related to each other, for example, men and women are disaggregated by sex, working and not working, are another breakdown, etc., I want the axis left some space between each breakdown to indicate these failures.

I tried some things with scale_x_discreteand its parameter crashed, but it seems that it is not. Any ideas?

+4
source share
3 answers

I do not know how to set the different distances between the strips in the line cabinet. However, you can add bars with a height of 0 and no label between 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])

:

  • a$x , as.character.
  • , "" . .
  • scale_x_discrete .

: enter image description here

+3
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))
+2

scale_x_discrete ( y), "", , . , .

. 8 , .

scale_x_discrete (Limits = c ("BMayC", "UMayC", "BMayN", "UMayN", "," BJuneC "," UJuneC "," BJuneN "," UJuneN "), label = c (" BMayC "," UMayC "," BMayN "," UMayN ",", "BJuneC", "UJuneC", "BJuneN", "UJuneN"))

0
source

All Articles