如何在李克特图表的每个条上输出正确的百分比? [英] How do I output the correct percentages on each bar of my Likert chart?

查看:98
本文介绍了如何在李克特图表的每个条上输出正确的百分比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取我的代码,以便在每个单独的条形图的顶部输出百分比.现在,下面显示的百分比是错误的.我的代码结合了标签1和2以及标签3和4,然后将这些数字输出到错误的一侧.

I'm trying to get my code to output the percentage on top of each individual bar. Right now, the percentages shown below are on the wrong side. My code combines Labels 1 and 2 and Labels 3 and 4, and then outputs those numbers on the incorrect sides.

是否有一个可以正确标记这些条形图的功能?我包括我的代码,.csv文件中的数据以及当前的可视化效果.

Is there a function to label those bars correctly? I'm including my code, the data from the .csv file, and the current visualization.

library(ggplot2)
library(reshape)
library(likert)
library(dplyr)

setwd("~/Desktop/")

df <- read.csv("Likert_Test.csv")

df[2:3] <- lapply(df[2:3], as.factor)
colnames(df)[2:3] <- c("cake?", "cookies?")

df[2:3] <- lapply(df[2:3], factor, levels = 1:4)
myplot <- likert(df[2:3], grouping = df$gender)

plot(myplot, centered = FALSE, col = c("#0A2240", "#3474DA", "#C1A783", "#323A45")) +
  scale_y_continuous(labels = function(x) paste0(x, "%")) +
  ggtitle("How much do you like...") +
  theme(legend.title = element_blank(),
        axis.title = element_blank(),
        plot.title = element_text(hjust = 0.5))

    gender      cake         cookies
    Male        3            1    
    Male        2            2         
    Male        2            2
    Male        4            2
    Male        2            3
    Male        2            3
    Male        2            3
    Male        1            1
    Male        4            2
    Female      1            1
    Female      3            1
    Female      3            4
    Female      3            4
    Female      1            1
    Female      4            3
    Female      4            2
    Female      3            2
    Female      2            1
    Female      3            1

推荐答案

这是一个示例答案-使用ggplot而不使用likart软件包.

Here's a sample answer - using ggplot without the using the likart package.



library(tidyverse)
#> Warning: package 'tibble' was built under R version 3.6.3
#> Warning: package 'dplyr' was built under R version 3.6.3
df <- readr::read_table("gender      cake         cookies
    Male        3            1    
    Male        2            2         
    Male        2            2
    Male        4            2
    Male        2            3
    Male        2            3
    Male        2            3
    Male        1            1
    Male        4            2
    Female      1            1
    Female      3            1
    Female      3            4
    Female      3            4
    Female      1            1
    Female      4            3
    Female      4            2
    Female      3            2
    Female      2            1
    Female      3            1")

df %>%
  pivot_longer(-gender, names_to = "question", values_to = "values") %>%
  group_by(gender, question) %>%
  count(values) %>%
  mutate(
    level = case_when(values %in% c(3, 4) ~ "high",
                      values %in% c(1, 2) ~ "low",
                      TRUE ~ "NA"),
    values = as.character(values),
    total_n = sum(n),
    pct_low = sum(n[level == "low"]) / sum(n),
    pct_high = sum(n[level == "high"]) / sum(n)
  ) %>%
  print() %>%
  ggplot(aes(x = gender, y = n, fill = values)) +
  geom_bar(aes(fill = values), position = position_fill(reverse = TRUE), stat = "identity") +
  scale_fill_manual(values = c("#0A2240", "#3474DA", "#C1A783", "#323A45")) +
  scale_y_continuous(labels = scales::percent_format(),
                     expand = expand_scale(mult = .05)) +
  geom_text(aes(
    y = -.05,
    x = gender,
    label = scales::percent(round(pct_low, 2), accuracy = 1)
  ),
  data = . %>% filter(level == "low")) +
  geom_text(aes(
    y = 1.05,
    x = gender,
    label = scales::percent(round(pct_high, 2), accuracy = 1)
  ),
  data = . %>% filter(level == "high")) +
  coord_flip() +
  facet_wrap(~ question, nrow = 2) +
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(),
    axis.line = element_line(colour = "black"),
    plot.title = element_text(hjust = 0.5),
    legend.position = "bottom"
  ) +
  labs(title = "How Much do you like...",
       fill = "",
       x = NULL,
       y = NULL)
#> # A tibble: 15 x 8
#> # Groups:   gender, question [4]
#>    gender question values     n level total_n pct_low pct_high
#>    <chr>  <chr>    <chr>  <int> <chr>   <int>   <dbl>    <dbl>
#>  1 Female cake     1          2 low        10   0.3      0.7  
#>  2 Female cake     2          1 low        10   0.3      0.7  
#>  3 Female cake     3          5 high       10   0.3      0.7  
#>  4 Female cake     4          2 high       10   0.3      0.7  
#>  5 Female cookies  1          5 low        10   0.7      0.3  
#>  6 Female cookies  2          2 low        10   0.7      0.3  
#>  7 Female cookies  3          1 high       10   0.7      0.3  
#>  8 Female cookies  4          2 high       10   0.7      0.3  
#>  9 Male   cake     1          1 low         9   0.667    0.333
#> 10 Male   cake     2          5 low         9   0.667    0.333
#> 11 Male   cake     3          1 high        9   0.667    0.333
#> 12 Male   cake     4          2 high        9   0.667    0.333
#> 13 Male   cookies  1          2 low         9   0.667    0.333
#> 14 Male   cookies  2          4 low         9   0.667    0.333
#> 15 Male   cookies  3          3 high        9   0.667    0.333

reprex软件包(v0.3.0)创建于2020-04-22 sup>

Created on 2020-04-22 by the reprex package (v0.3.0)

这篇关于如何在李克特图表的每个条上输出正确的百分比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆