【问题标题】:Overlaying percentages on top of individual bars in Likert在李克特的各个条形之上叠加百分比
【发布时间】:2020-04-22 16:12:04
【问题描述】:

我想将标签作为百分比添加到李克特图表的各个条形中。 有没有简单的方法可以做到这一点?我尝试过使用 geom_text、调整排序命令并启用“plot.percents = TRUE”。这些东西似乎都不起作用。

下面是我的代码、数据和当前图。

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

setwd("~/Desktop/")

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

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

df[2] <- lapply(df[2], factor, levels = 1:4)
myplot <- likert(df[2], 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),
        aspect.ratio = 1/6)

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

【问题讨论】:

    标签: r ggplot2 dplyr visualization likert


    【解决方案1】:

    您的百分比在左侧和右侧的位置与您的列值不匹配有一个奇怪的问题。更奇怪的是,使用Centered = TRUE,您的不同值被正确排序并且与您的百分比显示相匹配:

    plot(myplot, centered = TRUE, ordered = TRUE, col = c("#0A2240", "#3474DA", "#C1A783", "#323A45")) 
    

    无论如何,如果您正在寻找使用ggplot2 而不是likert 的解决方案,您可以从操纵Likert 的结果开始,以获得更长的格式,如下所示:

    library(tidyr)
    library(dplyr)
    
    as.data.frame(myplot$results) %>% rowwise() %>%
      mutate(Label_Left = sum(`1`,`2`), Label_Right = `3`+`4`) %>%
      pivot_longer(cols =`1`:`4`, names_to = "Response", values_to = "Percentage") 
    
    # A tibble: 16 x 6
       Group  Item     Label_Left Label_Right Response Percentage
       <fct>  <fct>         <dbl>       <dbl> <chr>         <dbl>
     1 Female cake?          30          70   1              20  
     2 Female cake?          30          70   2              10  
     3 Female cake?          30          70   3              50  
     4 Female cake?          30          70   4              20  
     5 Female cookies?       70          30   1              50  
     6 Female cookies?       70          30   2              20  
     7 Female cookies?       70          30   3              10  
     8 Female cookies?       70          30   4              20  
     9 Male   cake?          66.7        33.3 1              11.1
    10 Male   cake?          66.7        33.3 2              55.6
    11 Male   cake?          66.7        33.3 3              11.1
    12 Male   cake?          66.7        33.3 4              22.2
    13 Male   cookies?       66.7        33.3 1              22.2
    14 Male   cookies?       66.7        33.3 2              44.4
    15 Male   cookies?       66.7        33.3 3              33.3
    16 Male   cookies?       66.7        33.3 4               0  
    

    然后,您可以添加ggplot 部分并使用geom_text 显示所有需要的标签:

    library(tidyr)
    library(dplyr)
    library(ggplot2)
    
    as.data.frame(myplot$results) %>% rowwise() %>%
      mutate(Label_Left = sum(`1`,`2`), Label_Right = `3`+`4`) %>%
      pivot_longer(cols =`1`:`4`, names_to = "Response", values_to = "Percentage") %>%
      ggplot(aes(x = Group, y = Percentage))+
      geom_col(aes(fill = Response), position = position_stack(reverse = TRUE))+
      geom_text(data = . %>% filter(Percentage !=0), 
                aes(fill = Response, label = scales::percent(Percentage/100)), 
                position = position_stack(reverse = TRUE,0.5), color = "white")+
      scale_fill_manual(values =  c("#0A2240", "#3474DA", "#C1A783", "#323A45"))+
      scale_y_continuous(labels = function(x) scales::percent(x/100), name = "")+
      coord_flip()+
      facet_wrap(~Item, ncol = 1)+
      geom_text(data = . %>% distinct(Group, Item, Label_Left), 
                aes(x = Group, y = 0, label = scales::percent(round(Label_Left,0)/100)), hjust = 1)+
      geom_text(data = . %>% distinct(Group, Item, Label_Right), 
                aes(x = Group, y = 100, label = scales::percent(round(Label_Right,0)/100)), hjust = 0)+
      theme(legend.position = "bottom",
            axis.title.y = element_blank())+
      labs(title = "How much do you like... ")
    

    它回答了你的问题吗?


    可重现的例子

    我使用了您在上一个问题中显示的数据集 (How do I output the correct percentages on each bar of my Likert chart?)

    df <- data.frame(gender = c(rep(c("Male","Female"), each = 9),"Female"),
                     cake = c(3,2,2,4,2,2,2,1,4,1,3,3,3,1,4,4,3,2,3),
                     cookie = c(1,2,2,2,3,3,3,1,2,1,1,4,4,1,3,2,2,1,1))
    

    【讨论】:

    • 谢谢!这非常有帮助。不过,我确实有一个问题。你能解释一下上面的小标题发生了什么吗?我不确定我是否理解您包含的第一个面板中发生了什么。
    • 第一张图只是为了向您展示,当您使用centered = TRUE 绘制数据时,每个类别的数学左右百分比的显示在您使用centered = FALSE 时不再是这种情况。我不知道为什么您的数据会发生这种情况。目前没有任何解释。
    • 再次感谢您!如果可以的话,还有一个问题:有没有一种方法可以将百分比从侧面移除?我只想要栏中的数字,而不是旁边的数字。
    • 不客气。只需使用label_leftlabel_right 删除最后两个geom_text。它应该工作
    猜你喜欢
    • 2020-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多