大家在给文章作图的时候是不是会感到很痛苦,不知道该怎么调色,也对图形样式的选择感到迷茫。今天给大家介绍一个具有强大功能的绘图R包ggpubr。它整合了目前基本流行的图形样式,包括箱线图、气泡图、柱状图、散点图、密度图、环状图、火山图甚至可以为你安排布局设计表格。
安装ggpubr很简单,但是需要注意的是要重新安装我们自带的rlang包,不然很多功能无法实现。
接下来我们介绍下它的具体绘图的函数:
1. ggtexttable 绘制表格。其中主要的参数是theme = ttheme(type),其type的值包括c("default","blank", "classic", "minimal", "light","lBlack", "lBlue", "lRed", "lGreen","lViolet", "lCyan", "lOrange", "lBlackWhite","lBlueWhite", "lRedWhite", "lGreenWhite","lVioletWhite", "lCyanWhite", "lOrangeWhite","mBlack", "mBlue", "mRed", "mGreen","mViolet", "mCyan", "mOrange","mBlackWhite", "mBlueWhite", "mRedWhite","mGreenWhite", "mVioletWhite", "mCyanWhite","mOrangeWhite" ).以上的表格类型如果感兴趣可以自行去试试那个更好看。
我们举两个实例:
# data
df <- head(iris)
# Default table
# Remove row namesusing rows = NULL
ggtexttable(df, rows= NULL)
# Medium blue(mBlue) theme
ggtexttable(df, rows= NULL, theme = ttheme("mBlue"))
2. ggviolin绘制小提琴图,具体的参数如下:
其中:
orientation负责图形的方向。主要值c("vertical","horizontal", "reverse")。
draw_quantiles 分位数的设置。主要值:0.5,0.27,0.75.
add 添加元素。其值包括:"none","dotplot", "jitter", "boxplot","point", "mean", "mean_se", "mean_sd","mean_ci", "mean_range", "median","median_iqr", "median_mad", "median_range"。
fill填充小提琴图。可以是各种颜色。
示例如下:
# Load data
data("ToothGrowth")
df <- ToothGrowth
# Basic plot
#
ggviolin(df, x ="dose", y = "len")
代码语言:javascript复制ggviolin(df, "dose", "len", add = "jitter",
代码语言:javascript复制 draw_quantiles = 0.5)
ggviolin(df,"dose", "len", fill = "dose",
palette = c("#00AFBB","#E7B800", "#FC4E07"),
add = "boxplot", add.params =list(fill = "white"))
ggviolin(df,"dose", "len", color = "supp",
palette = c("#00AFBB","#E7B800"), add = "boxplot")#子集的绘制
3. ggballoonplot气泡图的绘制。
其中主要的参数设置:
代码语言:javascript复制# Define color palette
代码语言:javascript复制my_cols <- c("#0D0887FF", "#6A00A8FF", "#B12A90FF",
代码语言:javascript复制"#E16462FF", "#FCA636FF", "#F0F921FF")
代码语言:javascript复制
代码语言:javascript复制# Standard contingency table
代码语言:javascript复制#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
代码语言:javascript复制# Read a contingency table: housetasks
代码语言:javascript复制# Repartition of 13 housetasks in the couple
代码语言:javascript复制data <- read.delim(
代码语言:javascript复制 system.file("demo-data/housetasks.txt", package = "ggpubr"),
代码语言:javascript复制 row.names = 1
代码语言:javascript复制 )
data
代码语言:javascript复制
代码语言:javascript复制# Basic ballon plot
代码语言:javascript复制ggballoonplot(data)
代码语言:javascript复制ggballoonplot(data, color = "#0073C2FF", fill = "#0073C2FF")
代码语言:javascript复制ggballoonplot(data, fill = "value", shape = 23)
代码语言:javascript复制 gradient_fill(c("blue", "white", "red"))
代码语言:javascript复制ggballoonplot(data, fill = "value", color = "lightgray",
代码语言:javascript复制 size = 10, show.label = TRUE)
代码语言:javascript复制 gradient_fill(c("blue", "white", "red"))
data(HairEyeColor)
data= as.data.frame(HairEyeColor)
ggballoonplot(data,x= "Hair", y = "Eye", size = "Freq",ggtheme =theme_gray()) %>�cet("Sex")#其中facet主要分组的依据
4. ggpaired 配对的散点图绘制。
代码语言:javascript复制#::::::::::::::::::::::::::::::::::::::::::
代码语言:javascript复制before <-c(200.1, 190.9, 192.7, 213, 241.4, 196.9, 172.2, 185.5, 205.2, 193.7)
代码语言:javascript复制after <-c(392.9, 393.2, 345.1, 393, 434, 427.9, 422, 383.9, 392.3, 352.2)
代码语言:javascript复制
代码语言:javascript复制d <- data.frame(before = before, after = after)
代码语言:javascript复制ggpaired(d, cond1 = "before", cond2 = "after",
代码语言:javascript复制 fill = "condition", palette = "jco")
ggpaired(d, cond1 ="before", cond2 = "after",
fill = "condition", palette ="npg", line.color = "gray", line.size = 0.4)
以上是这个包的主要绘图函数,其他的我们实现的途径很多,我就不一一进行演示了。