【发布时间】:2021-05-31 17:33:06
【问题描述】:
是否可以在一张图中绘制多个甜甜圈图。
我有一个代码,它绘制了一个甜甜圈图,但是,是否可以通过year(day) 列将它们分开。结果一共会显示4个甜甜圈图?
library(ggplot2)
donut<-structure(list(`year(day)` = c(2018, 2018, 2018, 2018, 2018,
2019, 2019, 2019, 2019, 2019, 2020, 2020, 2020, 2020, 2020, 2021,
2021, 2021, 2021, 2021), kind = c("Audi", "BMW", "Skoda", "Ford",
"MB", "Audi", "BMW", "Skoda", "Ford", "MB", "Audi", "BMW", "Skoda",
"Ford", "MB", "Audi", "BMW", "Skoda", "Ford", "MB"), TOTL = c(82043.56,
22908.66, 135925.42, 50448.36, 762679.18, 83680.1538461538, 35655.2115384615,
95892.4807692308, 57961.5, 726726.423076923, 162654.5, 41344.6153846154,
110685.038461538, 40149.4615384615, 664138.153846154, 355072.729926464,
66555.3261904762, 102378.893809524, 55646.5438095238, 475979.351831226
)), row.names = c(NA, -20L), groups = structure(list(`year(day)` = c(2018,
2019, 2020, 2021), .rows = structure(list(1:5, 6:10, 11:15, 16:20), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, 4L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
donut$fraction <- donut$NETP / sum(donut$NETP)
donut$ymax <- cumsum(donut$fraction)
donut$ymin <- c(0, head(donut$ymax, n=-1))
donut$labelPosition <- (donut$ymax + donut$ymin)/2
donut$label <- paste0(donut$kind, "\n NETP: ", donut$NETP)
ggplot(donut, aes(ymax=ymax, ymin=ymin, xmax=7, xmin=4, fill=kind)) +
geom_rect() +
geom_text( x=10, aes(y=labelPosition, label=label, color=kind), size=4.5, fontface="bold") + # x here controls label position (inner / outer)
#scale_fill_brewer(palette=5) +
#scale_color_brewer(palette=5) +
coord_polar(theta="y") +
xlim(c(-3, 10)) +
theme_void()
【问题讨论】: