将来自 R 中 Likert 包的 ggplot2 中的图例中的文本换行

问题描述 投票:0回答:1

我在 R 中有一个名为 df 的数据框,它是 Likert 包的 pisaitems 数据的子集。

我面临的问题是,当我绘制 Likert 函数的结果时,我想包装以“Strongly”开头的类别的文本,并且同意或不同意出现在“Strongly”一词下方。我该如何在这个中做到这一点使用 ggplot2 进行 Likert 包设置?

dput(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
library(tidyverse)
library(likert)
likert1<-likert::likert(df)

plot(likert1 , 
     centered = TRUE, center = 3 , 
     ordered = FALSE , 
     include.center=TRUE ,  
     low.color = 'red', 
     neutral.color = 'lightblue' , 
     high.color='green' ,facet=TRUE , 
     legend.position = "bottom" ,
     pch = 0.7 , 
     lcolor = "Black", wrap = 70) + 
  theme(axis.text.y = element_text(colour="black", size="11", hjust =0)) + 
  theme(legend.title=element_blank())

我收到:

enter image description here

r dataframe ggplot2 likert
1个回答
0
投票

您可以通过更改

factor
变量的标签并用换行符替换空格来包装标签。

注意:第二个选项是通过

scale_fill_xxx
的 labels 参数包装标签,但这会覆盖
likert
应用的颜色,因此需要一些额外的努力才能获得正确的颜色。

library(likert)
#> Loading required package: ggplot2
#> Loading required package: xtable

df[] <- lapply(df, \(x) {
  factor(x,
    levels = levels(df$ST24Q01),
    labels = gsub("\\s", "\n", levels(df$ST24Q01))
  )
})

likert1 <- likert::likert(df)

plot(likert1,
  centered = TRUE, center = 3,
  ordered = FALSE,
  include.center = TRUE,
  low.color = "red",
  neutral.color = "lightblue",
  high.color = "green", facet = TRUE,
  legend.position = "bottom",
  pch = 0.7,
  lcolor = "Black", wrap = 70
) +
  theme(
    axis.text.y = element_text(
      colour = "black", size = "11", hjust = 0
    ),
    legend.title = element_blank()
  )

© www.soinside.com 2019 - 2024. All rights reserved.