我使用 Likert 包创建了一个 ggplot2 对象。我已经使用
修改了标签的字体和字体系列p$layers[[2]]$aes_params$fontface = "bold"
p$layers[[2]]$aes_params$fontfamily = "Calibri"
我要修改的标签在:
p$layers[[2]]$mapping$label
有没有办法修改它以仅显示大于 5% 的值
可重现的示例
options(digits=2)
require(likert)
data(pisaitems)
##### Item 24: Reading Attitudes
items24 <- pisaitems[,substr(names(pisaitems), 1,5) == 'ST24Q']
head(items24); ncol(items24)
names(items24) <- c(
ST24Q01="I read only if I have to.",
ST24Q02="Reading is one of my favorite hobbies.",
ST24Q03="I like talking about books with other people.",
ST24Q04="I find it hard to finish books.",
ST24Q05="I feel happy if I receive a book as a present.",
ST24Q06="For me, reading is a waste of time.",
ST24Q07="I enjoy going to a bookstore or a library.",
ST24Q08="I read only to get information that I need.",
ST24Q09="I cannot sit still and read for more than a few minutes.",
ST24Q10="I like to express my opinions about books I have read.",
ST24Q11="I like to exchange books with my friends.")
str(items24)
l24 <- likert(items24)
lr <- plot(l24,
# Group the items alphabetically
#group.order= group_names,
# for sorting ascending negative answers
# Plot the percentages for each response category
plot.percents = TRUE,
# Plot the total percentage for negative responses
plot.percent.low = FALSE,
# Plot the total percentage for positive responses
plot.percent.high = FALSE,
# Whether response categories should be centered
# This is only helpful when there is a middle response
# option such as "neutral" or "neither agree nor disagree"
centered = FALSE,
# change the ledgend position
legend.position = 'bottom',
# Wrap label text for item labels
wrap=200,
text.size = 6,
text.color = 'white')
lr$layers[[2]]$mapping$label
我已经尝试过了
lr$layers[[2]]$mapping$label <- quo(rlang::as_quosure("ifelse(round(value) < 5, '', paste0(round(value), \"%\")", env = rlang::caller_env()))
lr$layers[[2]]$mapping$label <- quo(rlang::as_quosure("ifelse(round(value) < 5, '', paste0(round(value), \"%\")"), env = 0x000001eab3634ea0)
返回错误
Don't know how to automatically pick scale for object of type
<quosure/formula>. Defaulting to continuous.
Error in `geom_text()`:
! Problem while computing aesthetics.
i Error occurred in the 2nd layer.
Caused by error in `compute_aesthetics()`:
! Aesthetics are not valid data columns.
x The following aesthetics are invalid:
x `label = rlang::as_quosure(...)`
i Did you mistype the name of a data column or forget to add
`after_stat()`?
如果将表达式包含在
expr()
中,它看起来会按预期工作,例如
options(digits=2)
#install.packages("likert")
library(likert)
#> Loading required package: ggplot2
#> Warning: package 'ggplot2' was built under R version 4.3.2
#> Loading required package: xtable
library(plyr)
data(pisaitems)
##### Item 24: Reading Attitudes
items24 <- pisaitems[,substr(names(pisaitems), 1,5) == 'ST24Q']
head(items24); ncol(items24)
#> ST24Q01 ST24Q02 ST24Q03 ST24Q04
#> 68038 Disagree Strongly agree Strongly agree Strongly disagree
#> 68039 Agree Strongly disagree Strongly disagree Strongly agree
#> 68040 Strongly agree Strongly disagree Strongly disagree Agree
#> 68041 Disagree Disagree Agree Strongly disagree
#> 68042 Strongly disagree Disagree Strongly disagree Disagree
#> 68043 Agree Strongly disagree Strongly disagree Agree
#> ST24Q05 ST24Q06 ST24Q07 ST24Q08
#> 68038 Strongly agree Strongly disagree Agree Disagree
#> 68039 Strongly disagree Agree Strongly disagree Agree
#> 68040 Strongly disagree Strongly agree Strongly disagree Agree
#> 68041 Disagree Disagree Agree Strongly disagree
#> 68042 Strongly disagree Disagree Disagree Agree
#> 68043 Strongly disagree Agree Strongly disagree Agree
#> ST24Q09 ST24Q10 ST24Q11
#> 68038 Strongly disagree Agree Agree
#> 68039 Strongly agree Strongly disagree Strongly disagree
#> 68040 Disagree Disagree Strongly disagree
#> 68041 Strongly disagree Agree Agree
#> 68042 Agree Agree Strongly disagree
#> 68043 Strongly agree Strongly disagree Strongly disagree
#> [1] 11
names(items24) <- c(
ST24Q01="I read only if I have to.",
ST24Q02="Reading is one of my favorite hobbies.",
ST24Q03="I like talking about books with other people.",
ST24Q04="I find it hard to finish books.",
ST24Q05="I feel happy if I receive a book as a present.",
ST24Q06="For me, reading is a waste of time.",
ST24Q07="I enjoy going to a bookstore or a library.",
ST24Q08="I read only to get information that I need.",
ST24Q09="I cannot sit still and read for more than a few minutes.",
ST24Q10="I like to express my opinions about books I have read.",
ST24Q11="I like to exchange books with my friends.")
str(items24)
#> 'data.frame': 66690 obs. of 11 variables:
#> $ I read only if I have to. : Factor w/ 4 levels "Strongly disagree",..: 2 3 4 2 1 3 2 3 3 4 ...
#> $ Reading is one of my favorite hobbies. : Factor w/ 4 levels "Strongly disagree",..: 4 1 1 2 2 1 2 1 3 1 ...
#> $ I like talking about books with other people. : Factor w/ 4 levels "Strongly disagree",..: 4 1 1 3 1 1 2 1 2 1 ...
#> $ I find it hard to finish books. : Factor w/ 4 levels "Strongly disagree",..: 1 4 3 1 2 3 4 3 3 3 ...
#> $ I feel happy if I receive a book as a present. : Factor w/ 4 levels "Strongly disagree",..: 4 1 1 2 1 1 2 3 1 2 ...
#> $ For me, reading is a waste of time. : Factor w/ 4 levels "Strongly disagree",..: 1 3 4 2 2 3 2 2 3 4 ...
#> $ I enjoy going to a bookstore or a library. : Factor w/ 4 levels "Strongly disagree",..: 3 1 1 3 2 1 2 1 2 1 ...
#> $ I read only to get information that I need. : Factor w/ 4 levels "Strongly disagree",..: 2 3 3 1 3 3 4 2 3 4 ...
#> $ I cannot sit still and read for more than a few minutes.: Factor w/ 4 levels "Strongly disagree",..: 1 4 2 1 3 4 3 1 3 4 ...
#> $ I like to express my opinions about books I have read. : Factor w/ 4 levels "Strongly disagree",..: 3 1 2 3 3 1 3 2 3 3 ...
#> $ I like to exchange books with my friends. : Factor w/ 4 levels "Strongly disagree",..: 3 1 1 3 1 1 2 3 3 1 ...
l24 <- likert(items24)
lr <- plot(l24,
# Group the items alphabetically
#group.order= group_names,
# for sorting ascending negative answers
# Plot the percentages for each response category
plot.percents = TRUE,
# Plot the total percentage for negative responses
plot.percent.low = FALSE,
# Plot the total percentage for positive responses
plot.percent.high = FALSE,
# Whether response categories should be centered
# This is only helpful when there is a middle response
# option such as "neutral" or "neither agree nor disagree"
centered = FALSE,
# change the ledgend position
legend.position = 'bottom',
# Wrap label text for item labels
wrap=200,
text.size = 6,
text.color = 'white')
lr$layers[[2]]$mapping$label <- rlang::as_quosure(rlang::expr(ifelse(round(value) < 5, '', paste0(round(value), "%"))), env = rlang::caller_env())
lr
# <10 instead of <5
lr$layers[[2]]$mapping$label <- rlang::as_quosure(rlang::expr(ifelse(round(value) < 10, '', paste0(round(value), "%"))), env = rlang::caller_env())
lr
创建于 2024-04-12,使用 reprex v2.1.0
这能解决你的问题吗?