gpt4 book ai didi

r - 带标签的多级饼图

转载 作者:行者123 更新时间:2023-12-04 12:10:58 24 4
gpt4 key购买 nike

我在 Rappid ( https://resources.jointjs.com/docs/rappid/v2.2/shapes.html#shapes.chart.pie ) 上找到了这张图片,并想用我自己的数据使用 R 来模拟它。我对图例和标签特别感兴趣,因为相关问题不包括 ( Multi-level Pie Chart in R )

labeled multi-level pie chart from Rappid site

下面是一些示例代码:

df <- data.frame(year = c(2014, 2014, 2014, 2014, 2014, 2013, 2013, 2013, 2013, 2013, 2012, 2012, 2012, 2012, 2012),
browser = c("IE", "Firefox", "Chrome", "Safari", "Opera","IE", "Firefox", "Chrome", "Safari","Opera", "IE", "Firefox", "Chrome", "Safari", "Opera"),
c = c("20.3", "18.3", "34.2", "17.8", "2.7", "27.5", "20.0","30.0", "14.8", "2.3", "30.9", "24.8", "24.6", "6.5","2.5"))

最佳答案

这是一个带有 ggplot2 的堆叠饼图。每年数据中的百分比加起来都没有达到 100%,所以为了这个例子的目的,我将它们缩放到 100%(如果你的真实数据没有用完,你可以改为添加一个“其他”类别所有选项)。我也更改了列的名称 ccc , 自 c是一个 R 函数。

library(tidyverse)

# Convert cc to numeric
df$cc = as.numeric(as.character(df$cc))

# Data for plot
pdat = df %>%
group_by(year) %>%
mutate(cc = cc/sum(cc)) %>%
arrange(browser) %>%
# Get cumulative value of cc
mutate(cc_cum = cumsum(cc) - 0.5*cc) %>%
ungroup

ggplot(pdat, aes(x=cc_cum, y=year, fill=browser)) +
geom_tile(aes(width=cc), colour="white", size=0.4) +
geom_text(aes(label=sprintf("%1.1f", 100*cc)), size=3, colour="white") +
geom_text(data=pdat %>% filter(year==median(year)), size=3.5,
aes(label=browser, colour=browser), position=position_nudge(y=0.5)) +
scale_y_continuous(breaks=min(pdat$year):max(pdat$year)) +
coord_polar() +
theme_void() +
theme(axis.text.y=element_text(angle=0, colour="grey40", size=9),
axis.ticks.y=element_line(),
axis.ticks.length=unit(0.1,"cm")) +
guides(fill=FALSE, colour=FALSE) +
scale_fill_manual(values=hcl(seq(15,375,length=6)[1:5],100,70)) +
scale_colour_manual(values=hcl(seq(15,375,length=6)[1:5],100,50))

enter image description here

您还可以使用堆积条形图,这可能更清楚:
ggplot(pdat, aes(x=cc_cum, y=year, fill=browser)) +
geom_tile(aes(width=cc), colour="white") +
geom_text(aes(label=sprintf("%1.1f", 100*cc)), size=3, colour="white") +
geom_text(data=pdat %>% filter(year == min(year)), size=3.2,
aes(label=browser, colour=browser), position=position_nudge(y=-0.6)) +
scale_y_continuous(breaks=min(df$year):max(df$year)) +
scale_x_continuous(expand=c(0,0)) +
theme_void() +
theme(axis.text.y=element_text(angle=0, colour="grey40", size=9),
axis.ticks.y=element_line(),
axis.ticks.length=unit(0.1,"cm")) +
guides(fill=FALSE, colour=FALSE) +
scale_fill_manual(values=hcl(seq(15,375,length=6)[1:5],100,70)) +
scale_colour_manual(values=hcl(seq(15,375,length=6)[1:5],100,50))

enter image description here

线图可能是最清晰的:
library(scales)

ggplot(pdat, aes(year, cc, colour=browser)) +
geom_line() +
geom_label(aes(label=sprintf("%1.1f", cc*100)), size=3,
label.size=0, label.padding=unit(1,"pt"), , colour="white") +
geom_text(aes(label=sprintf("%1.1f", cc*100)), size=3) +
geom_text(data=pdat %>% filter(year==max(year)),
aes(label=browser), hjust=0, nudge_x=0.08, size=3) +
theme_classic() +
expand_limits(x=max(pdat$year) + 0.3, y=0) +
guides(colour=FALSE) +
scale_x_continuous(breaks=min(pdat$year):max(pdat$year)) +
scale_y_continuous(labels=percent)

enter image description here

关于r - 带标签的多级饼图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48588312/

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