Xop*_*ter 5 r ggplot2 pie-chart donut-chart sunburst-diagram
我正在尝试创建一个两级旭日/圆环图(用于打印),其中第二级是第一级的详细视图。我已阅读并理解本教程,但我是 R 和 ggplot2 新手,在生成第二个级别时遇到困难。在前面提到的文章中,根级别只有一个元素(有点多余),而我的根有很多元素;其中,二级至少有1个,最多10个要素。
假设我的数据有三列:name、type和value; 其中name和type分别定义根元素和第二层元素。每个都name恰好有一个type,它是跨 s 的 sall的总和(其中,至少有一个,并且跨s 的集合可能相交或互斥)。例如:valuetypenametype
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
Run Code Online (Sandbox Code Playgroud)
我可以使用以下方法创建根级别堆栈(在转换为极坐标之前):
data.all <- data[data$type == "all",]
ggplot(data.all, aes(x=1, y=data.all$value, fill=data.all$name)) + geom_bar(stat="identity")
Run Code Online (Sandbox Code Playgroud)
我对第二级堆栈的需要是使type值在值内对齐name,与它们的值成比例:
+-----+ +-------+
| | | type3 |
| baz | +-------+
| | | type1 |
+-----+ +-------+
| | | |
| bar | | type3 |
| | | |
+-----+ +-------+
| | | type2 |
| foo | +-------+
| | | type1 |
-+-----+--+-------+-
Run Code Online (Sandbox Code Playgroud)
(注意,这显然不是按比例绘制的!)
我还需要对值进行一致的着色(例如,和的块type的颜色type1应该相同,等等)foobaz
我想我可以通过将name和type列组合成一个新列,然后按以下方式着色来做到这一点:
data.other <- data[data$type != "other",]
data.other$comb <- paste(data.other$name, data.other$type, sep=":")
ggplot(data.other, aes(x=2, y=data.other$value, fill=data.other$comb)) + geom_bar(stat="identity")
Run Code Online (Sandbox Code Playgroud)
然而,这破坏了颜色的一致性——显然,事后看来——而且,有趣的是,我绝对不相信对齐会是正确的。
我的 R/ggplot2 诞生可能非常明显(抱歉!);我怎样才能实现我想要的目标?
编辑我也遇到过这个问题和答案,但是我的数据看起来与他们的不同。如果我的数据可以被整合成相同的形状——我不知道该怎么做——那么我的问题就变成了他们的特例。
这可能只是其中的一部分,并且可能无法很好地扩展到更复杂的数据集。我对如何做到这一点非常好奇,并且有一个类似的更大的数据集,我正在尝试可视化工作,所以这实际上也帮助我完成了我的工作:)
基本上我所做的是将数据集分成三个级别的数据帧:父级别基本上是虚拟数据,级别 1 df 包含每个名称下所有类型的总和(我想我可以过滤你的数据 - 我type == "all"没有我的工作数据没有类似的列),第 2 级是所有外部节点。将它们全部绑定在一起,制作一个堆积条形图,并为其指定极坐标。
我为工作做的标签有很多,而且很长,所以我用了ggrepel::geom_text_repel标签。它们很快就变得笨重且丑陋。
显然这里的美学还有一些不足之处,但我认为它可以根据你的喜好进行美化。
library(tidyverse)
df <- "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" %>% read_table2() %>%
filter(type != "all") %>%
mutate(name = as.factor(name) %>% fct_reorder(value, sum)) %>%
arrange(name, value) %>%
mutate(type = as.factor(type) %>% fct_reorder2(name, value))
lvl0 <- tibble(name = "Parent", value = 0, level = 0, fill = NA)
lvl1 <- df %>%
group_by(name) %>%
summarise(value = sum(value)) %>%
ungroup() %>%
mutate(level = 1) %>%
mutate(fill = name)
lvl2 <- df %>%
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()
Run Code Online (Sandbox Code Playgroud)

由reprex 包(v0.2.0) 于 2018-04-24 创建。