什么?!一行代码就可以绘制这样的可视化效果?

2022-02-17 15:08:20 浏览数 (2)

今天小编给大家介绍一个绘制图表时添加阴影(shadow) 的小技巧,R-ggshadow 可视化绘制。R-ggshadow包提供geom_shadowline()、geom_shadowpoint()和geom_shadowpath() 等多个绘制阴影的函数,同时还提供朋克风格绘图样式,接下来将通过几个小例子来了解一下这个包的魅力。

R-ggshadow包样例演示

「样例一」:geom_shadowline()

代码语言:javascript复制
library(tidyverse)
library(ggtext)
library(hrbrthemes)
library(ggshadow)
library(rcartocolor)
#可视化绘制
ggplot(economics_long, aes(date, value01, colour = variable))   
   ggshadow::geom_shadowline()   
   scale_colour_manual(values = lacroix_palette(type = "paired"))  
   labs(
    title = "Example of <span style='color:#D20F26'>ggshadow:: geom_shadowline</span>",
    subtitle = "Processed charts with <span style='color:#1A73E8'>geom_shadowline()</span>",
    caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>")  
  #hrbrthemes::theme_ipsum(base_family = "Roboto Condensed")  
  hrbrthemes::theme_ft_rc(base_family = "Roboto Condensed")  
  theme(
    plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
                                  size = 20, margin = margin(t = 1, b = 12)),
    plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
    plot.caption = element_markdown(hjust = 1,face = 'bold',size = 12))

Example01 Of ggshadow::geom_shadowline()

「注意」:这里为了更好的显示阴影效果,使用了暗黑主题的绘图风格。

同时为了对比和普通的geom_line() 的不同,这里使用geom_line()绘制效果如下:

代码语言:javascript复制
***  
geom_line()  
***

Example Of geom_line()

可以看出两幅可视化效果有着明显的不同(阴影轮廓明显)。

「样例二」:改变线条颜色

代码语言:javascript复制
ggplot(economics_long, 
        aes(date, value01, 
            group = variable, 
            colour=value01, 
            shadowcolor='grey', 
            shadowalpha=0.5, 
            shadowsize=5*(1-value01)))   
    geom_shadowline()  
  hrbrthemes::theme_ft_rc(base_family = "Roboto Condensed") 

Example02 Of ggshadow::geom_shadowline()

「样例三」:Neon glow style

代码语言:javascript复制
ggplot(economics_long, aes(date, value01, color = variable))   
  geom_glowline()   guides(color='none')   
  theme(plot.background = element_rect(fill = "#190132"),
        panel.background = element_rect(fill = "#190132")) 

Example03 Of ggshadow::geom_shadowline()

「样例四」:Neon glow points

代码语言:javascript复制
ggplot(mtcars, aes(wt, mpg))   
  geom_glowpoint(color='yellow',size=2)   
  guides(color='none')  
  hrbrthemes::theme_ft_rc(base_family = "Roboto Condensed")  
  labs(
    title = "Example of <span style='color:#D20F26'>ggshadow:: geom_glowpoint</span>",
    subtitle = "Processed charts with <span style='color:#1A73E8'>geom_glowpoint()</span>",
    caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>")  
  hrbrthemes::theme_ft_rc(base_family = "Roboto Condensed")  
  theme(
    plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
                                  size = 20, margin = margin(t = 1, b = 12)),
    plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
    plot.caption = element_markdown(hjust = 1,face = 'bold',size = 12))

Example Of geom_glowpoint()

「样例五」:Adding a fill below the neon glow line

代码语言:javascript复制
ggplot(filter( economics_long, variable %in% c('pop', 'unemploy') ), 
    aes(date, value01-0.5, color = variable, fill=variable))   
    geom_glowline()   guides(color='none', shadowcolour='none', fill='none')   
    labs(
    title = "Example of <span style='color:#D20F26'>ggshadow:: geom_glowline</span>",
    subtitle = "Example charts with <span style='color:#1A73E8'>Adding a fill below the neon glow line</span>",
    caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>")  
    hrbrthemes::theme_ft_rc(base_family = "Roboto Condensed")  
    theme(
    plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
                                  size = 20, margin = margin(t = 1, b = 12)),
    plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
    plot.caption = element_markdown(hjust = 1,face = 'bold',size = 12))

Example Of Adding a fill below

以上就是小编介绍的关于R-ggshadow包的几个小例子,更多详细内容和参数设置可参考:R-ggshadow包官网[1]

总结

R-ggshadow包绘制出带阴影效果的可视化图表,也可以提供朋克风格的绘图样式,在一些偏商业的可视化环境中使用较多,希望小伙伴们可以有所启发哈~

参考资料

[1]R-ggshadow包官网介绍: https://github.com/marcmenem/ggshadow。

0 人点赞