如何为每个条形添加标签(在y轴上计数)?

时间:2017-05-10 12:13:03

标签: r ggplot2

我正在尝试重新创建如下所示的图像。我在为每个栏添加标签时遇到麻烦。我也已经搜索了谷歌和SO的解决方案,但我无法使它们适应我的问题。 没有必要实现完全100%的复制。

当前状态:已解决

Current State Graphic

目标

Target Graphic

代码

我编写了一个函数'createBarplot',它将数据帧(显然是数据帧)和x(字符串/字符)作为参数。不要问为什么。无论如何,这就是代码看起来像这样的原因。

ggplot(dataframe, aes(x = dataframe[, x])) +
    geom_bar(aes(y = (..count..)/sum(..count..)), fill="#0081DB", colour="#000F83", size=2) +
    scale_y_continuous(labels = scales::percent, breaks = seq(0, 0.6, by = 0.1)) +
    scale_x_discrete(labels = levels(dataframe[, x])) +
    labs(y = "Prozent", x = NULL) +
    ggtitle(label = attr(sampledata, "variable.labels")[do.call("getColumnIndexByName",args = list(dataframe, x))]) +
    geom_text(aes(y = ..count.. / sum(..count..),
                  label = paste0(round(..count.. / sum(..count..) * 100, 0),"%"),
                  hjust = ifelse( (..count.. / sum(..count..)) >= 0.025, 1.25, -0.25)),
              stat="count") +
    theme(panel.background = element_rect(fill = "white"),
          axis.line = element_line(colour = "black"),
          plot.title = element_text(hjust = 0.1)) +
    coord_flip()

数据

dataframe <- as.data.frame(x = c(5L, 2L, 4L, 5L, 5L, 5L, 4L, 4L, 2L, 4L, 4L, 4L, 5L, 
                                           3L, 4L, 4L, 4L, 3L, 4L, 4L, 4L, 5L, 5L, 3L, 4L, 4L, 3L, 4L, 4L, 
                                           3L, 4L, 4L, 4L, 4L, 2L, 4L, 4L, 4L, 3L, 5L, 5L, 4L, 4L, 5L, 5L, 
                                           5L, 4L, 4L, 2L, 5L, 4L, 4L, 5L, 5L, 5L, 4L, 4L, 4L, 4L, 5L, 4L, 
                                           4L, 4L, 4L, 4L, 4L, 3L, 2L, 4L, 4L, 3L, 4L, 4L, 4L, 4L, 4L, 3L, 
                                           4L, 4L, 4L, 4L, 2L, 2L, 4L, 4L, 4L, 4L, 3L, 4L, 4L, 4L, 5L, 3L, 
                                           4L, 4L, 4L, 3L, 2L, 5L, 3L, 4L, 3L, 3L, 4L, 4L, 5L, 4L, 4L, 2L, 
                                           5L, 3L, 4L, 4L, 5L, 4L, 5L, 4L, 4L, 5L, 4L, 2L, 3L, 4L, 4L, 4L, 
                                           4L, 5L, 4L, 3L, 3L, 5L, 3L, 4L, 4L, 4L, 4L, 3L, 3L, 5L, 4L, 5L, 
                                           4L, 4L, 4L, 3L, 4L, 3L, 2L, 4L, 3L, 4L, 4L, 4L, 4L, 3L, 4L, 5L, 
                                           4L, 4L, 4L, 4L, 4L, 5L, 4L, 3L, 4L, 4L, 4L, 3L, 3L, 4L, 4L, 4L, 
                                           5L, 5L, 4L, 4L, 4L, 5L, 5L, 4L, 4L, 4L, 4L, 5L, 4L, 4L, 5L, 4L, 
                                           3L, 5L, 4L, 3L, 3L, 5L, 4L, 4L, 5L, 4L, 4L, 5L, 5L, 4L, 5L, 4L, 
                                           5L, 3L, 3L, 4L, 5L, 4L, 3L, 4L, 4L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 
                                           5L, 4L, 4L, 3L, 4L, 3L, 4L, 5L, 4L, 4L, 2L, 5L, 4L, 5L, 2L, 3L, 
                                           3L, 4L, 4L, 4L, 5L, 5L, 3L, 4L, 4L, 5L, 4L, 4L, 4L), .Label = c("5 Sehr unzufrieden", 
                                                                                                           "4", "3", "2", "1 Sehr zufrieden"), class = "factor")

colnames(dataframe) <- "col"
x <- "col"

1 个答案:

答案 0 :(得分:1)

你可以做到

library(tidyverse)
dataframe %>% 
  ggplot(aes_string(x = x)) + 
  geom_bar(aes(y = (..count..)/sum(..count..)), stat = "count") + 
  coord_flip() + 
  geom_text(aes(y=..count../sum(..count..), label=paste0(..count../sum(..count..)*100,"%")), hjust=1, stat="count")

但是,如果将其与预先计算的

进行比较
dataframe %>% 
  count_(x) %>%
  mutate(n=n/sum(n)*100) %>%
  ggplot(aes(x=col, y=n)) + 
  geom_col() + 
  coord_flip() + 
  geom_text(aes(label = paste0(n, "%")), hjust=1)

我会说后者更具可读性(对于更大的数据集也可能更快)。两者都应该给你相同的结果。