i have a data frame in R called df which is a subset of pisaitems data of likert package.
The problem i face is that when i plot the result of the likert function the legend of level "Agree" appear in white color because there are no data for this category.But i want this category-level to have a color.
df_plot <- likert(df)
plot(df_plot)
Where the df is the below data :
structure(list(ST24Q01 = structure(c(2L, 2L, 4L, 2L, 1L, 2L,
2L, 2L, 2L, 4L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L,
4L, 2L, 2L, 2L, 1L, 2L, 1L, 4L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L,
2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L), levels = c("Strongly disagree",
"Disagree", "Agree", "Strongly agree"), class = "factor"), ST24Q02 = structure(c(4L,
1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 4L, 2L,
4L, 4L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 2L, 2L, 2L, 2L, 2L, 2L,
4L), levels = c("Strongly disagree", "Disagree", "Agree", "Strongly agree"
), class = "factor"), ST24Q03 = structure(c(4L, 1L, 1L, 2L, 1L,
1L, 2L, 1L, 2L, 1L, 4L, 2L, 1L, 2L, 4L, 2L, 4L, 2L, 2L, 2L, 2L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 2L, 1L, 4L), levels = c("Strongly disagree",
"Disagree", "Agree", "Strongly agree"), class = "factor"), ST24Q04 = structure(c(1L,
4L, 2L, 1L, 2L, 2L, 4L, 2L, 2L, 2L, 4L, 2L, 2L, 2L, 1L, 2L, 1L,
2L, 1L, 1L, 2L, 2L, 2L, 2L, 4L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L,
1L), levels = c("Strongly disagree", "Disagree", "Agree", "Strongly agree"
), class = "factor"), ST24Q05 = structure(c(4L, 1L, 1L, 2L, 1L,
1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 4L, 4L, 2L, 2L,
1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 4L), levels = c("Strongly disagree",
"Disagree", "Agree", "Strongly agree"), class = "factor"), ST24Q06 = structure(c(1L,
2L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 2L, 2L, 2L, 1L, 1L, 2L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
1L), levels = c("Strongly disagree", "Disagree", "Agree", "Strongly agree"
), class = "factor"), ST24Q07 = structure(c(2L, 1L, 1L, 2L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 4L, 2L, 2L,
4L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 4L, 2L, 2L, 2L, 4L, 4L, 1L, 4L), levels = c("Strongly disagree",
"Disagree", "Agree", "Strongly agree"), class = "factor"), ST24Q08 = structure(c(2L,
2L, 2L, 1L, 2L, 2L, 4L, 2L, 2L, 4L, 2L, 2L, 2L, 2L, 1L, 1L, 2L,
2L, 2L, 1L, 1L, 4L, 4L, 2L, 4L, 1L, 2L, 1L, 2L, 4L, 2L, 2L, 2L,
1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L,
1L), levels = c("Strongly disagree", "Disagree", "Agree", "Strongly agree"
), class = "factor"), ST24Q09 = structure(c(1L, 4L, 2L, 1L, 2L,
4L, 2L, 1L, 2L, 4L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 1L,
2L, 2L, 2L, 4L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L,
2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L), levels = c("Strongly disagree",
"Disagree", "Agree", "Strongly agree"), class = "factor"), ST24Q10 = structure(c(2L,
1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 4L, 2L, 1L, 2L, 4L, 2L, 4L,
4L, 4L, 4L, 4L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 2L,
4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 2L,
4L), levels = c("Strongly disagree", "Disagree", "Agree", "Strongly agree"
), class = "factor"), ST24Q11 = structure(c(2L, 1L, 1L, 2L, 1L,
1L, 2L, 2L, 2L, 1L, 4L, 1L, 1L, 4L, 2L, 2L, 4L, 4L, 4L, 2L, 2L,
2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 4L, 2L, 2L, 2L, 1L, 2L, 1L, 4L), levels = c("Strongly disagree",
"Disagree", "Agree", "Strongly agree"), class = "factor")), row.names = 68038:68087, class = "data.frame")
i receive :
all i want is o change the color of the agree level to something else.Let's say green. The problem is that the object df_plot is class of likert.
class(df_plot)
[1] "likert"
How can this thing change ?
The issue seems to be related to a recent change in ggplot2 3.5.0
which now requires to explicitly set show.legend=TRUE
inside the geom
to show a legend key for unused factor
levels and which apparently is not accounted for in likert
.
But a fix would be to manually set show.legend=TRUE
for the geom_bar
layer used to draw the bars which is the third layer of the ggplot
object returned by plot.likert()
:
library(likert)
#> Loading required package: ggplot2
#> Loading required package: xtable
packageVersion("likert")
#> [1] '1.3.5'
packageVersion("ggplot2")
#> [1] '3.5.0'
df_plot <- likert(df)
p <- plot(df_plot)
p$layers[[3]]$show.legend <- TRUE
p