vlambda博客
学习文章列表

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"))

R语言 轻松绘制顶级配图

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"))

R语言 轻松绘制顶级配图




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

R语言 轻松绘制顶级配图


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)


R语言 轻松绘制顶级配图


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 cyl color = "white", # Set bar border colors to white palette = "jco", # jco journal color palett. see ?ggpar sort.val = "desc", # Sort the value in dscending order sort.by.groups = FALSE, # Don't sort inside each group x.text.angle = 90 # Rotate vertically x axis texts)

R语言 轻松绘制顶级配图

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 cyl color = "white", # Set bar border colors to white palette = "jco", # jco journal color palett. see ?ggpar sort.val = "asc", # Sort the value in dscending order sort.by.groups = TRUE, # Sort inside each group x.text.angle = 90 # Rotate vertically x axis texts )

R语言 轻松绘制顶级配图

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_level color = "white", # Set bar border colors to white palette = "jco", # jco journal color palett. see ?ggpar sort.val = "asc", # Sort the value in ascending order sort.by.groups = FALSE, # Don't sort inside each group x.text.angle = 90, # Rotate vertically x axis texts ylab = "MPG z-score", xlab = FALSE, legend.title = "MPG Group")

R语言 轻松绘制顶级配图

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_level color = "white", # Set bar border colors to white palette = "jco", # jco journal color palett. see ?ggpar sort.val = "desc", # Sort the value in descending order sort.by.groups = FALSE, # Don't sort inside each group x.text.angle = 90, # Rotate vertically x axis texts ylab = "MPG z-score", legend.title = "MPG Group", rotate = TRUE, ggtheme = theme_minimal() )

R语言 轻松绘制顶级配图


04


ggdotchart{ggpubr}

Dot chart

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 groups palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette sorting = "ascending", # Sort value in descending order add = "segments", # Add segments from y = 0 to dots ggtheme = theme_pubr() # ggplot2 theme )

R语言 轻松绘制顶级配图

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 groups palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette sorting = "descending", # Sort value in descending order add = "segments", # Add segments from y = 0 to dots rotate = TRUE, # Rotate vertically group = "cyl", # Order by groups dot.size = 6, # Large dot size label = round(dfm$mpg), # Add mpg values as dot labels font.label = list(color = "white", size = 9, vjust = 0.5), # Adjust label parameters ggtheme = theme_pubr() # ggplot2 theme )

R语言 轻松绘制顶级配图

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_z", color = "cyl", # Color by groups palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette sorting = "descending", # Sort value in descending order add = "segments", # Add segments from y = 0 to dots add.params = list(color = "lightgray", size = 2), # Change segment color and size group = "cyl", # Order by groups dot.size = 6, # Large dot size label = round(dfm$mpg_z,1), # Add mpg values as dot labels font.label = list(color = "white", size = 9, vjust = 0.5), # Adjust label parameters ggtheme = theme_pubr() # ggplot2 theme )+ geom_hline(yintercept = 0, linetype = 2, color = "lightgray")

R语言 轻松绘制顶级配图

ggdotchart(dfm, x = "name", y = "mpg", color = "cyl", # Color by groups palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette sorting = "descending", # Sort value in descending order rotate = TRUE, # Rotate vertically dot.size = 2, # Large dot size y.text.col = TRUE, # Color y text by groups ggtheme = theme_pubr() # ggplot2 theme )+ theme_cleveland()


参考来源http://www.sthda.com/english/articles/24-ggpubr-publication-ready-plots/


【往期回顾推荐】








《R数据科学》是一本专门讲解tidyverse相关包的书籍,主要涉及dplyr、tidyr、ggplot2、purrr等,非常值得学习,基本上此一本书可以解答数据处理的大部分问题