简介
越学习包ggpubr越感觉其功能强大,本文主要讲解一下如何修改图形参数,我们知道ggplot2有着十分强大的绘图功能,但是其参数之复杂足以让人望而却步。ggpubr提供的函数ggpar()可以十分轻松地让我们修改图形参数,并且可以无缝对接到ggplot图形中。ggpar()可以修改以下图形参数:
- 图形标题,包括坐标轴标题以及图例标题
- 图例的位置以及外观
- 颜色必需的能修改
- 坐标轴limits
- 坐标转换
- 坐标刻度
- 主题
- 翻转等
安装包
#two ways to isntall the ggpubr package
install.packages("ggpubr")
#install the packages from the Github
devtools::install_github("kassambara/ggpubr")
绘图
library(ggpubr)#load ggpubr
#basic plots
p <- ggboxplot(ToothGrowth, x="dose", y="len", color="dose")
p
#add grids by the function grids()
p+grids(linetype="dashed")
#add panel borders lines by the function border()
p+border("black")
#change background color
p+bgcolor("#BFD5E3")+border("#BFD5E3")
修改标题以及坐标轴标签
p2 <- ggpar(p,
title = "Box plot created with ggpubr",
subtitle = "Length by dose",
caption = "Source: ggpubr",
xlab = "Dose (mg)",
ylab = "Teeth length",
legend.title = "Dose (mg)")
p2
修改标题以及标签的字体
ggpar(p2,
font.title = c(14, "bold.italic", "red"),
font.subtitle = c(10, "orange"),
font.caption = c(10, "orange"),
font.x = c(14, "blue"),
font.y = c(14, "#993333"))
当然也可以直接用函数font()
p2+
font("title", size = 14, color = "red", face = "bold.italic")+
font("subtitle", size = 10, color = "orange")+
font("caption", size = 10, color = "orange")+
font("xlab", size = 12, color = "blue")+
font("ylab", size = 12, color = "#993333")
从上面可以看出我们可以一次性完成添加标题以及修改字体
ggpar(p,
title = "Plot of length \n by dose",
xlab = "Dose (mg)",
legend.title = "Dose (mg)",
font.title = c(14, "bold.italic", "red"),
font.x = c(14, "bold", "#2E9FDF"),
font.y = c(14, "bold", "#E7B800"))
修改图例位置与外观
ggpar(p,
legend = "right", legend.title = "Dose (mg)")+
font("legend.title", color="blue", face = "bold")+
font("legend.text", color = "red")
颜色修改
ggpar()中有一个参数palette,即调色板,除了可以我们自定义颜色外,还可以调用RColorBrewer中的调色板以及包ggsci中的专门用于学术杂志的配色。
#use custom color palette
ggpar(p, palette = c("#00AFBB", "#E7B800", "#FC4E07"))
#use the RColorBrewer palette
ggpar(p, palette = "Dark2")
ggpar(p, palette = "grey")
#use the ggsci palette
ggpar(p, palette = "npg")#nature
当然也可以直接用ggpubr中的函数color_palette()以及fill_palette()
#jco color palette
p+color_palette("jco")
#custom color
p+color_palette(c("#00AFBB", "#E7B800", "#FC4E07"))
颜色梯度
ggpubr提供了两个函数用来处理颜色梯度:gradient_color()和gradient_fill()
通过一个例子来看如何运作
#first create a scatter plot
p3 <- ggscatter(mtcars, x="wt", y="mpg", color="mpg", size = 2)
#change the gradient color
#use one custom color
p3+gradient_color("red")
#use two colors
p3+gradient_color(c("blue", "red"))
#three colors
p3+gradient_color(c("blue", "white", "red"))
#use the RColorBrewer palette
p3+gradient_color("RdYlBu")
gradient_fill()用法也一样,不过是填充,这里就不演示了。
修改坐标轴limits、scales
#change y axis limits
ggpar(p, ylim = c(0, 50))
#change y axis cale to log2
ggpar(p, yscale = "log2")
#format axis scale
ggpar(p, yscale = "log2", format.scale = TRUE)#format.scale=TRUE说明y轴刻度也会scale
#也可以直接用yscale()
p+yscale("log2", .format = TRUE)
自定义坐标轴标签及刻度
#change the font of x and y axis texts
#rotate x and y texts
p+
font("xy.text", size = 12, color = "blue", face = "bold")+
rotate_x_text(45)+
rotate_y_text(45)
#remove ticks and axis texts
p+rremove("ticks")+
rremove("axis.text")
修改主题
默认主题为theme_pubr(),可以调用包ggthemes里面的主题
library(ggthemes)
p+ggthemes::theme_economist()
移除ggplot组件
通过ggpubr::rremove()可以移除组件:
p <- ggboxplot(ToothGrowth, x="dose", y="len", ggtheme = theme_igray())
p
p+rremove("grid")
SessionInfo
sessionInfo()
## R version 3.4.1 (2017-06-30)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 16.04.3 LTS
##
## Matrix products: default
## BLAS: /usr/lib/atlas-base/atlas/libblas.so.3.0
## LAPACK: /usr/lib/atlas-base/atlas/liblapack.so.3.0
##
## locale:
## [1] LC_CTYPE=zh_CN.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=zh_CN.UTF-8 LC_COLLATE=zh_CN.UTF-8
## [5] LC_MONETARY=zh_CN.UTF-8 LC_MESSAGES=zh_CN.UTF-8
## [7] LC_PAPER=zh_CN.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=zh_CN.UTF-8 LC_IDENTIFICATION=C
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] ggthemes_3.4.0 ggpubr_0.1.5 magrittr_1.5 ggplot2_2.2.1
##
## loaded via a namespace (and not attached):
## [1] Rcpp_0.12.12 bindr_0.1 knitr_1.17
## [4] munsell_0.4.3 colorspace_1.3-2 R6_2.2.2
## [7] rlang_0.1.2 stringr_1.2.0 plyr_1.8.4
## [10] dplyr_0.7.3 tools_3.4.1 grid_3.4.1
## [13] gtable_0.2.0 htmltools_0.3.6 yaml_2.1.14
## [16] lazyeval_0.2.0 rprojroot_1.2 digest_0.6.12
## [19] assertthat_0.2.0 tibble_1.3.4 bindrcpp_0.2
## [22] ggsci_2.7 RColorBrewer_1.1-2 purrr_0.2.3
## [25] glue_1.1.1 evaluate_0.10.1 rmarkdown_1.6
## [28] labeling_0.3 stringi_1.1.5 compiler_3.4.1
## [31] scales_0.5.0 backports_1.1.0 pkgconfig_2.0.1
联系方式:
wechat: yt056410
Email: tyan@zju.edu.cn
QQ: 1051927088
GitHub: https://github.com/YTLogos
简书: http://www.jianshu.com/u/bd001545cf0b
博客: https://ytlogos.github.io/
个人简介:
严涛
浙江大学作物遗传育种在读研究生(生物信息学方向)
伪码农,R语言爱好者,爱开源