简体   繁体   English

向条形ggplot添加计数和百分比

[英]Adding count and percent to bars ggplot

I have added counts to the top of bars, but cannot figure out how to add % in brackets next to the count!我已在条形顶部添加计数,但无法弄清楚如何在计数旁边的括号中添加 %! My attempts return prop*100 next to the count above the bar.我的尝试在条形上方的计数旁边返回prop*100 Here is my code that generates count only - what do I need to modify to this to add the %?这是我只生成计数的代码 - 我需要对此进行什么修改才能添加 %?

df %>% 
  ggplot(aes(x = day_of_week, fill = day_of_week)) +
    geom_bar(position = "dodge") +
    geom_text(aes(label = ..count..), stat = "count", vjust = -1.0, colour = "black") +
    ylim(0, 2600) +
    theme_classic() +
    theme(legend.position="none",
           axis.text.x = element_text(angle=45, hjust=1)
          ) +
  scale_fill_brewer(palette = 1, direction = - 1) +
    xlab("day_of_week") +
    ylab("Count (n)")

I ended up create a new col with a count of each category of interest and ran the code below!我最终创建了一个新的列,计算了每个感兴趣的类别并运行了下面的代码!

df2 <- df %>%
  select(day_of_week) %>%
  group_by(day_of_week) %>% 
  mutate(count_day_of_week_group = n()) %>%
  ungroup

df2 <- df2 %>%  
  distinct(count_day_of_week_group, .keep_all = TRUE)

ggplot(df2, aes(x = day_of_week, y = count_day_of_week_group)) + 
  geom_bar(stat = "identity", color = "black", fill = "dodgerblue1") +
  geom_text(label = with(df2, 
                         sprintf("%d (%.0f%%)", 
                                 count_day_of_week_group, 100*count_day_of_week_group/sum(count_day_of_week_group))), 
            vjust=-1)+
  ylim(0,3000)

Here is an option without creating a new column:这是一个不创建新列的选项:

df %>% 
  ggplot(aes(x = day_of_week, fill = day_of_week)) +
  geom_bar(position = "dodge") +
  geom_text(aes(label = paste0(..count..,"(",round(..count..*100/nrow(df)), "%)")), stat = "count", vjust = -1.0, colour = "black") +
  ylim(0, 2600) +
  theme_classic() +
  theme(legend.position="none",
        axis.text.x = element_text(angle=45, hjust=1)
  ) +
  scale_fill_brewer(palette = 1, direction = - 1) +
  xlab("day_of_week") +
  ylab("Count (n)")

在此处输入图像描述

And here is an option with a new column created before the plot:这是一个在 plot 之前创建的新列的选项:

df %>% group_by(day_of_week) %>%
  summarise(n =n())%>%
  mutate(pct= round(n/sum(n)*100, digit=0))%>%
  ggplot(aes(x = day_of_week, y = n, fill = day_of_week)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = paste0(n, " (",pct, "%)")), vjust = -1.0, colour = "black") +
  ylim(0, 2600) +
  theme_classic() +
  theme(legend.position="none",
        axis.text.x = element_text(angle=45, hjust=1)
  ) +
  scale_fill_brewer(palette = 1, direction = - 1) +
  xlab("day_of_week") +
  ylab("Count (n)")

在此处输入图像描述

Data数据


set.seed(14) 
df = data.frame(day_of_week = factor(sample(c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"),
                                     size = 3000,
                                     prob = c(1,3,5,2,6,10,1),
                                     replace = TRUE), levels = c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM