首页 > 科研绘图 > 这个小清新统计可视化工具太赞了~~
2022
11-18

这个小清新统计可视化工具太赞了~~

来 源:DataCharm/作 者:宁俊骐

最近小编在查阅资料的时候发现一个超喜欢的可视化绘制工具- R-smplot,本来想着忙完这段时间给大家直播的时候再系统介绍,但随着对这个工具的学习,还是决定现在就推荐给大家。好了,话不多说,我们直接开始,今天推文的主要内容如下:

  • R-smplot包简单介绍
  • R-smplot包案例介绍

R-smplot包简单介绍

R-smplot包,sm为 simple(简单)的简称,意为使R进行可视化过程变得简单,而且R-smplot包还完美兼容ggplot2绘图语法,熟悉ggplot2绘图的小伙伴可以快速上手。此外,该包还提供多个绘图函数:

  • 多个偏向于统计绘图的函数,如 sm_boxplot 和 sm_violin 函数;
  • 多个映射颜色,如: sm_color 和 sm_palette ;
  • 多个绘图主题,如 sm_corr_theme 和 sm_minimal 等,
  • 还提供大量常见的绘图函数,如 sm_bland_altman 、 sm_raincloud 、和 sm_common_axis 函数。

R-smplot包案例介绍

这一部分,小编通过具体的绘制示例给大家介绍 smplot包优秀的绘图函数、映射颜色和绘图主题,让小伙伴们对这个可视化包有所了解,详细内容如下:

R-smplot包映射颜色介绍

S-smplot包提供了非常“小清新”的颜色映射函数,这里直接给出样式,如下:

smplot’s color paletteR-smplot包映绘图主题介绍

R-smplot包提供的绘图主题也是非常多,下面就依次绘制 不同主题的可视化效果:

  • ggplot2默认主题

library(smplot)

library(tidyverse)

library(ggtext)

library(hrbrthemes)

# ggplot2默认主题

p1 <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = class)) +

geom_point(size = 2)

ggplot2默认主题

  • sm_corr_theme

p1 + sm_corr_theme

sm_corr_theme

还可以在主题基础上进行修改和选择映射颜色:

p2 <- p1 + sm_corr_theme(borders = FALSE, legends = FALSE) +

scale_color_manual(values = sm_palette(7))

sm_corr_theme set

  • sm_minimal

p1 + sm_minimal

sm_minimal

  • sm_slope_theme

p1 + sm_slope_theme

sm_slope_theme

R-smplot包常见绘图函数介绍

这一部分,小编列举出R-smplots包的常见绘图函数,如下:

「详细内容如下:」

  • sm_statCorr

p1 <- ggplot(data = mtcars, mapping = aes(x = drat, y = mpg)) +

geom_point(shape = 21, fill = sm_color( 'green'), color = 'white', size = 3)

p1 + sm_corr_theme +

sm_statCorr(color = sm_color( 'green'),

line_type = 'solid',

label_x = 3.5,

label_y = 30,

text_size = 5)

sm_statCorr example

  • sm_bar

set.seed(11) # generate random data

method1 = c(rnorm(19,0,1),2.5)

method2 = c(rnorm(19,0,1),2.5)

Subject <- rep(paste0( 'S',seq(1:20)), 2)

Data <- data.frame(Value = matrix(c(method1,method2),ncol=1))

Method <- rep(c( 'Method 1', 'Method 2'), each = length(method1))

df_general <- cbind(Subject, Data, Method)

# 可视化

ggplot(data = df_general, mapping = aes(x = Method, y = Value, fill = Method)) +

sm_bar(shape = 21, color = 'white', bar_fill_color = 'gray80') +

scale_fill_manual(values = sm_color( 'crimson', 'green'))

sm_bar Example

  • sm_boxplot

set.seed(1) # generate random data

day1 = rnorm(16,0,1)

day2 = rnorm(16,5,1)

Subject <- rep(paste0( 'S',seq(1:16)), 2)

Data <- data.frame(Value = matrix(c(day1,day2),ncol=1))

Day <- rep(c( 'Day 1', 'Day 2'), each = length(day1))

df <- cbind(Subject, Data, Day)

# 可视化

ggplot(data = df, mapping = aes(x = Day, y = Value)) +

sm_boxplot(fill = 'black')

sm_boxplot Example01

此外,还可以进行修改:

ggplot(data = df, mapping = aes(x = Day, y = Value, fill = Day)) +

sm_boxplot(shape = 21, point_size = 4, notch = 'TRUE', alpha = 0.5) +

scale_fill_manual(values = sm_color( 'blue', 'orange'))

sm_boxplot Example02

  • sm_violin

ggplot(data = df, mapping = aes(x = Day, y = Value, fill = Subject,

group = Day, color = Day)) +

sm_violin(shape = 21, color = 'white', point_alpha = 0.6) +

scale_fill_manual(values = sm_palette(16)) +

scale_color_manual(values = sm_color( 'blue', 'orange'))

sm_violin Example

  • sm_slope

ggplot(data = df, mapping = aes(x = Day, y = Value, group = Subject)) +

sm_slope(labels = c( 'Day 1', 'Day 2'))

sm_slope Example

  • sm_bland_altman

set.seed(1)

first <- rnorm(20)

second <- rnorm(20)

df3 <- as_tibble(cbind(first,second))

res <- sm_statBlandAlt(df3 $first,df3 $second)

sm_bland_altman(df3 $first, df3 $second, shape = 21, fill = sm_color( 'green'), color = 'white') +

scale_y_continuous(limits = c(-4,4)) +

annotate( 'text', label = 'Mean', x = -1, y = res $mean_diff+ 0.4) +

annotate( 'text', label = signif(res $mean_diff,3), x = -1, y = res $mean_diff- 0.4) +

annotate( 'text', label = 'Upper limit', x = 1.2, y = res $upper_limit+ 0.4) +

annotate( 'text', label = signif(res $upper_limit,3), x = 1.2, y = res $upper_limit- 0.4) +

annotate( 'text', label = 'Lower limit', x = 1.2, y = res $lower_limit+ 0.4) +

annotate( 'text', label = signif(res $lower_limit,3), x = 1.2, y = res $lower_limit-0.4)

sm_bland_altman Example

  • sm_raincloud

set.seed(2) # generate random data

day1 = rnorm(20,0,1)

day2 = rnorm(20,5,1)

day3 = rnorm(20,6,1.5)

day4 = rnorm(20,7,2)

Subject <- rep(paste0( 'S',seq(1:20)), 4)

Data <- data.frame(Value = matrix(c(day1,day2,day3,day4),ncol=1))

Day <- rep(c( 'Day 1', 'Day 2', 'Day 3', 'Day 4'), each = length(day1))

df2 <- cbind(Subject, Data, Day)

#可视化

sm_raincloud(data = df2, x = Day, y = Value, boxplot_alpha = 0.5,

color = 'white', shape = 21, sep_level = 2) +

scale_x_continuous(limits = c(0.25,4.75), labels = c( '1', '2', '3', '4'), breaks = c(1,2,3,4)) +

xlab( 'Day') +

scale_color_manual(values = rep( 'transparent',4)) +

scale_fill_manual(values = sm_palette(4))

sm_raincloud Example

到这里,关于R-smplot包的绘图功能就简单介绍了一下。

总结

今天介绍的这个优秀的可视化工具 R-smplot包功能还是非常强大的,通过介绍也可以看出该包更倾向于 统计绘图,这也是我们在绘制学术图表常用的图表类型,希望小伙伴们可以学习一下~

- END -



最后编辑:
作者:萌小白
一个热爱网络的青年!

发布评论

表情