如何在r中绘制不同月份的比较

问题描述 投票:0回答:1

[我试图通过电晕时期来显示新求职者的增长。因此,我从完整的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")

[我正在寻找最好的方法来显示一月至四月间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())

和情节:

Pie chart example

感谢您的帮助

r ggplot2 pie-chart
1个回答
0
投票

尝试一下。问题是您在y上映射了求职者的数量,而不是百分比。只需计算百分比并将其映射到y

library(dplyr)
library(ggplot2)

dist.newseek <- Cdata %>% 
  group_by(Month,District) %>% 
  summarise(NewSeekers = sum(NewSeekers)) %>% 
  # Compute percentages
  mutate(NewSeekersPct = NewSeekers / sum(NewSeekers))
#> `summarise()` regrouping output by 'Month' (override with `.groups` argument)

ggplot(dist.newseek, aes(x="", y=NewSeekersPct, group=District, color=District, fill=District)) +
  geom_bar(width = 1, stat = "identity") +
  geom_text(aes(label = paste0(NewSeekers,
                               "\n(",
                               # Use the computed pct
                               scales::percent(NewSeekersPct, accuracy = .1),
                               ")")),
            position = position_stack(vjust = 0.5), color = "white") +
  coord_polar("y", start=0) + 
  facet_wrap(~ Month) +
  theme(axis.text = element_blank(),
        axis.ticks = element_blank(),
        panel.grid  = element_blank())

“”


0
投票

我想您希望每个月都计算百分比。然后,您可能想按月份分组并汇总。

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,
                             " (",
                             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))

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.