绘图技巧R语言做图R plot

R使用abestr创建估计图

2021-05-15  本文已影响0人  R语言数据分析指南

本节来分享一个统计分析的小例子,通过R中的abestr包来绘制估计图 ,各位观众老爷细细品味
原文链接:https://mp.weixin.qq.com/s/8rXlJNPi8LAK_4pvhG3UUg

估计图简介:https://acclab.github.io/robust-statistical-visualization.html

相对于传统图而言,估计图具有五个主要优势:

条形图 箱形图 抖动图 估计图
避免错误的二分法
显示所有观察值
专注于效果大小
可视化效果尺寸精度
显示平均差异分布

下面让我们开始在R中创建图形

加载R包

library(tidyverse)
library(dabestr)

构建数据集

set.seed(123)

N = 40
c1 <- rnorm(N, mean = 100, sd = 25)
c2 <- rnorm(N, mean = 100, sd = 50)
g1 <- rnorm(N, mean = 120, sd = 25)
g2 <- rnorm(N, mean = 80, sd = 50)
g3 <- rnorm(N, mean = 100, sd = 12)
g4 <- rnorm(N, mean = 100, sd = 50)
gender <- c(rep('Male', N/2), rep('Female', N/2))
dummy <- rep("Dummy", N)
id <- 1: N
wide.data <- 
  tibble::tibble(
    Control1 = c1, Control2 = c2,
    Group1 = g1, Group2 = g2, 
    Group3 = g3, Group4 = g4,
    Dummy = dummy,
    Gender = gender, ID = id)

宽表转长表

my.data <- wide.data %>% 
  pivot_longer(cols = -c(ID, Gender, Dummy),
                           names_to = "Group")
my.data %>%
  dabest(Group,value, 
         idx = c("Control1","Group1"), 
         paired = FALSE) %>% 
  mean_diff() %>% 
  plot(color.column = Gender)

创建2组间的估计图

my.data %>%
  dabest(Group,value, 
         idx = c("Control1", "Group1"), 
         paired = TRUE, id.col = ID) %>% 
  mean_diff() %>% 
  plot(color.column = Gender)

Gardner-Altman估计图的关键特征是:

  • 绘制了所有数据点
  • 展示平均差异及其95%置信区间分别显示为点估计和垂直条

创建多组比较图,需要先指定一个列表

multi.two.group.unpaired <- 
  my.data %>%
  dabest(Group,value, 
         idx = list(c("Control1", "Group1"), 
                    c("Control2", "Group2")),
         paired = FALSE)

multi.two.group.unpaired.meandiff <- mean_diff(multi.two.group.unpaired)

multi.two.group.unpaired.meandiff %>% 
  plot(color.column = Gender)
shared.control <- 
  my.data %>%
  dabest(Group,value, 
         idx = c("Control2", "Group2", "Group4"),
         paired = FALSE
  )

shared.control.mean_diff <- shared.control %>% mean_diff()

plot(shared.control.mean_diff, 
     color.column = Gender,
     rawplot.type = "swarmplot")

多组图

multi.group <- 
  my.data %>%
  dabest(Group, value, 
         idx = list(c("Control1", "Group1", "Group3"), 
                    c("Control2", "Group2", "Group4")),
         paired = FALSE
  )

multi.group.mean_diff <- multi.group %>% mean_diff() 

plot(multi.group.mean_diff, color.column = Gender)

细节调整

rawplot.ylim和effsize.ylim参数分别为rawplot和delta图自定义y轴范围

plot(multi.group.mean_diff, 
     color.column = Gender,
     rawplot.ylim = c(-100, 200),
     effsize.ylim = c(-60, 60)
)

rawplot.groupwidt控制每组数据点在x方向上扩散,默认值为0.3

plot(multi.group.mean_diff, 
     color.column = Gender,
     rawplot.markersize = 1,
     rawplot.groupwidth = 0.4
)

rawplot.ylabel和effsize.ylabel参数修改y轴标题

plot(multi.group.mean_diff, 
     color.column = Gender,
     rawplot.ylabel = "Rawplot Title?",
     effsize.ylabel = "My delta plot!"
)

axes.title.fontsize参数调整y轴标题的字体大小

plot(multi.group.mean_diff, 
     color.column = Gender,
     axes.title.fontsize = 10
)

palette参数自定义颜色

plot(multi.group.mean_diff, 
     color.column = Gender,
     palette = "Dark2"
)
plot(multi.group.mean_diff, 
     color.column = Gender,
     palette = c("#FFA500", "sienna4")
)

theme 调用ggplot2主题

plot(multi.group.mean_diff, 
     color.column = Gender,
     theme =theme_bw()
)

参考:https://cran.r-project.org/web/packages/dabestr/vignettes/using-dabestr.html
https://acclab.github.io/bootstrap-confidence-intervals.html

上一篇 下一篇

猜你喜欢

热点阅读