R 可视化

R-ggChernoff包绘制表情符号

R-ggChernoff包作为ggplot2的拓展包,其提供的geom_chernoff()绘图函数与geom_point()函数语法类似,只不过,其绘制的为带笑脸的表情符号,而不是常规的点。这种笑脸表情符号制作过程依据如下:脸部特征会根据连续值的相对大小变换成微笑或者皱眉。默认情况下,平均值将生成笔直的脸😑,而较高的值将产生笑脸😊,而较低的值将产生皱眉😢。如果没有映射到相应变量,默认将全部都是笑脸。更多详细内容可参考R-ggChernoff包官网
接下来,将通过几个例子进行该包的解释:

「样例一」:

  1. library(ggChernoff)
  2. library(tidyverse)
  3. library(ggtext)
  4. library(hrbrthemes)
  5. library(LaCroixColoR)
  6. geom_chernoff_plot01 <- ggplot() +
  7. geom_chernoff(data = iris,aes(Petal.Width, Petal.Length, fill = Species),
  8. size=5) +
  9. scale_fill_manual(values = lacroix_palette(type = "paired"))+
  10. labs(
  11. title = "Example of <span style='color:#D20F26'>ggChernoff::geom_chernoff function</span>",
  12. subtitle = "processed charts with <span style='color:#1A73E8'>geom_chernoff()</span>",
  13. caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
  14. hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
  15. theme(
  16. plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
  17. size = 20, margin = margin(t = 1, b = 12)),
  18. plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
  19. plot.caption = element_markdown(face = 'bold',size = 12))

绘图技巧 | 绘制“符号”可视化图表 - 图1
Example01 of ggChernoff

「样例二」:

  1. test_data <- lattice::barley %>% filter(site==c("University Farm","Waseca","Morris"))
  2. geom_chernoff_plot02 <- ggplot() +
  3. geom_chernoff(data=test_data,aes(x = year, y = variety, smile = yield, brow = yield),
  4. fill = 'goldenrod1',size=8) +
  5. scale_x_discrete(limits = c('1931', '1932')) +
  6. #分面处理
  7. facet_wrap(~ site) +
  8. labs(
  9. title = "Example of <span style='color:#D20F26'>ggChernoff::geom_chernoff function</span>",
  10. subtitle = "processed charts with <span style='color:#1A73E8'>geom_chernoff()</span>",
  11. caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
  12. hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
  13. theme(
  14. plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
  15. size = 20, margin = margin(t = 1, b = 12)),
  16. plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
  17. plot.caption = element_markdown(face = 'bold',size = 12))

绘图技巧 | 绘制“符号”可视化图表 - 图2
Example02 of ggChernoff
以上两个例子很好的解释了geom_chernoff() 绘图函数的绘图原理(其实就是将数值映射到smilebrow等参数上),大家可根据自己的数据进行合理的数值映射即可。接下来,小编再介绍另一个绘制符号字体的R包。

R-emojifont包绘制表情符号

R-emojifont包不同于上面介绍的R包,其支持的符号字体(emoji font)也更加丰富,主要包括:Emoji characters(表情符号字符) 和 Font Awesome。更多信息可参考R-emojifont包官网
接下来,通过几个例子进行简单描述:

「样例一」:

  1. data <- data.frame(
  2. name=c("A","B","C","D","E") ,
  3. value=c(3,10,19,28,45),
  4. label = c(emoji('smiley'), emoji('smile'),emoji('sweat_smile'),
  5. emoji('smiley_cat'),emoji('smirk'))
  6. )
  7. Barplot <- ggplot(data, aes(x=name, y=value,label=label,fill=name)) +
  8. geom_bar(stat = "identity",color="black",width = .8) +
  9. geom_text(aes(y=value+6,color=name),family="EmojiOne",
  10. size=8) +
  11. hrbrthemes::scale_color_ipsum()+
  12. hrbrthemes::scale_fill_ipsum()+
  13. labs(
  14. title = "Example of <span style='color:#D20F26'>emojifont::EmojiOne</span>",
  15. subtitle = "processed charts with <span style='color:#1A73E8'>EmojiOne</span>",
  16. caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
  17. hrbrthemes::theme_ipsum(base_family = "Roboto Condensed")+
  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))

绘图技巧 | 绘制“符号”可视化图表 - 图3
Example01 of emojifont
这里可以看出,在每个柱形图的上方添加了对于的表情符合,当然,还可以单独使用表情符号进行绘制:

「样例二」:

  1. ##plot2
  2. x = seq(0, 2*pi, length=30)
  3. y = sin(x)
  4. ggplot() + geom_emoji('heartbeat', x=x, y=y,
  5. size=10,color = "red") +
  6. labs(
  7. title = "Example of <span style='color:#D20F26'>emojifont::geom_emoji</span>",
  8. subtitle = "processed charts with <span style='color:#1A73E8'>geom_emoji()</span>",
  9. caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
  10. hrbrthemes::theme_ipsum(base_family = "Roboto Condensed")+
  11. #theme_ft_rc(base_family = "Roboto Condensed") +
  12. theme(plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
  13. size = 20, margin = margin(t = 1, b = 12)),
  14. plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
  15. plot.caption = element_markdown(face = 'bold',size = 12),
  16. # 去除刻度线和网格线
  17. axis.text.x = element_blank(),
  18. axis.text.y = element_blank(),
  19. axis.title.x = element_blank(),
  20. axis.title.y = element_blank(),
  21. panel.grid.major = element_blank(),
  22. panel.grid.minor = element_blank())

绘图技巧 | 绘制“符号”可视化图表 - 图4
Example of geom_emoji()

「样例三」:

  1. df<-read_excel("test_data.xlsx")
  2. ##数据处理
  3. df <- df %>% mutate(
  4. emoji_label = if_else(gender == "Male",fontawesome("fa-mars-double"),
  5. fontawesome("fa-venus-double")))
  6. example <- ggplot(data = df,aes(decade, category, color = as.numeric(prop), label = emoji_label)) +
  7. geom_tile(fill = "grey25", color = "grey20", size = 1) +
  8. geom_text(family = 'fontawesome-webfont', size = 6) +
  9. facet_grid(. ~ gender) +
  10. scale_color_scico(palette = "buda", name = NULL, na.value = "grey25",
  11. guide = guide_colorbar(direction = "horizontal",
  12. barheight = unit(3, units = "mm"),
  13. barwidth = unit(150, units = "mm"),
  14. draw.ulim = FALSE, title.position = 'bottom',
  15. title.hjust = 0.5, label.hjust = 0.5)) +
  16. labs(
  17. title = "Example of emojifont library",
  18. subtitle = "processed charts with geom_text()")+
  19. theme(
  20. strip.text = element_blank(),
  21. rect = element_rect(fill = "grey20", colour = "grey20", size = 0.4,
  22. color = "grey20",linetype = 1),
  23. plot.background = element_rect(color = "grey20"),
  24. panel.background = element_rect(fill = NA, colour = NA),
  25. panel.border = element_rect(colour = "grey85", fill = NA, color = "grey20",size = rel(1)),
  26. panel.grid = element_blank(),
  27. plot.title = element_text(size = 15, hjust = 0.5, vjust = 1, face = "bold",colour = "white"),
  28. plot.subtitle = element_text(size = 12, hjust = 0.5, vjust = 1,colour = "white"),
  29. axis.text = element_text(size = 9,colour = "grey85"),
  30. legend.position = "bottom",
  31. legend.text = element_text(colour = "white"),
  32. axis.ticks = element_blank(),
  33. axis.title = element_blank()
  34. axis.text = element_text(size=10))

绘图技巧 | 绘制“符号”可视化图表 - 图5
Example03 of emojifont