将标签添加到ggplot堆叠条形图中的整个条形

时间:2018-12-18 23:16:56

标签: r ggplot2

我有一些关于多少个人同意/不同意某些陈述的调查数据。我想从这些数据中基本制作一个堆叠的条形图,其中的条形表示语句,每个条形图中的不同部分表示每个级别的响应百分比。

鉴于数据的结构方式(下面包含示例数据),我可以使用以下代码创建这种类型的图表:

ggplot(data=data, aes(x = items)) +
  geom_bar(aes(fill = answer), position = "fill") +
  coord_flip() +
  guides(fill = guide_legend(reverse = TRUE))

我想在这张图表上走得更远,并在每个条形上都加上同意或强烈同意每种说法的人所占的百分比。我不要每个级别的百分比。

我希望我的图表看起来像这样:

chart

我可以这样计算百分比:

data %>%
  mutate(answer = recode_factor(answer, 
                                "Strongly Agree" = "Agree",
                                "Strongly Disagree" = "Disagree")) %>%
  group_by(items) %>%
  summarise(agree = sum(answer == 'Agree'), disagree = sum(answer == 'Disagree')) %>%
  mutate(percent_agree = agree / (agree + disagree)) %>%
  select(items, percent_agree)

但是然后我不确定如何将其与正确的语句和栏进行匹配。我尝试加入汇总百分比,然后再使用labels选项在绘制之前对原始数据表示同意/强烈同意,但这并不符合预期。

如何添加百分比同意/强烈同意作为图表标签?


data <- structure(list(items = c("Responds to longer-term changes in global market cycles", 
"Responds to longer-term changes in global market cycles", "Responds to longer-term changes in global market cycles", 
"Responds to longer-term changes in global market cycles", "Responds to longer-term changes in global market cycles", 
"Responds to longer-term changes in global market cycles", "Responds to longer-term changes in global market cycles", 
"Responds to longer-term changes in global market cycles", "Responds to longer-term changes in global market cycles", 
"Responds to longer-term changes in global market cycles", "Responds to longer-term changes in global market cycles", 
"Responds to longer-term changes in global market cycles", "Responds to longer-term changes in global market cycles", 
"Responds to longer-term changes in global market cycles", "Responds to longer-term changes in global market cycles", 
"Responds to longer-term changes in global market cycles", "Responds to longer-term changes in global market cycles", 
"Responds to longer-term changes in global market cycles", "Responds to longer-term changes in global market cycles", 
"Responds to longer-term changes in global market cycles", "Responds to longer-term changes in global market cycles", 
"Responds to longer-term changes in global market cycles", "Responds to longer-term changes in global market cycles", 
"Responds to longer-term changes in global market cycles", "Responds to longer-term changes in global market cycles", 
"Responds to longer-term changes in global market cycles", "Responds to longer-term changes in global market cycles", 
"Responds to longer-term changes in global market cycles", "Responds to longer-term changes in global market cycles", 
"Responds to longer-term changes in global market cycles", "Responds to longer-term changes in global market cycles", 
"Responds to longer-term changes in global market cycles", "Responds to longer-term changes in global market cycles", 
"Responds to longer-term changes in global market cycles", "Responds to longer-term changes in global market cycles", 
"Responds to longer-term changes in global market cycles", "Responds to longer-term changes in global market cycles", 
"Responds to longer-term changes in global market cycles", "Responds to longer-term changes in global market cycles", 
"Responds to longer-term changes in global market cycles", "Responds to longer-term changes in global market cycles", 
"Responds to longer-term changes in global market cycles", "Responds to longer-term changes in global market cycles", 
"Responds to longer-term changes in global market cycles", "Responds to longer-term changes in global market cycles", 
"Responds to longer-term changes in global market cycles", "Responds to longer-term changes in global market cycles", 
"Responds to longer-term changes in global market cycles", "Responds to longer-term changes in global market cycles", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Makes changes to respond to an existing business partners’ needs", 
"Responds to short-term market shifts", "Responds to short-term market shifts", 
"Responds to short-term market shifts", "Responds to short-term market shifts", 
"Responds to short-term market shifts", "Responds to short-term market shifts", 
"Responds to short-term market shifts", "Responds to short-term market shifts", 
"Responds to short-term market shifts", "Responds to short-term market shifts", 
"Responds to short-term market shifts", "Responds to short-term market shifts", 
"Responds to short-term market shifts", "Responds to short-term market shifts", 
"Responds to short-term market shifts", "Responds to short-term market shifts", 
"Responds to short-term market shifts", "Responds to short-term market shifts", 
"Responds to short-term market shifts", "Responds to short-term market shifts", 
"Responds to short-term market shifts", "Responds to short-term market shifts", 
"Responds to short-term market shifts", "Responds to short-term market shifts", 
"Responds to short-term market shifts", "Responds to short-term market shifts", 
"Responds to short-term market shifts", "Responds to short-term market shifts", 
"Responds to short-term market shifts", "Responds to short-term market shifts", 
"Responds to short-term market shifts", "Responds to short-term market shifts", 
"Responds to short-term market shifts", "Responds to short-term market shifts", 
"Responds to short-term market shifts", "Responds to short-term market shifts", 
"Responds to short-term market shifts", "Responds to short-term market shifts", 
"Responds to short-term market shifts", "Responds to short-term market shifts", 
"Responds to short-term market shifts", "Responds to short-term market shifts", 
"Responds to short-term market shifts", "Responds to short-term market shifts", 
"Responds to short-term market shifts", "Responds to short-term market shifts", 
"Responds to short-term market shifts", "Responds to short-term market shifts", 
"Responds to short-term market shifts", "Adopts new technologies", 
"Adopts new technologies", "Adopts new technologies", "Adopts new technologies", 
"Adopts new technologies", "Adopts new technologies", "Adopts new technologies", 
"Adopts new technologies", "Adopts new technologies", "Adopts new technologies", 
"Adopts new technologies", "Adopts new technologies", "Adopts new technologies", 
"Adopts new technologies", "Adopts new technologies", "Adopts new technologies", 
"Adopts new technologies", "Adopts new technologies", "Adopts new technologies", 
"Adopts new technologies", "Adopts new technologies", "Adopts new technologies", 
"Adopts new technologies", "Adopts new technologies", "Adopts new technologies", 
"Adopts new technologies", "Adopts new technologies", "Adopts new technologies", 
"Adopts new technologies", "Adopts new technologies", "Adopts new technologies", 
"Adopts new technologies", "Adopts new technologies", "Adopts new technologies", 
"Adopts new technologies", "Adopts new technologies", "Adopts new technologies", 
"Adopts new technologies", "Adopts new technologies", "Adopts new technologies", 
"Adopts new technologies", "Adopts new technologies", "Adopts new technologies", 
"Adopts new technologies", "Adopts new technologies", "Adopts new technologies", 
"Adopts new technologies", "Adopts new technologies", "Adopts new technologies"
), answer = structure(c(1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 
2L, 1L, 2L, 2L, 3L, 1L, 3L, 2L, 1L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 
1L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
3L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 3L, 2L, 2L, 2L, 2L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 3L, 1L, 2L, 3L, 2L, 2L, 2L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 
2L, 2L, 1L, 3L, 2L, 2L, 2L, 1L, 1L, 2L, 3L, 2L, 2L, 3L, 2L, 2L, 
3L, 3L, 3L, 2L, 2L, 1L, 2L, 1L, 1L, 3L, 1L, 2L, 2L, 3L, 3L, 3L, 
1L, 3L, 2L, 3L, 3L, 2L, 2L, 3L, 2L, 2L, 3L, 2L, 2L, 2L, 3L, 2L, 
3L, 3L, 3L, 1L, 3L, 2L, 2L, 2L, 2L, 2L), .Label = c("Strongly Agree", 
"Agree", "Disagree", "Strongly Disagree"), class = c("ordered", 
"factor"))), row.names = c(NA, -196L), class = "data.frame")

1 个答案:

答案 0 :(得分:3)

有点笨拙,但可以完成工作:

1

代码

ggplot(data = d, aes(x = items, y = n)) +
    geom_col(aes(fill = answer), position = "fill") +
    coord_flip() +
    geom_label(aes(items, Inf,
                   label = ifelse(answer == "Agree", scales::percent(cumperc), NA),
                   hjust = 1,
                   vjust = 1)) +
    guides(fill = guide_legend(reverse = TRUE)) 

我利用“同意”的因子水平来绘制其累积百分比。您还可以看到对数据进行了一些调整,以使其更容易使用,但是通过在运行时对原始数据集进行整理,通常也可以得到相同的结果。没那么漂亮。

数据

d <- data %>% 
    # reshape into a more manageable format 
    group_by(items, answer) %>%
    summarize(n = n()) %>%
    ungroup() %>%
    # add cumulative percentages
    group_by(items) %>%
    mutate(cumperc = cumsum(n)/sum(n)) %>%
    ungroup() %>%
    # add line breaks to the items for aesthetic reasons
    mutate(items = gsub("to an", "to an\n", items),
           items = gsub("term changes", "term changes\n", items))

> head(d)
# A tibble: 6 x 4
  items                                    answer         n cumperc
  <chr>                                    <ord>      <int>   <dbl>
1 Adopts new technologies                  Strongly …     6   0.122
2 Adopts new technologies                  Agree         24   0.612
3 Adopts new technologies                  Disagree      19   1    
4 "Makes changes to respond to an\n exist… Strongly …     9   0.184
5 "Makes changes to respond to an\n exist… Agree         34   0.878
6 "Makes changes to respond to an\n exist… Disagree       6   1