With the following data and code, how can I add two variables on the y-axis?
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", scales = "free_x")
And here is the kind of plot I am looking for. Numbers are written with red fonts. How can I have the "nbs" variable under each "br" variable?
Thanks
CodePudding user response:
One why to do it is to add the label prior to ggplot
.
Here column br
and nbs
are concatenated together with a line separator (\n
).
library(ggplot2)
library(dplyr)
dat %>% mutate(br_lab = paste0(br, "\n", nbs)) %>%
ggplot(aes(x = br_lab, y = acc, fill = Method))
geom_col(colour="black",width=1, position=position_dodge(0.7), na.rm=FALSE)
labs(x = "br")
facet_wrap(~tr, strip.position = "top", scales = "free_x")