R 数据可视化

R-waffle包简介

使用R绘制华夫饼图最简单的方法是使用waffle包进行绘制(当然,也可以使用ggplot2的geom_tile()geom_point()函数绘制)。这里主要介绍waffle包waffle()geom_pictogram()geom_waffle()scale_label_pictogram() 绘图函数,详细如下:

  • waffle():绘制方形华夫饼图
  • geom_waffle():绘制方形华夫饼图图层(用法和ggplot2相似)
  • geom_pictogram():绘制类似图形文字的集合图层。
  • scale_label_pictogram():与geom_pictogram()一起使用,使用Font Awesome字体映射标签。

接下来,通过各种例子介绍上述介绍的绘图函数。

R-waffle包样例介绍

1、waffle()

基本例子:

  1. library(tidyverse)
  2. library(ggtext)
  3. library(hrbrthemes)
  4. library(wesanderson)
  5. library(LaCroixColoR)
  6. library(RColorBrewer)
  7. library(waffle)
  8. parts <- data.frame(
  9. names = LETTERS[1:4],
  10. vals = c(80, 30, 20, 10)
  11. )
  12. waffle_plot <- waffle(parts,rows = 8)+
  13. theme_ipsum(base_family = "Roboto Condensed",grid = "") +
  14. labs(
  15. title = "Example of <span style='color:#D20F26'>waffle::waffle() function</span>",
  16. subtitle = "processed charts with <span style='color:#1A73E8'>waffle()</span>",
  17. caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
  18. theme(plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
  19. size = 20, margin = margin(t = 1, b = 12)),
  20. plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
  21. plot.caption = element_markdown(face = 'bold',size = 12),
  22. axis.text.x = element_blank(),
  23. axis.text.y = element_blank())

绘图技巧 | 不同样式华夫饼图(Waffle charts)绘制技巧 - 图1
Example01 of waffle()
当然还可以通过设置use_glyph参数,使用fontawesome 图标进行绘制,如下:

  1. waffle(parts,rows = 8,
  2. colors = c("#9DC8C8", "#58C9B9", "#519D9E","#D1B6E1"),
  3. use_glyph = "file-medical",
  4. size=5,legend_pos = "bottom")+
  5. ····

绘图技巧 | 不同样式华夫饼图(Waffle charts)绘制技巧 - 图2
Example02 of waffle()

2、geom_waffle()

  1. df <- data.frame(
  2. parts = factor(rep(month.abb[1:3], 3), levels=month.abb[1:3]),
  3. vals = c(10, 20, 30, 6, 14, 40, 30, 20, 10)
  4. )
  5. df_count <- df %>% count(parts, wt = vals)
  6. plot01 <- ggplot(data = df_count,aes(fill = parts, values = n)) +
  7. geom_waffle(n_rows = 20, size = .5, colour = "black", flip = TRUE) +
  8. #ggsci::scale_fill_lancet(labels = c("Fruit", "Sammich", "Pizza"))+
  9. scale_fill_manual(values = lacroix_palette(type = "paired"),
  10. labels = c("Fruit", "Sammich", "Pizza")) +
  11. guides(fill=guide_legend(nrow=1,byrow=TRUE,reverse = TRUE,title=NULL))+
  12. labs(
  13. title = "Example of <span style='color:#D20F26'>waffle::geom_waffle function</span>",
  14. subtitle = "processed charts with <span style='color:#1A73E8'>geom_waffle()</span>",
  15. caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
  16. theme_ipsum(base_family = "Roboto Condensed",grid="") +
  17. theme_enhance_waffle()+
  18. theme(plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
  19. size = 20, margin = margin(t = 1, b = 12)),
  20. plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
  21. plot.caption = element_markdown(face = 'bold',size = 12),
  22. legend.position = 'bottom',)

绘图技巧 | 不同样式华夫饼图(Waffle charts)绘制技巧 - 图3
Example01 of geom_waffle
「Waffle Bar Charts」

  1. test02 <- storms %>% filter(year >= 2010) %>% count(year, status)
  2. ggplot(test02, aes(fill = status, values = n)) +
  3. geom_waffle(color = "white", size = .25, n_rows = 10, flip = TRUE) +
  4. facet_wrap(~year, nrow = 1, strip.position = "bottom") +
  5. scale_x_discrete() +
  6. scale_y_continuous(labels = function(x) x * 10,expand = c(0,0)) +
  7. scale_fill_manual(values = lacroix_palette(type = "paired"),
  8. name=NULL) +
  9. coord_equal() +
  10. labs(
  11. title = "Example of <span style='color:#D20F26'>waffle::geom_pictogram function</span>",
  12. subtitle = "processed charts with <span style='color:#1A73E8'>geom_pictogram()</span>",
  13. caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>",
  14. x = "Year",
  15. y = "Count") +
  16. theme_minimal(base_family = "Roboto Condensed") +
  17. theme(plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
  18. size = 20, margin = margin(t = 1, b = 12)),
  19. plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
  20. plot.caption = element_markdown(face = 'bold',size = 12),
  21. legend.position = 'bottom',
  22. panel.grid = element_blank(),
  23. axis.ticks.y = element_line()) +
  24. guides(fill = guide_legend(reverse = TRUE))

绘图技巧 | 不同样式华夫饼图(Waffle charts)绘制技巧 - 图4
Example of geom_waffle()
通过此例子,就可以通过使用不同图方形个数表示不同类别的个数,当然,也可以使用不同样式图标进行表示。

3、geom_pictogram()scale_label_pictogram()

还还可以通过geom_pictogram()scale_label_pictogram()绘图函数使用fontawesome字体图标进行绘制,例子如下:
fa 字体.zip

  1. #导入字体:比较重要的一步
  2. library(showtext)
  3. font_add(family = "FontAwesome5Free-Solid", regular = "E:\\Download Fonts\\fa-solid-900.ttf")
  4. font_add(family = "FontAwesome5Free-Regular", regular = "E:\\Download Fonts\\fa-regular-400.ttf")
  5. font_add(family = "FontAwesome5Brands-Regular", regular = "E:\\Download Fonts\\fa-brands-400.ttf")
  6. font_add(family = "Roboto Condensed",regular = "E:\\Download Fonts\\RobotoCondensed-Regular.ttf")
  7. plot02 <- ggplot(data = df_count) +
  8. geom_pictogram(aes(label = parts, values = n,colour = parts),n_rows = 10,
  9. flip = TRUE, make_proportional = TRUE) +
  10. scale_colour_manual(values = lacroix_palette(type = "paired"),
  11. labels = c("Fruit", "Sammich", "Pizza")) +
  12. scale_label_pictogram(
  13. values = c("apple-alt", "bread-slice", "pizza-slice"),
  14. labels = c("Fruit", "Sammich", "Pizza")
  15. ) +
  16. guides(fill=guide_legend(nrow=1,byrow=TRUE,reverse = TRUE,title=NULL))+
  17. labs(
  18. title = "Example of <span style='color:#D20F26'>waffle::geom_pictogram() function</span>",
  19. subtitle = "processed charts with <span style='color:#1A73E8'>geom_pictogram()</span>",
  20. caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
  21. theme_ipsum(grid="") +
  22. theme_enhance_waffle()+
  23. theme(plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",face = 'bold',
  24. size = 20, margin = margin(t = 1, b = 12)),
  25. plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
  26. plot.caption = element_markdown(face = 'bold',size = 12),
  27. legend.position = 'bottom',)

绘图技巧 | 不同样式华夫饼图(Waffle charts)绘制技巧 - 图5
Example Of geom_pictogram