首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >仅包含汇总数据的同一图上的ggplot2饼和甜甜圈

仅包含汇总数据的同一图上的ggplot2饼和甜甜圈
EN

Stack Overflow用户
提问于 2020-02-07 22:23:46
回答 1查看 367关注 0票数 0

我正在尝试从如here所示的moonBook包中生成类似于此图表的内容

我的数据涉及几十个组织的远程工作计划的参与度,我所有的数据都是总百分比,而不是个人回应。

因此,对于组织X,我知道:

代码语言:javascript
复制
Telework rate: 33%
No telework rate: 67%

对于那些进行远程工作的人,他们的频率是:

代码语言:javascript
复制
Very infrequently: 19.8%
1-2 days/month: 4.8%
1-2 days/week: 7.9%
3-4 days/week: 0.4%
Every work day: 0.1%

对于那些不远程办公的人来说,他们的理由是:

代码语言:javascript
复制
Must be physically present: 11.5%
Technical limitations: 7.1%
Not authorized: 25.5%
Choose not to: 23.9%

我想让中心的饼显示给定组织的远程工作/非远程工作突破,而外部的甜甜圈显示频率/原因。但我不知道如何继续,因为我发现的几乎每一个例子都是从个人回应开始的。

EN

回答 1

Stack Overflow用户

发布于 2020-02-08 00:22:23

这里有一个解决方案,可能真的很接近你正在寻找的。

我使用了一些与您展示的数据类似的数据:

代码语言:javascript
复制
df<-data.frame(Org=c("Tele",rep(c("Tele","NotTele"),each=4)),
                     CatFreq = LETTERS[1:9],
               Freq = c(19.8,4.8,7.9,0.4,0.1,11.5,7.1,25.5,22.9))

代码的其余部分

代码语言:javascript
复制
library(tidyverse)

df %>%
  #Calculate cumulative sums
  mutate(cumsum = cumsum(Freq),
         min = cumsum-Freq) %>%
  #Calculate cumulative sums by Org
  group_by(Org) %>%
  mutate(cumsum2 = max(cumsum(Freq))) %>%
  ungroup() %>%
  #Get mid point to print labels for Org levels
  mutate(mid = ifelse(Org == "Tele", cumsum2 / 2, min(cumsum2) + (max(cumsum2)/2) ) ) %>%
  #Do the ggplot by doing two geom_rect, 1 for Org, 1 for CatFreq
  ggplot()+
  geom_rect(aes(xmin = 1,
                xmax = 2,
                ymin = min, 
                ymax = cumsum,
                fill = Org))+
  #Add labels for Org
  geom_text(aes(x = 1 + ((2 - 1)/2), 
                y = mid,
                label = paste(Org, paste0(cumsum2, "%"), sep= "\n"))) +
  geom_rect(aes(xmin = 2,
                xmax = 3,
                ymin = min, 
                ymax = cumsum,
                fill = CatFreq))+
  #Add labels for CatFreq
  geom_text(aes(x = 2 + ((3 - 2)/2), 
                y = min + ((cumsum - min)/2),
                label = paste(CatFreq, paste0(Freq, " %"), sep= "\n"))) +
  #Changing the plot to pie
  coord_polar("y", start = 0) +
  #Making it a doughnut
  xlim(c(0,4)) + 
  #Set theme void
  theme_void() +
  #Eliminate legend
  theme(legend.position = "none")

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60115324

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档