ggpubr包学习(二)

作者: Zee_李海海 | 来源:发表于2021-02-26 09:32 被阅读0次

Box plots (箱体图)

library(ggpubr)
data("ToothGrowth")
df<-ToothGrowth
p <- ggboxplot(df, x = "dose", y = "len",color = "dose", palette =c("#00AFBB", "#E7B800", "#FC4E07"),add = "jitter", shape = "dose")
p
Boxplot.png
 # Add p-values comparing groups
 # Specify the comparisons you want
my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )
p1<-p + stat_compare_means(comparisons = my_comparisons) # Add pairwise comparisons p-value
  +stat_compare_means(label.y = 50)     # Add global p-value
p1
Boxplot01.png

Violin plot with boxplot (小提琴图)

# Change fill color by groups: dose
# add boxplot with white fill color

# Violin plots with box plots inside
# :::::::::::::::::::::::::::::::::::::::::::::::::::
# Change fill color by groups: dose
# add boxplot with white fill color
p2<-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")+ # Add significance levels
  stat_compare_means(label.y = 50)                                      # Add global the p-value
p2 
violin.png

Density plot (密度图)

set.seed(1234)
wdata = data.frame(
   sex = factor(rep(c("F", "M"), each=200)),
   weight = c(rnorm(200, 55), rnorm(200, 58)))
head(wdata, 4)
#>   sex   weight
#> 1   F 53.79293
#> 2   F 55.27743
#> 3   F 56.08444
#> 4   F 52.65430
# Change outline and fill colors by groups ("sex")
# Use custom palette
p3<-ggdensity(wdata, x = "weight",add = "mean", rug = TRUE,color = "sex", fill = "sex",palette = c("#00AFBB", "#E7B800"))
Density plot.png

Histogram plot (直方图)

# Histogram plot with mean lines and marginal rug
# Change outline and fill colors by groups ("sex")
# Use custom color palette
p4<-gghistogram(wdata, x = "weight", add = "mean", rug = TRUE,color = "sex", fill = "sex",palette = c("#00AFBB", "#E7B800"))
p4
Histogram plot.png

以上图片,可以通过patchwork包将其进行排版组合

Example

library(patchwork)
plot<-p+p1+p3
plot
plot12.png
plot1<-p3/p4
plot1
plot13.png
plot2<-p / p1 | p4
plot2
plot14.png

参考链接:https://rpkgs.datanovia.com/ggpubr/

相关文章

网友评论

    本文标题:ggpubr包学习(二)

    本文链接:https://www.haomeiwen.com/subject/tajufltx.html