gpt4 book ai didi

r - 使用 ggplot2 和分组变量创建圆环图

转载 作者:行者123 更新时间:2023-12-04 16:11:50 26 4
gpt4 key购买 nike

抱歉,如果这已经得到回答,但我已经尝试了许多帖子中的代码无济于事。我正在尝试在 ggplot 2 中制作 donut chart ,这对我来说是新的。它似乎在大多数情况下都有效,但并未将国家/地区分组在一起,因此每一行在饼图中都有自己的块,而不是将所有英国行放在一起(抱歉,如果这有点乱)。

下面是一些示例数据的代码(我实际上有 14 个国家和 1200 行):

  country <- c("Australia", "Australia", "China", "UK", "UK", "UK", "Australia", "New Zealand", "Hong Kong", "India", "India", "Korea", "Malaysia", "UK")
GAV <- c(32626614, 611751827, 1070038943.77, 1070038990, 611751347, 567751827, 444751827, 611751444, 999751827, 111751827, 222751827, 331751827, 611751844, 611777827)

panel_donut <- data.frame(country, GAV)

使用 NA GAV 删除行
panel_donut <- panel_donut[!is.na(panel_donut$GAV),]

计算百分比
panel_donut$percentage <-  panel_donut$GAV / sum(panel_donut$GAV)* 100

panel_donut <- panel_donut[rev(order(panel_donut$percentage)), ]

panel_donut$ymax <- cumsum(panel_donut$percentage)

panel_donut$ymin <- c(0, head(panel_donut$ymax, n = -1))

panel_donut

重新排序颜色级别
panel_donut <-  panel_donut[order(panel_donut$country), ]

绘制图表
library(ggplot2)
library(ggrepel)

donut <- ggplot(panel_donut, aes(fill = country, ymax = ymax, ymin = ymin, xmax = 100, xmin = 80)) +

geom_rect(colour = "black") +

coord_polar(theta = "y") +

xlim(c(0, 100)) +

theme(legend.title = element_text(colour = "black", size = 16, face = "bold"),
legend.text = element_text(colour = "black", size = 15),
panel.grid = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank())

donut

目前,我得到一个 donut chart ,但所有级别都有单独的块,即英国有 4 个 donut 块,而不是将其分组为 1 个。我想知道我的代码哪里出了问题导致这种情况发生.

在此先感谢您的帮助!

最佳答案

是的,您的主数据框有您所在国家/地区的多个条目。你需要总结它们。试试这个方法:

library(ggplot2)
library(ggrepel)
library(dplyr)

panel_donut %>%
group_by(country) %>%
summarise(percentage = sum(percentage)) %>%
mutate(ymax = cumsum(percentage),
ymin = c(0, head(ymax, n = -1))) %>%
ggplot(aes(fill = country, ymax = ymax, ymin = ymin, xmax = 100, xmin = 80)) +
geom_rect(colour = "black") +
coord_polar(theta = "y") +
xlim(c(0, 100)) +
scale_fill_brewer(palette = "Set1") +
theme(legend.title = element_text(colour = "black", size = 16, face = "bold"),
legend.text = element_text(colour = "black", size = 15),
panel.grid = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank())

输出是:

enter image description here

尽管我建议使用相同的库使用更简单的方法:
data.frame(country, GAV) %>% 
filter(!is.na(GAV)) %>%
mutate(percentage = GAV / sum(GAV) * 100) %>%
group_by(country) %>%
summarise(percentage = sum(percentage)) %>%
mutate(ymax = cumsum(percentage),
ymin = c(0, head(ymax, n = -1))) %>%
ggplot(aes(fill = country, ymax = ymax, ymin = ymin, xmax = 100, xmin = 80)) +
geom_rect(colour = "black") +
coord_polar(theta = "y") +
xlim(c(0, 100)) +
scale_fill_brewer(palette = "Set1", guide = "none") +
theme(legend.title = element_text(colour = "black", size = 16, face = "bold"),
legend.text = element_text(colour = "black", size = 15),
panel.grid = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank()) +
geom_label_repel(aes(label = paste(country, "\n", round(percentage, 1),"%"),
x = 100,
y = (ymin + ymax)/2),
inherit.aes = F,
show.legend = F, size = 3) +
annotate("text", x = 0, y = 0, size = 15, label = "Donut Chart")

输出:

enter image description here

关于r - 使用 ggplot2 和分组变量创建圆环图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55667701/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com