在R中的华夫饼图:99个图标而不是100个图标。

3

这里是数据:

df <- structure(list(country = c("Australia", "Australia", "Australia", 
"South Korea", "South Korea", "South Korea"), parts = c("case_1", 
"case_2", "non_case", "case_1", "case_2", "non_case"), values = c(1, 
19, 80, 1, 29, 70)), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

library(tidyverse)
library(waffle)
df %>% ggplot(aes(label = parts, values = values)) +
    geom_pictogram(n_rows = 10, aes(color = parts),
                   family = 'fontawesome-webfont',
                   flip = TRUE, 
                   size = 6, 
                  show.legend = FALSE, 
                  make_proportional = TRUE
                  ) +
    scale_label_pictogram(
        name = "Confirmed cases",
        values = c("male"),
    ) +
    coord_equal() +theme_minimal() +
facet_wrap(~country, nrow = 2, strip.position = "top") 

在上述代码中,我生成了以下华夫饼图。使用make_proportional = TRUEn_rows = 10,我预期每个国家会得到100个图标,但是对于韩国只得到了99个。唯一解决这个问题的方法是先计算所有比例,然后使用make_proportional = FALSE,但这需要一些时间。此外,我觉得这有点奇怪。如果有人能帮我解决这个问题,我将不胜感激。

enter image description here


2
我遇到了这个错误:Error in geom_pictogram(n_rows = 10, aes(color = parts), family = "fontawesome-webfont", : could not find function "geom_pictogram" - TarJae
2
是的,很可能你正在使用来自 CRAN 的版本(0.7.0)。我正在使用来自 https://github.com/hrbrmstr/waffle 的版本 1.0.1。 - Zhiqiang Wang
2
@TarJae,你可以尝试使用 devtools::install_github("hrbrmstr/waffle") - Quinten
2个回答

2
在waffle的函数中似乎存在一个奇怪的四舍五入问题。根据文档make_proportional参数执行以下操作:

从原始值计算比例?(即,每个值n将被替换为n/sum(n));默认为FALSE。

似乎值的四舍五入出了问题。这里使用as.integer进行演示,其中n2显示当前发生的情况,而n3是实际应该发生的值:
library(dplyr)
df %>%
  group_by(country) %>%
  mutate(n = values/sum(values)) %>%
  ungroup() %>%
  mutate(n2 = as.integer(n*100),
         n3 = as.integer(round(n*100)))
#> # A tibble: 6 × 6
#>   country     parts    values     n    n2    n3
#>   <chr>       <chr>     <dbl> <dbl> <int> <int>
#> 1 Australia   case_1        1  0.01     1     1
#> 2 Australia   case_2       19  0.19    19    19
#> 3 Australia   non_case     80  0.8     80    80
#> 4 South Korea case_1        1  0.01     1     1
#> 5 South Korea case_2       29  0.29    28    29
#> 6 South Korea non_case     70  0.7     70    70

使用 reprex v2.0.2 于2022-10-16创建

正如您所看到的,它应该像n3一样。因此,您可以检查您的值与n2之间的差是否大于0,以将1添加到您的值中,从而获得所需的结果,如下所示:

library(waffle)
library(magrittr)
library(ggplot2)
library(dplyr)
library(emojifont)
library(waffle)
library(extrafont)
#> Registering fonts with R
df %>%
  group_by(country) %>%
  mutate(n = values/sum(values)) %>%
  ungroup() %>%
  mutate(n2 = as.integer(n*100)) %>%
  mutate(values = ifelse(values-n2 > 0, values + 1, values)) %>%
  ggplot(aes(label = parts, values = values)) +
  geom_pictogram(n_rows = 10, aes(color = parts),
                 family = 'fontawesome-webfont',
                 flip = TRUE, 
                 size = 6, 
                 show.legend = FALSE, 
                 make_proportional = TRUE
  ) +
  scale_label_pictogram(
    name = "Confirmed cases",
    values = c("male"),
  ) +
  coord_equal() +theme_minimal() +
  facet_wrap(~country, nrow = 2, strip.position = "top")

Created on 2022-10-16 with reprex v2.0.2


1

虽然不是解决方案,但可能有一些奇怪的情况发生。也许是舍入效应。

waffle() 包含一个名为 'make_proportion' 的函数,该函数基于以下几行:

if (params[["make_proportional"]]) {
  .x[["values"]] <- .x[["values"]] / sum(.x[["values"]])
  .x[["values"]] <- round_preserve_sum(.x[["values"]], digits = 2)
  .x[["values"]] <- as.integer(.x[["values"]] * 100)
}

然而,将第一行和第三行应用于“韩国”价值:
 >   df
# A tibble: 3 × 3
  country     parts    values
  <chr>       <chr>     <dbl>
1 South Korea case_1        1
2 South Korea case_2       29
3 South Korea non_case     70
 >   df[["values"]] <- df[["values"]] / sum(df[["values"]])
 >   df
# A tibble: 3 × 3
  country     parts    values
  <chr>       <chr>     <dbl>
1 South Korea case_1     0.01
2 South Korea case_2     0.29
3 South Korea non_case   0.7 
 >     df[["values"]] <- as.integer(df[["values"]] * 100)
 >   df
# A tibble: 3 × 3
  country     parts    values
  <chr>       <chr>     <int>
1 South Korea case_1        1
2 South Korea case_2       28
3 South Korea non_case     70

100变成了99!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接