Search code examples
rggplot2likert

Annotate text in likert chart in R using ggplot2


i am using the pisa items data sets found in likert package in R. I have split the data chart into two zones : "Warning" Zone" and "No Warning Zone".The "Warning" Zone" is from 95% to 100% so I want to put the text "Warning" Zone" in red color in the last bar at the left (not right) inside the plot but horizontally, and vertically now the text "No Warning Zone" to the rest from 0% to 96% also at the left.

How can i do this in R using ggplot2?


library(tidyverse)
library(likert)

data(pisaitems) 

items28 <- pisaitems[, substr(names(pisaitems), 1, 5) == "ST24Q"] 
dim(items28)
items28[items28 == "Agree"] <- "Disagree"
colnames(items28)

df= items28[1:50,]


anno =
 partial(
   annotate,
   "text",
   x = 0,
   angle = 90,
   size = 3,
   fontface = "bold"
 )
p =  likert::likert.bar.plot(df)+geom_vline(
 xintercept = c(1.5),
 linetype = "dashed",
 colour = "grey20"
) 


enter image description here


Solution

  • You need to use the likert function first.

    l <- likert(df)
    
    p <- likert::likert.bar.plot(l) + geom_vline(
      xintercept = c(1.5),
      linetype = "dashed",
      colour = "grey20"
    ) 
    
    p +
      annotate("text", y=-80, x=1, label="Warning Zone", col="red") +
      annotate("text", y=-80, x=6, label="No Warning Zone", col="red", angle = 90)  
    

    enter image description here