[英]ggplot, pie chart and different radii in facet_wrap
I would like to create pie charts with different radii in separate facets.我想在不同的方面创建具有不同半径的饼图。 How can I modify the following code (which produces equal radii) so the variable total is th rasdius for each slice?如何修改以下代码(产生相等的半径),以便每个切片的变量 total 为 th rasdius?
library(tidyverse)
mydf <- data_frame(value=c(1:3,1:3),
total=rep(c(10,20),times=3),
cond=rep(c("x","y","z"),times=2),
group=rep(c("a","b"),each=3))
mydf %>% ggplot(aes(x="",y=value,fill=cond)) +
geom_bar(stat="identity",width=1) +
facet_wrap(~ group) +
coord_polar("y")
Your question is a bit unclear.你的问题有点不清楚。 Something like that?类似的东西?
library(tidyverse)
mydf <- data_frame(value = c(1:3, 1:3),
total = rep(c(10, 20), times = 3),
cond = rep(c("x", "y", "z"),times = 2),
group = rep(c("a", "b"), each = 3))
mydf %>% ggplot(aes(x = "", y = value, fill = cond)) +
geom_bar(stat = "identity", width = 1) +
facet_wrap(total ~ cond) +
coord_polar("x")
This an old question but I wanted to do the same thing: The pie radius should vary with the group total.这是一个老问题,但我想做同样的事情:饼图半径应随组总数而变化。 First of all, I use geom_bar(position = "fill")
so that the two group bars stack up to the same height.首先,我使用geom_bar(position = "fill")
使两个组条堆叠到相同的高度。 Then I use the group totals as a width aesthetic: ggplot(aes(x = total/2, width = total))
.然后我使用组总数作为宽度美学: ggplot(aes(x = total/2, width = total))
。 Using half the total for the x aesthetic ensures that we get proper pies not doughnuts.使用 x 美学总数的一半确保我们得到适当的馅饼而不是甜甜圈。 So, the following code works for me although I'm not sure about how much of this is rather a hack than a proper solution (ggplot issues a warning if we move the width
aesthetic into the geom_bar
call: geom_bar(aes(width = total))
).因此,以下代码对我有用,尽管我不确定其中有多少是黑客而不是正确的解决方案(如果我们将width
美学移动到geom_bar
调用中,ggplot 会发出警告: geom_bar(aes(width = total))
)。
library(tidyverse)
mydf <- tibble(group = rep(c("group a", "group b"), each = 3),
cond = rep(c("x", "y", "z"), times = 2),
value = c(1, 2, 3, 2, 4, 6)) %>%
group_by(group) %>%
add_tally(value, name = "total") %>%
ungroup() %>%
mutate(label = sprintf("total = %d", total)) %>%
print()
#> # A tibble: 6 x 5
#> group cond value total label
#> <chr> <chr> <dbl> <dbl> <chr>
#> 1 group a x 1 6 total = 6
#> 2 group a y 2 6 total = 6
#> 3 group a z 3 6 total = 6
#> 4 group b x 2 12 total = 12
#> 5 group b y 4 12 total = 12
#> 6 group b z 6 12 total = 12
mydf %>% ggplot(aes(x = total/2, y = value, fill = cond, width = total)) +
geom_bar(stat = "identity", position = "fill", color = "white") +
facet_wrap(~ group + label, strip.position = "bottom") +
coord_polar("y", start = 0, direction = -1) +
theme_bw(base_size = 12) +
theme(axis.title = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank(),
panel.grid = element_blank(),
panel.border = element_blank(),
legend.title = element_text(size = 14),
strip.background = element_rect(fill = NA, colour = NA),
strip.text = element_text(size = 16))
Created on 2020-02-13 by the reprex package (v0.3.0)由reprex 包(v0.3.0) 于 2020 年 2 月 13 日创建
To better understand how this works, we can remove coord_polar()
and the theme()
calls to have a look at the underlying bar chart:为了更好地理解这是如何工作的,我们可以删除coord_polar()
和theme()
调用以查看底层条形图:
mydf %>% ggplot(aes(x = total/2, y = value, fill = cond, width = total)) +
geom_bar(stat = "identity", position = "fill", color = "white") +
facet_wrap(~ group + label, strip.position = "bottom") +
theme_bw(base_size = 12)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.