数据可视化 R

R-ggCyberPunk包简介

R-ggCyberPunk包作为基于ggplot2、专门绘制赛博朋克风的第三方包,其提供的绘制函数有限,主要包括以下几个:

  • geom_glowing_area():绘制赛博朋克风发光区域图(area plot)。
  • geom_glowing_line():绘制赛博朋克风发光线图(line plot )。
  • geom_linesaber():绘制赛博朋克风激光剑外观线图(line plot)。
  • theme_cyberpunk():绘制赛博朋克风绘图主题。

更多参数信息可参考:R-ggCyberPunk介绍

R-ggCyberPunk包实例演示

这一部分就列举使用几个 R-ggCyberPunk包绘制赛博朋克风的可视化作品,如下:

geom_glowing_area()」:

  1. library(tidyverse)
  2. library(ggtext)
  3. library(hrbrthemes)
  4. library(ggCyberPunk)
  5. df = data.frame(A=c(1,4,4,6,7,5,1),
  6. B=c(4,3,5,7,5,6,7),
  7. Time=c(1,2,3,4,5,6,7)) %>%
  8. # 将数据准换成DF类型
  9. pivot_longer(cols = c(A,B),names_to = "group", values_to = "count")
  10. #可视化绘制
  11. plot01 <- ggplot(data = df,aes(x=Time, y = count, color = group, fill= group))+
  12. geom_glowing_area()+
  13. scale_fill_cyberpunk("zune")+
  14. scale_color_cyberpunk("zune")+
  15. labs(
  16. title = "Example of <span style='color:#D20F26'>ggCyberPunk::geom_glowing_area function</span>",
  17. subtitle = "processed charts with <span style='color:#1A73E8'>geom_glowing_area()</span>",
  18. caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
  19. theme_cyberpunk(font = "Roboto Condensed")+
  20. theme(
  21. plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
  22. size = 20, margin = margin(t = 1, b = 12)),
  23. plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
  24. plot.caption = element_markdown(hjust = 1,face = 'bold',size = 12)
  25. )

2021-08-27-16-32-35-837256.png
Example Of geom_glowing_area()with zune
注意:这里scale_fill_cyberpunk() 中可设置main, zune,cotton candy, laser sword 四种选项。效果分别如下:
main」:
2021-08-27-16-32-36-017257.png
Example Of geom_glowing_area()with main
cotton candy」:
2021-08-27-16-32-36-190256.png
Example Of geom_glowing_area()with cotton candy
laser sword」:
2021-08-27-16-32-36-333256.png
Example Of geom_glowing_area()with laser sword

geom_glowing_line()」:

  1. df = data.frame(A=c(1,4,4,6,7,5,1),
  2. B=c(4,3,5,7,5,6,7),
  3. Time=c(1,2,3,4,5,6,7)) %>%
  4. pivot_longer(cols = c(A,B),names_to = "group", values_to = "count")
  5. plot2 <- ggplot(data = df,aes(x=Time, y = count, color = group, fill= group))+
  6. geom_glowing_line()+
  7. scale_color_cyberpunk()+
  8. labs(
  9. title = "Example of <span style='color:#D20F26'>ggCyberPunk::geom_glowing_line function</span>",
  10. subtitle = "processed charts with <span style='color:#1A73E8'>geom_glowing_line()</span>",
  11. caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
  12. theme_cyberpunk(font = "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(hjust = 1,face = 'bold',size = 12)
  18. )

2021-08-27-16-32-36-934258.png
Example Of geom_glowing_line()

geom_linesaber()」:

  1. df = data.frame(A=c(1,4,4,6,7,5,1),
  2. B=c(4,3,5,7,5,6,7),
  3. Time=c(1,2,3,4,5,6,7)) %>%
  4. pivot_longer(cols = c(A,B),names_to = "group", values_to = "count")
  5. plot3 <- ggplot(data = df,aes(x=Time, y = count, color = group, fill= group))+
  6. geom_linesaber()+
  7. theme_cyberpunk()+
  8. scale_color_cyberpunk(reverse = T)+
  9. labs(
  10. title = "Example of <span style='color:#D20F26'>ggCyberPunk::geom_linesaber function</span>",
  11. subtitle = "processed charts with <span style='color:#1A73E8'>geom_linesaber()</span>",
  12. caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
  13. theme_cyberpunk(font = "Roboto Condensed")+
  14. theme(
  15. plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
  16. size = 20, margin = margin(t = 1, b = 12)),
  17. plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
  18. plot.caption = element_markdown(hjust = 1,face = 'bold',size = 12)
  19. )

2021-08-27-16-32-37-406265.png
Example Of geom_linesaber()
以上就是简单介绍了R-ggCyberPunk包三个绘图函数,接下里,使用改包主题对常见图表进行朋克风格图表绘制,如下:

「geom_boxplot」:箱线图

  1. plot4 <- ggplot(mpg, aes(class, hwy,fill=factor(drv))) +
  2. geom_boxplot() +
  3. scale_fill_cyberpunk("zune",name="")+
  4. labs(
  5. title = "Example of <span style='color:#D20F26'>ggCyberPunk::scale_fill_cyberpunk function</span>",
  6. subtitle = "processed charts with <span style='color:#1A73E8'>scale_fill_cyberpunk()</span>",
  7. caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
  8. theme_cyberpunk(font = "Roboto Condensed")+
  9. theme(
  10. plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
  11. size = 20, margin = margin(t = 1, b = 12)),
  12. plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
  13. plot.caption = element_markdown(hjust = 1,face = 'bold',size = 12)
  14. )

2021-08-27-16-32-37-735254.png
Example Of ggplot2::geom_boxplot() with theme_cyberpunk()
当然,还可以设置其他颜色风格的样式。如果不喜欢R绘制类似的图表,可以试试python的可视化库-mplcyberpunk,更多关于mplcyberpunk库的详细内容大家可参考:Python-mplcyberpunk库

参考资料

R-ggCyberPunk介绍:https://github.com/delabj/ggCyberPunk
python-mplcyberpunk库:https://github.com/dhaitz/mplcyberpunk