我在 R 中有一个名为 df 的数据框,它是 Likert 包的 pisaitems 数据的子集。
我面临的问题是,当我绘制 Likert 函数的结果时,级别“同意”的图例以白色显示,因为没有此类别的数据。但我希望此类别级别有颜色。
df_plot <- likert(df)
plot(df_plot)
其中 df 是以下数据:
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")
我收到:
我想要的只是将同意级别的颜色更改为其他颜色。假设是绿色。问题是对象 df_plot 是 Likert 类。
class(df_plot)
[1] "likert"
这件事怎么能改变?
该问题似乎与
ggplot2 3.5.0
中最近的更改有关,现在需要在 show.legend=TRUE
内显式设置 geom
以显示未使用的 factor
级别的图例键,并且显然未在 likert
中说明
.
但修复方法是为用于绘制条形的
show.legend=TRUE
层手动设置geom_bar
,该层是由ggplot
返回的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