Home > database >  R ggplot2 x-axis labels in facet_wrap
R ggplot2 x-axis labels in facet_wrap

Time:08-06

Below please find the data, code and graph. How can I have the x-axis labels (AA, BB, CC in the red box) under all sets of graphs? The first two sets don't have the labels.

library(ggplot2)

dat <- read.table(text = "
br tr   nbs Method  acc
CC TTT  259    NEW  0.02450
CC TTT  259    OLD  0.01974
CC LLL 1035    NEW  0.28283
CC LLL 1035    OLD  0.16452
CC GGG 1074    NEW  0.31853
CC GGG 1074    OLD  0.18404
CC FFF 1076    NEW  0.50280
CC FFF 1076    OLD  0.32355
CC DDD  260    NEW  0.33026
CC DDD  260    OLD  0.22996
BB TTT  313    NEW  0.21159
BB TTT  313    OLD  0.01854
BB LLL 1442    NEW  0.28157
BB LLL 1442    OLD  0.16598
BB GGG 1442    NEW  0.29526
BB GGG 1442    OLD  0.13012
BB FFF 1444    NEW  0.44271
BB FFF 1444    OLD  0.32320
BB DDD  311    NEW  0.38943
BB DDD  311    OLD  0.22127
AA TTT  666    NEW  0.23289
AA TTT  666    OLD -0.03539
AA LLL 1026    NEW  0.27130
AA LLL 1026    OLD  0.13948
AA GGG 1090    NEW  0.24337
AA GGG 1090    OLD  0.07120
AA FFF 1090    NEW  0.43193
AA FFF 1090    OLD  0.31404
AA DDD  665    NEW  0.39351
AA DDD  665    OLD  0.08671
", header = TRUE)

ggplot(dat, aes(x = br, y = acc, fill = Method)) 
  geom_col(colour="black",width=1, position=position_dodge(0.7), na.rm=FALSE)  
  facet_wrap(~tr, strip.position = "top")

enter image description here

Thanks

CodePudding user response:

We need to set scales = "free_x" in facet_wrap to have the x-axis labels.

library(ggplot2)

ggplot(dat, aes(x = br, y = acc, fill = Method)) 
  geom_col(colour="black",width=1, position=position_dodge(0.7), na.rm=FALSE)  
  facet_wrap(~tr, scales = "free_x")

CodePudding user response:

You can use scales="free_x" argument inside facet_wrap() to have the x-axis tick labels on facets

  • Related