我试图用R中的geom_waffle()来做一个华夫饼图.这是我目前为止的代码;

library(tidyverse)
library(waffle)
library(ggthemes)

haunted_places <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-10-10/haunted_places.csv')
haunted_places_top_5 <- haunted_places %>% 
  mutate(last_word = stringr::str_extract(location, "\\S+$"),
         last_word = str_to_title(last_word),
         last_word = case_when(last_word == "Rd." ~ "Road",
                               last_word == "Rd" ~ "Road",
                               last_word == "Streets" ~ "Street",
                               last_word == "Street)" ~ "Street", 
                               last_word == "St." ~ "Street", 
                               .default = as.character(last_word))) %>%
  group_by(last_word) %>%
  mutate(count = n(),
         percentage = count/10992,
         total_count = 10992) %>%
  select(last_word, count, total_count, percentage) %>%
  distinct() %>%
  arrange(desc(count))

haunted_places_top_5 <- haunted_places_top_5[1:5, ] 

ggplot(haunted_places_top_5, aes(fill = last_word, values = count)) +
  geom_waffle(color = "white", size = 0.25, na.rm = TRUE, nrows = 10) +
  facet_wrap(~last_word, nrow = 2) +
  theme_fivethirtyeight() +
  theme(panel.grid.major.x = element_blank(),
        panel.grid.major.y = element_blank(),
        axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        legend.position = "none") +
  coord_fixed(ratio = 1)

并且,此代码返回以下绘图;

enter image description here

我对这种可视化的问题主要是因为瓷砖太小了.有没有办法或诀窍可以帮助定制刻面面板的大小,以便让观众更好地看到它们?感谢您事先的关照.

推荐答案

一种可行的方法包括通过将计数值除以10来缩小计数值.这种调整有效地减少了计数值,导致华夫饼图的每个平铺代表10个单位,而不是只有一个单位.随后,为了清楚起见,这一变化可以清楚地注释在曲线图上.

plot <- ggplot(haunted_places_top_5, aes(fill = last_word, values = count/10)) +  # Scale down the count
  geom_waffle(color = "white", size = 0.25, na.rm = TRUE, nrows = 5) +
  facet_wrap(~last_word, nrow = 2) +
  theme_fivethirtyeight() +
  theme(panel.grid.major.x = element_blank(),
        panel.grid.major.y = element_blank(),
        axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        legend.position = "none") +
  coord_fixed(ratio = 1) +
  annotate("text", x = Inf, y = Inf, label = "Each tile represents 10 units", 
           hjust = 1.1, vjust = 1.1, size = 3, color = "black")  # Add annotation
plot

enter image description here

R相关问答推荐

为什么以及如何修复Mapview不显示所有点并且st_buffer合并一些区域R?

单击 map 后,将坐标复制到剪贴板

R等效于LABpascal(n,1)不同的列符号

在R中创建一个包含转换和转换之间的时间的列

如何通过Docker部署我的shiny 应用程序(多个文件)

通过在colname中查找其相应值来创建列

有没有一种方法可以同时对rhandsontable进行排序和从rhandsontable中删除?

查找所有站点的最小值

我们如何在R中透视数据并在之后添加计算

如何对r中包含特定(未知)文本的行求和?

我如何使用tidyselect来传递一个符号数组,比如Pivot_Long?

将统计检验添加到GGPUBR中的盒图,在R

按组跨多列创建伪变量

如何在使用因子时获得Sankey图的Scale_Fill_Viridis的全范围

是否有一个R函数可以输出在输入的字符向量中找到的相应正则表达式模式?

如何在Quarto中使用美人鱼图表中的标记来加粗文本

使用同一行中的前一个值填充R矩阵中的缺失值

通过比较来自多个数据框的值和R中的条件来添加新列

如何编辑被动式数据表?

使用循环改进功能( struct 简单)