有没有什么简单的方法可以结合ggplot2中的镶嵌面,或者结合华夫饼包来创建华夫饼图?
例如,用100个方块替换下面的每一条,以表示1%。
ggplot(mtcars, aes(x = factor(vs), y = hp, fill = as.factor(carb))) +
geom_bar(stat = 'identity', position = 'fill') +
facet_wrap('gear')
发布于 2019-03-29 05:38:25
这里是一种仅使用tidyverse
(即dplyr
、tidyr
和ggplot2
)来创建华夫饼图或方形饼图的替代方法。它是基于hrbrmstr's answer的,但我试图让它更通用一些;任何频率表都可以作为输入,并且很容易调整华夫饼的尺寸(例如,矩形而不是正方形)。
library(tidyverse)
freq_table = mtcars %>%
count(gear, vs, carb, wt = hp) %>%
group_by(gear, vs) %>%
mutate(pct = n / sum(n)) %>%
select(gear, vs, carb, pct)
第二步创建坐标。使用tidyr::expand()
而不是waffleize()
。仍在使用smart_round()
。
waffle.n = 100 # Number of blocks
waffle.cols = ceiling(sqrt(waffle.n)) # For square. Otherwise pick integer.
coordinates = freq_table %>%
group_by(gear, vs) %>%
mutate(waffle.num = smart_round(pct,1) * waffle.n) %>%
group_by(carb, gear, vs) %>%
expand(count = seq(1:waffle.num)) %>%
select(-count) %>%
group_by(gear, vs) %>%
arrange(gear, vs) %>%
mutate(
waffle.x = rep_len(1:waffle.cols, waffle.n),
waffle.y = floor((row_number() - 1) / waffle.cols)
)
I通过两个变量(gear
和vs
)进行分组,因此使用facet_grid()
。如果按单个变量分组,则使用facet_wrap()
。您需要稍微调整选项以获得最佳结果(例如设备的大小或点的大小和笔划)。
fig = coordinates %>%
ggplot(aes(x = waffle.x, y = waffle.y, fill = as.factor(carb))) +
geom_point(size = 7, shape = 22, color = "white", stroke = 0.8) +
#geom_raster() + # Alternative to geom_point() without gap between blocks.
facet_grid(rows = vars(gear), cols = vars(vs)) +
theme_void() +
theme(legend.position = "bottom", plot.margin = margin(5.5, 5.5, 5.5, 5.5, "pt"), panel.spacing = unit(15, "pt"))
fig
#ggsave("fig.pdf", width = 13, height = 17.5, units = "cm", dpi = 150)
数据块数量不均匀且份额不能被10整除的A more interesting example。
https://stackoverflow.com/questions/52741666
复制相似问题