我一直在寻找一种方法来使用 ggplot 进行这种类型的绘图。 @camille 的回答真的很有帮助!我最终使用this answer here too 为这个问题创建了一个稍微修改的答案。
已经快一年了,但也许其他人还在寻找这种答案!也许其他答案中提到的其他包更有用,但对于我们这些想要留在 ggplot 中的人来说,希望这能有所帮助。
我认为我可以按照 OP 的要求去做(始终如一地为第二层着色),尽管我不确定这是最佳的方式。
我没有使用geom_col,而是使用了geom_rect。这为我们提供了更大的灵活性,并且还可以更好地控制每个矩形的绘制位置(堆叠的条形图总是存在堆叠条形图的问题)。此外,奇怪的是,在极坐标 geom_col 中,最终绘制了从 0 到 x 的所有饼图。所以@camille 不得不使用填充的透明度来获得想要的结果。在geom_rect 中,我们可以设置xmin 和xmax 以获得我们想要的确切形状。
但我们需要做一些数据处理以使数据帧成形。
另外,我试图制作的情节有一些第二层是空的。因此,我稍微更改了数据集,以包含一个额外的一级类,而没有二级类。
这是我的解决方案:
library(tidyverse)
library(ggplot2)
library(RColorBrewer)
df <- "name type value
foo all 444
foo type1 123
foo type2 321
bar all 111
bar type3 111
baz all 999
baz type1 456
baz type3 543
boz - 222" %>% read_table2() %>% filter(type != 'all') %>%
mutate(type=ifelse(type=='-', NA, type)) %>% arrange(name, value)
# here I create the columns xmin, xmax, ymin, ymax using cumsum function
# (be VERY careful with ordering of rows!)
# I also created a column 'colour' which I map to the asthetic 'colour' (colour of line of each rectangle)
# it is a boolean saying if a line should or should not be drawn.
# for empty second levels i want to draw an empty space (no fill and no line)
# define a padding space between the levels of the pie chart
padding <- 0.05
# create df for level 0
lvl0 <- tibble(name = "Parent", value = 0, level = 0, fill = NA) %>%
mutate(xmin=0, xmax=1, ymin=0, ymax=value) %>%
mutate(x.avg=0, y.avg=0, colour=FALSE)
print(lvl0)
# create df for level 1
lvl1 <- df %>%
group_by(name) %>%
summarise(value = sum(value)) %>%
ungroup() %>%
mutate(level = 1) %>%
mutate(fill = name) %>%
mutate(xmin=1+padding, xmax=2, ymin=0, ymax=cumsum(value)) %>%
mutate(ymin=lag(ymax, default=0),
x.avg=(xmin+xmax)/2,
y.avg=(ymin+ymax)/2,
colour=TRUE)
print(lvl1)
# create df for level 2
lvl2 <- df %>%
select(name = type, value, fill = name) %>%
mutate(level = 2) %>%
mutate(fill=paste0(fill, '_', name)) %>%
mutate(xmin=2+padding, xmax=3, ymin=0, ymax=cumsum(value)) %>%
mutate(ymin=lag(ymax, default=0),
x.avg=(xmin+xmax)/2,
y.avg=(ymin+ymax)/2,
colour=ifelse(grepl('_NA', fill), FALSE, TRUE))
print(lvl2)
# this is my dirty workaround for defining the colours of levels 1 one 2 independently. Probably not the best way and
# maybe it will not scale very well... But for this small data set it seemed to work...
# number of classes in each level (don't include NA)
n.classes.1 <- 4
n.classes.2 <- 3
n.classes.total <- n.classes.1 + n.classes.2
# get colour pallete for level 1
col.lvl1 <- brewer.pal(n.classes.total,"Dark2")[1:n.classes.1]
names(col.lvl1) <- as.character(unique(lvl1$name))
# get colour pallete for level 2 (don't include NA)
col.lvl2 <- brewer.pal(n.classes.total,"Dark2")[(n.classes.1+1):n.classes.total]
names(col.lvl2) <- as.character(unique(lvl2$name)[!is.na(unique(lvl2$name))])
# compile complete color pallete
fill.pallete <- c(col.lvl1)
for (l1 in as.character(unique(lvl1$name))) {
for (l2 in as.character(unique(lvl2$name))) {
if (!is.na(l2)) {
name.type <- paste0(l1, '_', l2)
aux <- col.lvl2[l2]
names(aux) <- name.type
fill.pallete <- c(fill.pallete, aux)
} else {
# if level2 is NA, then assign transparent colour
name.type <- paste0(l1, '_NA')
aux <- NA
names(aux) <- name.type
fill.pallete <- c(fill.pallete, aux)
}
}
}
print(fill.pallete)
# put all data frames together for ggplot
df.total <- bind_rows(lvl0, lvl1, lvl2) %>%
mutate(name = as.factor(name) %>% fct_reorder2(fill, value)) %>%
arrange(fill, name) %>%
mutate(level = as.factor(level))
print(df.total)
# create plot (it helped me to look at the rectangular coordinates first before changing to polar!)
g <- ggplot(data=df.total, aes(fill = fill)) +
geom_rect(aes(ymax=ymax, ymin=ymin, xmax=xmax, xmin=xmin, colour=colour), size = 0.1) +
scale_fill_manual(values = fill.pallete, , guide = F, na.translate = FALSE) +
scale_color_manual(values = c('TRUE'='gray20', 'FALSE'='#FFFFFF00'),
guide = F, na.translate = FALSE) +
geom_text(aes(x = x.avg, y = y.avg, label = name), size = rel(2.5)) +
scale_x_discrete(breaks = NULL) +
scale_y_continuous(breaks = NULL) +
labs(x = NULL, y = NULL) +
theme_minimal() +
theme(panel.grid=element_blank()) +
coord_polar(theta = "y", start = 0, direction = -1)
print(g)
This is the resulting plot.