R语言 轻松绘制顶级配图
说明:hhh{bbb}中hhh指函数,bbb指包名。
01
—
ggdensity{ggpubr}
密度图
library(ggpubr)## 创建数据set.seed(1024)dt = data.frame(sex = factor(rep(c("F", "M"), each=200)),weight = c(rnorm(200, 55), rnorm(200, 58)))head(dt, 3)# sex weight# 1 F 54.22134# 2 F 54.61052# 3 F 52.96620## 制图ggdensity(dt, x = "weight",add = "mean", rug = TRUE,color = "sex", fill = "sex",palette = c("#00AFBB", "#E7B800"))
ggdensity
set.seed(1024)dt = data.frame(sex = factor(rep(c("F", "M"), each=200)),weight = c(rnorm(200, 55), rnorm(200, 58)))gghistogram(dt, x = "weight",add = "mean", rug = TRUE,color = "sex", fill = "sex",palette = c("#00AFBB", "#E7B800"))
02
—
ggboxplot{ggpubr}
Box plots
library(ggpubr)## 数据data("ToothGrowth")df <- ToothGrowthhead(df, 4)# len supp dose# 1 4.2 VC 0.5# 2 11.5 VC 0.5# 3 7.3 VC 0.5# 4 5.8 VC 0.5# 图p <- ggboxplot(df, x = "dose", y = "len",color = "dose", palette =c("#00AFBB", "#E7B800", "#FC4E07"),add = "jitter", shape = "dose")p
library(ggpubr)## 数据data("ToothGrowth")df <- ToothGrowthhead(df, 4)# len supp dose# 1 4.2 VC 0.5# 2 11.5 VC 0.5# 3 7.3 VC 0.5# 4 5.8 VC 0.5## 图p <- ggboxplot(df, x = "dose", y = "len",color = "dose", palette =c("#00AFBB", "#E7B800", "#FC4E07"),add = "jitter", shape = "dose")my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )p + stat_compare_means(comparisons = my_comparisons)+stat_compare_means(label.y = 50)
03
—
ggbarplot{ggpubr}
bar plots
library(ggpubr)# 数据data("mtcars")dfm <- mtcars# Convert the cyl variable to a factor 转换数据类型dfm$cyl <- as.factor(dfm$cyl)# Add the name columsdfm$name <- rownames(dfm)# Inspect the datahead(dfm[, c("name", "wt", "mpg", "cyl")])ggbarplot(dfm, x = "name", y = "mpg",fill = "cyl", # change fill color by cylcolor = "white", # Set bar border colors to whitepalette = "jco", # jco journal color palett. see ?ggparsort.val = "desc", # Sort the value in dscending ordersort.by.groups = FALSE, # Don't sort inside each groupx.text.angle = 90 # Rotate vertically x axis texts)
library(ggpubr)# 数据data("mtcars")dfm <- mtcars# Convert the cyl variable to a factor 转换数据类型dfm$cyl <- as.factor(dfm$cyl)# Add the name columsdfm$name <- rownames(dfm)# Inspect the datahead(dfm[, c("name", "wt", "mpg", "cyl")])ggbarplot(dfm, x = "name", y = "mpg",fill = "cyl", # change fill color by cylcolor = "white", # Set bar border colors to whitepalette = "jco", # jco journal color palett. see ?ggparsort.val = "asc", # Sort the value in dscending ordersort.by.groups = TRUE, # Sort inside each groupx.text.angle = 90 # Rotate vertically x axis texts)
library(ggpubr)# Calculate the z-score of the mpg datadfm$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"))# Inspect the datahead(dfm[, c("name", "wt", "mpg", "mpg_z", "mpg_grp", "cyl")])ggbarplot(dfm, x = "name", y = "mpg_z",fill = "mpg_grp", # change fill color by mpg_levelcolor = "white", # Set bar border colors to whitepalette = "jco", # jco journal color palett. see ?ggparsort.val = "asc", # Sort the value in ascending ordersort.by.groups = FALSE, # Don't sort inside each groupx.text.angle = 90, # Rotate vertically x axis textsylab = "MPG z-score",xlab = FALSE,legend.title = "MPG Group")
library(ggpubr)# Calculate the z-score of the mpg datadfm$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"))# Inspect the datahead(dfm[, c("name", "wt", "mpg", "mpg_z", "mpg_grp", "cyl")])ggbarplot(dfm, x = "name", y = "mpg_z",fill = "mpg_grp", # change fill color by mpg_levelcolor = "white", # Set bar border colors to whitepalette = "jco", # jco journal color palett. see ?ggparsort.val = "desc", # Sort the value in descending ordersort.by.groups = FALSE, # Don't sort inside each groupx.text.angle = 90, # Rotate vertically x axis textsylab = "MPG z-score",legend.title = "MPG Group",rotate = TRUE,ggtheme = theme_minimal())
04
—
ggdotchart{ggpubr}
Dot chart
library(ggpubr)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"))ggdotchart(dfm, x = "name", y = "mpg",color = "cyl",palette = c("#00AFBB", "#E7B800", "#FC4E07"),sorting = "ascending",add = "segments",ggtheme = theme_pubr())
library(ggpubr)# Calculate the z-score of the mpg datadfm$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"))ggdotchart(dfm, x = "name", y = "mpg",color = "cyl", # Color by groupspalette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palettesorting = "descending", # Sort value in descending orderadd = "segments", # Add segments from y = 0 to dotsrotate = TRUE, # Rotate verticallygroup = "cyl", # Order by groupsdot.size = 6, # Large dot sizelabel = round(dfm$mpg), # Add mpg values as dot labelsfont.label = list(color = "white", size = 9,vjust = 0.5), # Adjust label parametersggtheme = theme_pubr() # ggplot2 theme)
library(ggpubr)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"))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")
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()
参考来源http://www.sthda.com/english/articles/24-ggpubr-publication-ready-plots/
