Using this data.frame
> df
var value percent
1 AAAA BBBB CCCCC -0.5 9
2 FFFF DDDD CCCCC 0.3 13
3 BBBB NNNN DDDDD 0.7 17
4 NNNN MMMM BBBBB -0.4 25
I want to add the percent
and the sign between brackets "like this (9%)
" as a second line x axis label.
using this script (following this answer)
my.labels <- c(
"AAAA BBBB CCCCC\n(9%)",
"FFFF DDDD CCCCC\n(13%)" ,
"BBBB NNNN DDDDD\n(17%)",
"NNNN MMMM BBBBB\n(25%)"
)
ggplot(df, aes(x = var, y = value))+
geom_bar(stat ="identity", width = 0.4)+
scale_x_discrete(labels = my.labels)
It is fine to do this for 4 variables only but if I have many variables, it will be time taking. I think there should be a faster/efficient way to do this for many variables. Any suggestions will be appreciated.