I want to add text to right side of a likert plot.
I tried doing it with geom_text(), but I can't figure out how to load the right strings into the geom_text() function.
Here is a sample dataset:
library(likert)
data <- data.frame("bad carpet" = factor(sample(1:5, 50, replace=T)),
"bad shower" = factor(sample(1:5, 50, replace=T)),
"bad bath" = factor(sample(1:5, 50, replace=T)))
plot(likert(data))
This produces: problem
What I want (with the correct font & size of course): solution
I already added:
plot(likert(data)) + scale_y_continuous(limits=c(-100,150)) +
coord_flip(ylim=c(-110,110)) +
theme(plot.margin=unit(c(0.2,2,0.2,0.2),"cm"))
which produces extra room for the text. Geom_text() should be added somewhere in between, but that is where I get stuck.
As described in other answers (for example, here and here), you can use any of the coord_
functions with clip="off"
to add annotations of any geom outside the plot area.
This addition to your plotting code gets you close, but you'll have to play around with size and colour to get them to match exactly.
p <- plot(likert(data)) +
scale_y_continuous(limits=c(-100,150)) +
coord_flip(ylim=c(-110,110), clip = "off") +
theme(plot.margin=unit(c(0.2,2.25,0.2,0.2),"cm"))
p + annotate("text", label=labs, x=c(1:3), y=c(125), hjust=0, size=3, colour="grey30")