r

您所在的位置:网站首页 双环饼图怎么做 r

r

2024-07-09 15:00| 来源: 网络整理| 查看: 265

我正在尝试创建一个两级旭日/圆环图(用于打印),其中第二级是第一级的详细 View 。我已阅读并理解 this tutorial ,但我是 R 和 ggplot2 新手,在生成第二个级别时遇到困难。在前面提到的文章中,根级别只有一个元素(有点多余),而我的根有很多元素;其中,二级至少有1个,最多10个元素。

假设我的数据有三列:name、type 和 value;其中 name 和 type 分别定义根元素和第二层元素。每个名称都具有all中的一种类型,它是跨的值的总和type(其中至少有一个,并且在 name 中,type 的集合可以交叉或互斥)。例如:

name type value ----- ------- ------ foo all 444 foo type1 123 foo type2 321 bar all 111 bar type3 111 baz all 999 baz type1 456 baz type3 543

我可以使用以下方法创建根级堆栈(在转换为极坐标之前):

data.all % fct_reorder(value, sum)) %>% arrange(name, value) %>% mutate(type = as.factor(type) %>% fct_reorder2(name, value)) lvl0 % summarise(value = sum(value)) %>% ungroup() %>% mutate(level = 1) %>% mutate(fill = name) lvl2 % select(name = type, value, fill = name) %>% mutate(level = 2) bind_rows(lvl0, lvl1, lvl2) %>% mutate(name = as.factor(name) %>% fct_reorder2(fill, value)) %>% arrange(fill, name) %>% mutate(level = as.factor(level)) %>% ggplot(aes(x = level, y = value, fill = fill, alpha = level)) + geom_col(width = 1, color = "gray90", size = 0.25, position = position_stack()) + geom_text(aes(label = name), size = 2.5, position = position_stack(vjust = 0.5)) + coord_polar(theta = "y") + scale_alpha_manual(values = c("0" = 0, "1" = 1, "2" = 0.7), guide = F) + scale_x_discrete(breaks = NULL) + scale_y_continuous(breaks = NULL) + scale_fill_brewer(palette = "Dark2", na.translate = F) + labs(x = NULL, y = NULL) + theme_minimal()

由 reprex package (v0.2.0) 于 2018 年 4 月 24 日创建。

关于r - 使用 ggplot2 的多个依赖级别旭日图/圆环图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50004058/



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3