簡體   English   中英

ggplot、餅圖和 facet_wrap 中的不同半徑

[英]ggplot, pie chart and different radii in facet_wrap

我想在不同的方面創建具有不同半徑的餅圖。 如何修改以下代碼(產生相等的半徑),以便每個切片的變量 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")

你的問題有點不清楚。 類似的東西?

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")

1

這是一個老問題,但我想做同樣的事情:餅圖半徑應隨組總數而變化。 首先,我使用geom_bar(position = "fill")使兩個組條堆疊到相同的高度。 然后我使用組總數作為寬度美學: ggplot(aes(x = total/2, width = total)) 使用 x 美學總數的一半確保我們得到適當的餡餅而不是甜甜圈。 因此,以下代碼對我有用,盡管我不確定其中有多少是黑客而不是正確的解決方案(如果我們將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))

reprex 包(v0.3.0) 於 2020 年 2 月 13 日創建

為了更好地理解這是如何工作的,我們可以刪除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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM