在R中创建一个具有3个级别的环形图。

huangapple go评论77阅读模式
英文:

Donut chart with 3 levels in R

问题

尝试复制以下帖子中的环形图(第一个答案:https://stackoverflow.com/questions/50004058/multiple-dependent-level-sunburst-doughnut-chart-using-ggplot2/76687651#76687651),寻找一个类似于该图的环形图的解决方案:

df <- "name  type    value
Total   all     100
Planerl&#246;se   Total 54
Total   all   100
Abgeltung   Thurgau   30.24
Abgeltung   Bund     23.8
Abgeltung   Kanton Thurgau Netto   20.23
Abgeltung   Gemeinden   9.98" %>%
  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)

lvl3 <- df %>%
  select(name = type, value, fill = name) %>%
  mutate(level = 3)

bind_rows(lvl0, lvl1, lvl2, lvl3) %>%
  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, "3"=0.4), 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()

感谢您的帮助!

英文:

trying to replicate the donut chart in the following post (first answer: https://stackoverflow.com/questions/50004058/multiple-dependent-level-sunburst-doughnut-chart-using-ggplot2/76687651#76687651) looking for a solution with a donut chart that looks more or less like that:
在R中创建一个具有3个级别的环形图。
How would I need to adapt the following code to get such a plot:

df &lt;- &quot;name  type    value
Total   all     100
Planerl&#246;se   Total 54
Total   all   100
Abgeltung   Thurgau   30.24
Abgeltung   Bund     23.8
Abgeltung   Kanton Thurgau Netto   20.23
Abgeltung   Gemeinden   9.98&quot; %&gt;% read_table2() %&gt;%
filter(type != &quot;all&quot;) %&gt;%
mutate(name = as.factor(name) %&gt;% fct_reorder(value, sum)) %&gt;%
arrange(name, value) %&gt;%
mutate(type = as.factor(type) %&gt;% fct_reorder2(name, value))
lvl0 &lt;- tibble(name = &quot;Parent&quot;, value = 0, level = 0, fill = NA)
lvl1 &lt;- df %&gt;%
group_by(name) %&gt;%
summarise(value = sum(value)) %&gt;%
ungroup() %&gt;%
mutate(level = 1) %&gt;%
mutate(fill = name)
lvl2 &lt;- df %&gt;%
select(name = type, value, fill = name) %&gt;%
mutate(level = 2)
lvl3 &lt;- df %&gt;%
select(name = type, value, fill = name) %&gt;%
mutate(level = 3)
bind_rows(lvl0, lvl1, lvl2, lvl3) %&gt;%
mutate(name = as.factor(name) %&gt;% fct_reorder2(fill, value)) %&gt;%
arrange(fill, name) %&gt;%
mutate(level = as.factor(level)) %&gt;%
ggplot(aes(x = level, y = value, fill = fill, alpha = level)) +
geom_col(width = 1, color = &quot;gray90&quot;, size = 0.25, position = position_stack()) +
geom_text(aes(label = name), size = 2.5, position = position_stack(vjust = 0.5)) +
coord_polar(theta = &quot;y&quot;) +
scale_alpha_manual(values = c(&quot;0&quot; = 0, &quot;1&quot; = 1, &quot;2&quot; = 0.7, &quot;3&quot;=0.4), guide = F) +
scale_x_discrete(breaks = NULL) +
scale_y_continuous(breaks = NULL) +
scale_fill_brewer(palette = &quot;Dark2&quot;, na.translate = F) +
labs(x = NULL, y = NULL) +
theme_minimal()

Thank you for any help!

答案1

得分: 1

这里是您发布的代码的简化版本,生成一个三层环形图:

df <- read.table(text="name  type    value
Planerlöse   Total 54
Abgeltung   Thurgau   30.24
Abgeltung   Bund     23.8
Abgeltung   'Kanton Thurgau Netto'   20.23
Abgeltung   Gemeinden   9.98", header=T)

lvl0 <- tibble(name = "Parent", value = 0, level = 0, fill = NA)

lvl1 <- df %>%
  group_by(name) %>%
  summarise(value = sum(value)) %>%
  ungroup() %>%
  mutate(level = 1, 
         fill = name)

lvl2 <- df %>%
  select(name = type, value, fill = name) %>%
  mutate(level = 2)

lvl3 <- df %>%
  select(name = type, value, fill = name) %>%
  mutate(level = 3)

bind_rows(lvl0, lvl1, lvl2, lvl3) %>%
  mutate(fill = ifelse(is.na(fill), "Total", fill),
         name = as.factor(name),
         level = as.factor(level)) %>%
  arrange(fill, name) %>%
  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, "3"=0.4), 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()

在R中创建一个具有3个级别的环形图。


<details>
<summary>英文:</summary>
Here&#39;s a simplified version of the code you posted, which generates a three layered donut chart:

df <- read.table(text="name type value
Planerlöse Total 54
Abgeltung Thurgau 30.24
Abgeltung Bund 23.8
Abgeltung 'Kanton Thurgau Netto' 20.23
Abgeltung Gemeinden 9.98", header=T)

lvl0 <- tibble(name = "Parent", value = 0, level = 0, fill = NA)

lvl1 <- df %>%
group_by(name) %>%
summarise(value = sum(value)) %>%
ungroup() %>%
mutate(level = 1,
fill = name)

lvl2 <- df %>%
select(name = type, value, fill = name) %>%
mutate(level = 2)

lvl3 <- df %>%
select(name = type, value, fill = name) %>%
mutate(level = 3)

bind_rows(lvl0, lvl1, lvl2, lvl3) %>%
mutate(fill = ifelse(is.na(fill), "Total", fill),
name = as.factor(name),
level = as.factor(level)) %>%
arrange(fill, name) %>%
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, "3"=0.4), 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()


[![donuts][1]][1]
[1]: https://i.stack.imgur.com/Za1JG.png
</details>

huangapple
  • 本文由 发表于 2023年7月17日 14:03:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701848.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定