小提琴图+箱线图
ggviolin(df, x = "dose", y = "len", fill = "dose", palette = c("#00AFBB", "#E7B800", "#FC4E07"), add = "boxplot",#添加箱子 add.params = list(fill = "white"))+ #箱线图填充颜色 stat_compare_means(comparisons = my_comparisons, label = "p.signif")+ stat_compare_means(label.y = 50)
条图
#构建数据集 # 导入内置数据 data("mtcars") dfm <- mtcars # 将cyl变量转为因子 dfm$cyl <- as.factor(dfm$cyl) # 添加name列 dfm$name <- rownames(dfm) #检查数据 head(dfm[, c("name", "wt", "mpg", "cyl")])
> head(dfm[, c("name", "wt", "mpg", "cyl")]) name wt mpg cyl Mazda RX4 Mazda RX4 2.620 21.0 6 Mazda RX4 Wag Mazda RX4 Wag 2.875 21.0 6 Datsun 710 Datsun 710 2.320 22.8 4 Hornet 4 Drive Hornet 4 Drive 3.215 21.4 6 Hornet Sportabout Hornet Sportabout 3.440 18.7 8 Valiant Valiant 3.460 18.1 6
顺序条图
ggbarplot(dfm, x = "name", y = "mpg", fill = "cyl", # 根据cyl分组填充颜色 color = "white", #设置条形边框颜色为白色 palette = "jco", # jco杂志配色 sort.val = "desc", # 降序 sort.by.groups = FALSE, # 不按组排序 x.text.angle = 90 # 垂直旋转x轴文本角度 )
ggbarplot(dfm, x = "name", y = "mpg", fill = "cyl", color = "white", palette = "jco", sort.val = "asc", # 升序 sort.by.groups = TRUE, # 先分组先排序 x.text.angle = 90 )
偏差图
偏差图显示了定量值与参考值的偏差。
z分数(z-score),也叫标准分数(standard score)是一个数与平均数的差再除以标准差的过程。在统计学中,标准分数是一个观测或数据点的值高于被观测值或测量值的平均值的标准偏差的符号数。
#计算mpg数据的 z-score
dfm$mpg_z <- (dfm$mpg -mean(dfm$mpg))/sd(dfm$mpg) dfm$mpg_grp <- factor(ifelse(dfm$mpg_z < 0, "low", "high"), levels = c("low", "high")) #根据z分数分组并添加为新列。很好用的代码可以记记 head(dfm[, c("name", "wt", "mpg", "mpg_z", "mpg_grp", "cyl")])
> head(dfm[, c("name", "wt", "mpg", "mpg_z", "mpg_grp", "cyl")]) name wt mpg mpg_z mpg_grp cyl Mazda RX4 Mazda RX4 2.620 21.0 0.1508848 high 6 Mazda RX4 Wag Mazda RX4 Wag 2.875 21.0 0.1508848 high 6 Datsun 710 Datsun 710 2.320 22.8 0.4495434 high 4 Hornet 4 Drive Hornet 4 Drive 3.215 21.4 0.2172534 high 6 Hornet Sportabout Hornet Sportabout 3.440 18.7 -0.2307345 low 8 Valiant Valiant 3.460 18.1 -0.3302874 low 6
ggbarplot(dfm, x = "name", y = "mpg_z", fill = "mpg_grp", color = "white", palette = "jco", sort.val = "asc", sort.by.groups = FALSE, x.text.angle = 90, ylab = "MPG z-score", xlab = FALSE, legend.title = "MPG Group" )
ggbarplot(dfm, x = "name", y = "mpg_z", fill = "mpg_grp", color = "white", palette = "jco", sort.val = "desc", #降序 sort.by.groups = FALSE, x.text.angle = 90, ylab = "MPG z-score", legend.title = "MPG Group", rotate = TRUE, #纵向 ggtheme = theme_minimal() )
点图
棒棒糖图
ggdotchart(dfm, x = "name", y = "mpg", color = "cyl", #颜色分组 palette = c("#00AFBB", "#E7B800", "#FC4E07"), #常规调色板 sorting = "ascending", # 升序 add = "segments", # 将y = 0的段添加到点 ggtheme = theme_pubr() # 主题 )
ggdotchart(dfm, x = "name", y = "mpg", color = "cyl", palette = c("#00AFBB", "#E7B800", "#FC4E07"), sorting = "descending", #降序 add = "segments", rotate = TRUE, #纵向 group = "cyl", #先分组 dot.size = 6, #点大小 label = round(dfm$mpg), #点标签 font.label = list(color = "white", size = 9, vjust = 0.5), #调整标签参数 ggtheme = theme_pubr() )
ggdotchart(dfm, x = "name", y = "mpg_z", color = "cyl", palette = c("#00AFBB", "#E7B800", "#FC4E07"), sorting = "descending", add = "segments", add.params = list(color = "lightgray", size = 2), # 更改棒(线条)颜色和大小 group = "cyl", dot.size = 6, label = round(dfm$mpg_z,1), font.label = list(color = "white", size = 9, vjust = 0.5), ggtheme = theme_pubr() )+ geom_hline(yintercept = 0, linetype = 2, color = "lightgray") #绘制水平线
Cleveland点图
ggdotchart(dfm, x = "name", y = "mpg", color = "cyl", palette = c("#00AFBB", "#E7B800", "#FC4E07"), sorting = "descending", rotate = TRUE, dot.size = 2, y.text.col = TRUE, ggtheme = theme_pubr() )+ theme_cleveland() #Cleveland主题
参考
https://rpkgs.datanovia.com/ggpubr/