在 ggplot Likert 条中添加水平线

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

我有一个名为 p 的 ggplot 对象:

library(dplyr, warn = FALSE)

likert_levels <- c(
  "Strongly disagree",
  "Disagree",
  "Neither agree nor disagree",
  "Agree",
  "Strongly agree"
)

df <-
  tibble(
    "there is an argument sort_prop_include_center that could be set up to TRUE in order to include half of the centered level when sorting data" = sample(likert_levels, 150, replace = TRUE),
    "Similarly, the argument totals_include_center allows you to include half of the centered level into the left and the right totals" = sample(likert_levels, 150, replace = TRUE, prob = 5:1),
    "Here is one possible option which uses reorder and an ifelse to reorder the variable mapped on y using the counts (aka the sum) of Strictly disagree " = sample(likert_levels, 150, replace = TRUE, prob = 1:5),
    "and disagree answers. Under the hood ggstats::gglikert reshape the data to long where the question id's are stored in a column named .question and the answers in a column named .answer:" = sample(likert_levels, 150, replace = TRUE, prob = 1:5),
    "They used sampling data and create a data frame called df. I am using the same as given in the link.(the df not the df_dk). Ok, if i run in R the following code :" = sample(c(likert_levels, NA), 150, replace = TRUE),
    "proportion of answers higher than the centered level. I want the plot to be sorted according to very left proportions that are the combination (sum) of the two lower levels. (i.e Respectively the percentages on the very right in the sum of the two lower categories. )" = sample(likert_levels, 150, replace = TRUE, prob = c(1, 0, 1, 1, 0))
  ) %>%
  mutate(across(everything(), ~ factor(.x, levels = likert_levels)))



library(ggplot2)
library(ggstats)

p = ggstats::gglikert(df) +
  aes(y = reorder(.question,
                  ifelse(
                    .answer %in% c("Strongly disagree", "Disagree"),
                    1, 0
                  ),
                  FUN = sum
  ))+ theme(axis.text.y = element_text(size = 8))

我想水平添加两行:

  1. 分隔 y 轴的线现在按 20% 的降序排序,并且

  2. 第二行为 50%。

另外,我想要一个从 0 到 20% 的文本在左侧的面板(图)内写“警告区”,从 20% 到 50% 的文本“一般”,从 50% 到 100% 的文本写“无警告”区”

如何在这个排序比例李克特图中使用 R 中的 ggplot2 包来实现这一点?

r ggplot2 likert
1个回答
0
投票

这对你有用吗?

library(dplyr, warn = FALSE)
library(ggplot2)
library(ggstats)
library(purrr)

# Data
likert_levels <- c(
  "Strongly disagree",
  "Disagree",
  "Neither agree nor disagree",
  "Agree",
  "Strongly agree"
)

df <-
  tibble(
    "there is an argument sort_prop_include_center that could be set up to TRUE in order to include half of the centered level when sorting data" = sample(likert_levels, 150, replace = TRUE),
    "Similarly, the argument totals_include_center allows you to include half of the centered level into the left and the right totals" = sample(likert_levels, 150, replace = TRUE, prob = 5:1),
    "Here is one possible option which uses reorder and an ifelse to reorder the variable mapped on y using the counts (aka the sum) of Strictly disagree " = sample(likert_levels, 150, replace = TRUE, prob = 1:5),
    "and disagree answers. Under the hood ggstats::gglikert reshape the data to long where the question id's are stored in a column named .question and the answers in a column named .answer:" = sample(likert_levels, 150, replace = TRUE, prob = 1:5),
    "They used sampling data and create a data frame called df. I am using the same as given in the link.(the df not the df_dk). Ok, if i run in R the following code :" = sample(c(likert_levels, NA), 150, replace = TRUE),
    "proportion of answers higher than the centered level. I want the plot to be sorted according to very left proportions that are the combination (sum) of the two lower levels. (i.e Respectively the percentages on the very right in the sum of the two lower categories. )" = sample(likert_levels, 150, replace = TRUE, prob = c(1, 0, 1, 1, 0))
  ) %>%
  mutate(across(everything(), ~ factor(.x, levels = likert_levels)))

# function to save retyping common arguments
anno <- partial(annotate, "text", x = -1, angle = 90, size = 3, fontface = "bold")

gglikert(df) +
  aes(y = reorder(.question,
                  ifelse(
                    .answer %in% c("Strongly disagree", "Disagree"),
                    1, 0
                  ),
                  FUN = sum
  )) +
  theme(axis.text.y = element_text(size = 8)) + 
  geom_hline(yintercept = c(1.5, 5.5), linetype = "dashed", colour = "grey20") +
  coord_cartesian(clip = "off") +
  anno(y = 1, label = "warning", color = "#A6611A") +
  anno(y = 3.5, label = "so-so", color = "black") +
  anno(y = 6, label = "no warning", color = "#018571") +
  labs(y = NULL)

创建于 2024-04-20,使用 reprex v2.1.0

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