首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >geom_waffle没有显示一个单元格

geom_waffle没有显示一个单元格
EN

Stack Overflow用户
提问于 2021-12-29 14:35:48
回答 1查看 293关注 0票数 3

我一直在使用包waffle,我无法解决如何修复这个华夫饼图,使它显示100个平方/单元格

这是数据和我的代码:

代码语言:javascript
复制
# devtools::install_github("hrbrmstr/waffle")
library(ggplot) 
library(waffle)

data <- structure(list(greenfield = c(0, 1), n = c(162L, 399L), total_investments = c(561, 
561), percentage = c(28.8770053475936, 71.1229946524064), name_greenfield = c("M&A", 
"Greenfield")), row.names = c(NA, -2L), groups = structure(list(
    greenfield = c(0, 1), .rows = structure(list(1L, 2L), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, -2L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

# Waffle plot

ggplot(data,
       aes(fill = name_greenfield,
           values = percentage))  +
  coord_equal() +
  geom_waffle(size = 0.31,  
              flip = T, 
              make_proportional = T, 
              height = 0.9, width = 0.9,
              color = "white") +
  coord_equal() + 
  theme_ipsum_rc(grid="",
                 plot_title_face = "plain",
                 plot_title_size = 14,
                 plot_title_margin = 0) +
  theme_enhance_waffle()  +
  labs(title = "Types of investments",
       fill = "",
       caption = "Source: Author's own elaboration") + 
  scale_fill_grey(start = 0.4, end = 0.7)

我还没有发现问题,如果有人能帮我的话,我会很感激的!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-29 15:26:21

代码中有一个非常非常奇怪的错误。我怀疑这是怎么回事。在GitHub的源代码中,有一个文件stat-waffle.R。在第93行,您会发现:

代码语言:javascript
复制
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)
}

有些奇怪的事情发生在你的价值观上。

代码语言:javascript
复制
.x <- list(values = c(29, 71))

.x[["values"]] <- .x[["values"]] / sum(.x[["values"]])
# [1] 0.29 0.71
.x[["values"]] <- waffle:::round_preserve_sum(.x[["values"]], digits = 2)
# [1] 0.29 0.71
.x[["values"]] <- as.integer(.x[["values"]] * 100)
# [1] 28 71

这就是为什么你看到一个丢失的广场。这和浮点数的数学有关。

代码语言:javascript
复制
format(round(.x$values / sum(.x$values), 2) * 100, digits = 22)
# [1] "28.999999999999996" "71.000000000000000"

如果您进行此修改,结果将如预期的那样。

代码语言:javascript
复制
.x[["values"]] <- as.integer(round(.x[["values"]] * 100))
# [1] 29 71

这对你意味着什么?将数字四舍五入为整数,提前加到100,并使用make_proportional = FALSE。这样,代码块就不会运行。

代码语言:javascript
复制
data$percentage <- as.integer(round(round(data$percentage / sum(data$percentage), 2) * 100))
data$percentage
# [1] 29 71

ggplot(data,
       aes(fill = name_greenfield,
           values = percentage))  +
  coord_equal() +
  geom_waffle(size = 0.31,  
              flip = T, 
              make_proportional = FALSE, 
              height = 0.9, width = 0.9,
              color = "white") +
  coord_equal() + 
  theme_enhance_waffle()  +
  labs(title = "Types of investments",
       fill = "",
       caption = "Source: Author's own elaboration") + 
  scale_fill_grey(start = 0.4, end = 0.7)

这应该能给你带来你所期待的。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70520851

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档