首页 > 解决方案 > 如何在r中绘制不同月份的比较

问题描述

我试图展示新求职者在新冠时间几个月内的增长。因此,我从完整的 df 中过滤了我需要的统计数据表,并将其命名为“dist.newseek”

 Cdata <- structure(list(Month = c(
  "2020-01", "2020-01", "2020-01", "2020-01",
  "2020-01", "2020-02", "2020-02", "2020-02", "2020-02", "2020-02",
  "2020-03", "2020-03", "2020-03", "2020-03", "2020-03", "2020-04",
  "2020-04", "2020-04", "2020-04", "2020-04"
), District = c(
  "Dan",
  "Jerusalem", "North", "Sharon", "South", "Dan", "Jerusalem",
  "North", "Sharon", "South", "Dan", "Jerusalem", "North", "Sharon",
  "South", "Dan", "Jerusalem", "North", "Sharon", "South"
), NewSeekers = c(
  6551L,
  3589L, 6154L, 4131L, 4469L, 5529L, 2721L, 5061L, 3464L, 3612L,
  231315L, 137479L, 159445L, 123753L, 104868L, 55038L, 33995L,
  40572L, 31373L, 23914L
)), row.names = c(NA, -20L), class = "data.frame")

我正在努力寻找最好的方式来展示 1 月到 4 月之间 NewSeekers 的成长如果你有任何其他情节建议,我会接受

至于问题,我用了ggplot,geom_text但是圆圈不完整,文字根本不清楚

这是我使用的代码:

dist.newseek <- Cdata %>% 
  group_by(Month,District) %>% 
  summarise(NewSeekers=sum(NewSeekers))


ggplot(dist.newseek, aes(x="", y=NewSeekers, group=District, color=District, fill=District)) +
  geom_bar(width = 1, stat = "identity") +
  geom_text(aes(label = paste0(NewSeekers,
                               " (",
                               scales::percent(NewSeekers / sum(NewSeekers)),
                               ")")),
            position = position_stack(vjust = 0.5)) +
  coord_polar("y", start=0) + facet_wrap(~ Month) +
  theme(axis.text = element_blank(),
        axis.ticks = element_blank(),
        panel.grid  = element_blank())

和情节:

饼图示例

标签: rggplot2

解决方案


我猜您希望在每个月内计算百分比。然后你可能想按月份分组并总结。

group_by(dist.newseek, Month) %>%
  mutate(percent=NewSeekers / sum(NewSeekers)) %>%
  ggplot(aes(x="", y=percent, fill=District)) +
  geom_bar(stat = "identity") +
  facet_wrap(~ Month, ncol=3) +
  coord_polar("y", start=0) + 
  geom_text(aes(label = paste0(NewSeekers,
                             "\n (",
                             scales::percent(percent, accuracy=1),
                             ")")),
          position = position_stack(vjust = 0.5), size=1.5) +
  theme(axis.text = element_blank(),
        axis.ticks = element_blank(),
        panel.grid  = element_blank(),
        text=element_text(size=10),
        legend.position = c(0.7, 0.3)) +
  labs(x="", y="") +
  guides(fill=guide_legend(ncol=2))

在此处输入图像描述


推荐阅读