r - 如何为一个小节居中饼图和嵌套饼图?
问题描述
我正在尝试绘制嵌套饼图。但是,我没有得到完整的饼图。馅饼的顶部和底部缺失。
例如,我只对在嵌套饼图中扩展“农业”感兴趣。我已经计算了每个的大小百分比
我对饼图感兴趣,如下所示
这是我的工作脚本。
library(ggplot2)
library(plotrix)
df <- structure(list(parent = c("Energy", "Industry", "Transport", "Buildings ", "Agriculture", "Agriculture", "Agriculture", "Agriculture", "Agriculture", "Agriculture", "Agriculture", "Forestry and Land Use " ), node = c("Energy", "Industry", "Transport", "Buildings ", "Enteric Fermentation", "Manure Left on Pasture", "Synthetic Fertilizers", "Paddy Rice", "Manure Management ", "Burning of savannahs", "Other", "Forestry and Land Use "), size = c(35, 21, 14, 6, 4.8, 1.92, 1.56, 1.2, 0.84, 0.6, 1.08, 12)), row.names = c(NA, -12L), class = c("data.table", "data.frame"))
# aggregate data for the df pie chart
df_data <-
aggregate(df$size,
by = list(df = df$parent),
FUN = sum)
# order sub_data data by df so it will line up with df pie chart
sub_data <- df[order(df$node), ]
df_colors <- c('#85EA72', '#3B3B3F', '#71ACE9', '#747AE6', '#F69852','#F69992')
# adjust these as desired (currently colors all subdatas the same as df)
sub_data_colors <-
c('#85EA72', '#3B3B3F', '#71ACE9', '#747AE6', '#F69852','#F69992', '#85EA88', '#3B3B4F', '#71ACC9', '#747EE6', '#F69899','#F68882')
# format labels to display subdata and % market size
sub_data_labels <- paste(sub_data$sub_data, ": ", sub_data$size, "%", sep = "")
# coordinates for the center of the chart
center_x <- 0.5
center_y <- 0.5
plot.new()
# draw sub_data pie chart first
sub_data_chart <-
floating.pie(
xpos = center_x,
ypos = center_y,
x = sub_data$size,
radius = 0.35,
border = "white",
col = sub_data_colors
)
# add labels for sub_data pie chart
pie.labels(
x = center_x,
y = center_y,
angles = sub_data_chart,
labels = sub_data_labels,
radius = 0.38,
bg = NULL,
cex = 0.8,
font = 2,
col = "gray40"
)
# overlay df pie chart
df_chart <-
floating.pie(
xpos = center_x,
ypos = center_y,
x = df_data$x,
radius = 0.25,
border = "white",
col = df_colors
)
# add labels for df pie chart
pie.labels(
x = center_x,
y = center_y,
angles = df_chart,
labels = df_data$df,
radius = 0.125,
bg = NULL,
cex = 0.8,
font = 2,
col = "white"
)
有人可以帮助我找出错误吗?谢谢你。
解决方案
关于缺失的边缘,只需将“绘图”选项卡增加到所需的大小。
关于拆分的小节,我的解决方案基本上是将所有其他部分设置为白色,这样您就不会在白色背景上看到它们。
library(ggplot2)
library(plotrix)
library(data.table)
df <- structure(list(parent = c("Energy", "Industry", "Transport", "Buildings ",
"Agriculture", "Agriculture", "Agriculture",
"Agriculture", "Agriculture", "Agriculture",
"Agriculture", "Forestry and Land Use " ),
node = c("Energy", "Industry", "Transport", "Buildings ",
"Enteric Fermentation", "Manure Left on Pasture",
"Synthetic Fertilizers", "Paddy Rice",
"Manure Management ", "Burning of savannahs",
"Other", "Forestry and Land Use "),
size = c(35, 21, 14, 6, 4.8, 1.92, 1.56,
1.2, 0.84, 0.6, 1.08, 12)),
row.names = c(NA, -12L),
class = c("data.table", "data.frame"))
# aggregate data for the df pie chart
df_data <-
aggregate(df$size,
by = list(df = df$parent),
FUN = sum)
# order sub_data data by df so it will line up with df pie chart
sub_data <- df[order(df$node), ]
df_colors <- c('#85EA72', '#3B3B3F', '#71ACE9', '#747AE6', '#F69852','#F69992')
# adjust these as desired (currently colors all subdatas the same as df)
sub_data_colors <-
c('#85EA72', '#3B3B3F', '#71ACE9', '#747AE6', '#F69852','#F69992', '#85EA88',
'#3B3B4F', '#71ACC9', '#747EE6', '#F69899','#F68882')
# format labels to display subdata and % market size
sub_data_labels <- paste(sub_data$sub_data, ": ", sub_data$size, "%", sep = "")
# coordinates for the center of the chart
center_x <- 0.5
center_y <- 0.5
# colors should always match with the group
sub_data$color <- sub_data_colors
# set all other groups to white. Hence, they are not shown in the plot
sub_data[parent != "Agriculture", color := "#ffffff"]
# correct order is important
data.table::setorder(sub_data, -parent)
# Enlarge the "Plots" tab to the needed size
plot.new()
# draw sub_data pie chart first
sub_data_chart <-
floating.pie(
xpos = center_x,
ypos = center_y,
x = sub_data$size,
radius = 0.35,
border = "white",
col = sub_data$color
)
# add labels for sub_data pie chart
pie.labels(
x = center_x,
y = center_y,
angles = sub_data_chart,
labels = sub_data_labels,
radius = 0.38,
bg = NULL,
cex = 0.8,
font = 2,
col = "gray40"
)
data.table::setorder(df_data, -df)
# overlay df pie chart
df_chart <-
floating.pie(
xpos = center_x,
ypos = center_y,
x = df_data$x,
radius = 0.25,
border = "white",
col = df_colors
)
# add labels for df pie chart
pie.labels(
x = center_x,
y = center_y,
angles = df_chart,
labels = df_data$df,
radius = 0.125,
bg = NULL,
cex = 0.8,
font = 2,
col = "white"
)
推荐阅读
- cmake - Cmake 创建 Makefile 支持不同配置的调试和发布
- javascript - NodeJS [错误] - 未定义选项属性未定义的值未定义超出范围
- javascript - setState() 不工作,错误:超过最大更新深度
- uipath - UiPath - 鼠标悬停菜单中的活动“单击”
- html - 即使有跨度,如何将输入标签推到中间,因为其他输入标签是?
- pine-script - 盈亏平衡交易以负号(松树)关闭
- ios - ForEach 两个数组 - 可能的简单解决方案?
- python - 根据条件从 pandas DF 中检索不同值的数量
- css - :global in styled-jsx 如何在 React.js 中工作?
- javascript - 如何使用 Javascript 在网站上获取 Sticky 标头?