首页 > 解决方案 > 带有ggplot2的饼图,计算条目的出现次数

问题描述

我希望能够用 ggplot2 在 R 中制作一个“饼图”,但计算某个数据出现的次数。

在我的示例中,我有一个 Excel,从中提取一列以及列中出现的值,例如名为“discipline”的列具有以下值:

discipline1, discipline2, discipline3, discipline1, discipline1, discipline2,
discipline2, discipline2, discipline2, discipline2, discipline3, discipline3. 

所以我想画的是每个值出现的百分比:

discipline1, discipline2, discipline3

使用饼图。

对于条形图,我使用了 value stat="count",但我在饼图中看到我不能。

例如,我尝试过这样的事情:

ggplot(df, aes(x = "", y = as.factor(df[,discipline]), 
               fill = as.factor(df[,discipline]))) +
  geom_bar(width = 1, stat = "identity", color = "white") +
  coord_polar("y", start = 0)+
  geom_text(aes(y = as.factor(df[,discipline]), 
                label = as.factor(df[,discipline])), color = "white")+
  scale_fill_manual(values =c("#0073C2FF", "#EFC000FF", "#868686FF"))+
  theme_void()

标签: rggplot2plotpie-chartgeom-text

解决方案


您可以简单地coord_polar(theta='y')在您的之后添加geom_bar

library(ggplot2)

ggplot(data=as.data.frame(discipline), 
        aes(x = factor(1),fill = factor(discipline))) + 
  geom_bar(stat = "count") + 
  scale_y_continuous(breaks = seq(0,12,3), labels = c("0", "25%", "50%", "75%", "100%")) + 
  coord_polar(theta='y') +
  theme(axis.text.y = element_blank(), 
        axis.title.y = element_blank(), 
        axis.ticks.y = element_blank(),
        axis.title.x = element_blank()) +
  labs(fill = "Discipline")

更新:

如果您想添加标签geom_text,我建议使用不同的方法而不是stat = 'count'; 见下文:

library(dplyr)
library(ggplot2)
library(scales)

data.frame(discipline) %>% 
  group_by(discipline) %>% 
  mutate(cnt = n(),
         pct = percent(cnt / nrow(.), accuracy = 1)) %>%
  unique %>% ungroup %>% 
  mutate(place = cumsum(cnt) - cnt/2) %>% 
ggplot(data = ., 
         aes(x = factor(1), weight = cnt, fill = factor(discipline))) + 
  geom_bar(position = "stack") + 
  scale_y_continuous(breaks = seq(0, length(discipline), length(discipline)/4), 
                     labels = c("0", "25%", "50%", "75%", "100%")) + 
  coord_polar(theta='y') +
  geom_text(aes(x = 1.1, y = place, label = pct)) +
  theme(axis.text.y = element_blank(), 
        axis.title.y = element_blank(), 
        axis.ticks.y = element_blank(),
        axis.title.x = element_blank()) +
  labs(fill = "Discipline")

数据:

discipline <- c("discipline1", "discipline2", "discipline3", "discipline1", 
                "discipline1", "discipline2", "discipline2", "discipline2", 
                "discipline2", "discipline2", "discipline3", "discipline3")

推荐阅读