R语言绘图:28个实用程序包
全文注释
# 示例参考帮助文档
# 我用Rstudio重现了所有示例
# 初识R语言,请戳我的原创推文“R语言入门”与“一起来学R”
# 使用Stata绘图,请戳我的原创推文“Stata绘图:37条外部命令”
# 本文以字母顺序排序程序包
# 更多用于R语言绘图的程序包,欢迎在留言区补充
cartogram
扭曲的地图,以传达统计信息
开发
Sebastian Jeworutzki,
sebastian.jeworutzki@ruhr-uni-bochum.de;
Timothee Giraud;
Nicolas Lambert;
Roger Bivand;
Edzer Pebesma
示例
# 安装并加载包
install.packages("maptools")
library(maptools)
# 绘制非洲边界
data(wrld_simpl)
afr=wrld_simpl[wrld_simpl$REGION==2,]
plot(afr)
# 安装并加载包
install.packages("cartogram")
library(cartogram)
# 使用2005年的非洲人口数据
afr_cartogram <- cartogram(afr, "POP2005", itermax=5)
# 反映非洲人口特征
plot(afr_cartogram)
circlize
圈圈布局、弦图
开发
Zuguang Gu,
z.gu@dkfz.de
示例
# 安装并加载包
install.packages("circlize")
library(circlize)
# 生成数据
name=c(3,10,10,3,6,7,8,3,6,1,2,2,6,10,2,3,3,10,4,5,9,10)
feature=paste("feature ", c(1,1,2,2,2,2,2,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5) , sep="")
dat <- data.frame(name,feature)
dat <- with(dat, table(name, feature))
# 绘制弦图
chordDiagram(as.data.frame(dat), transparency = 0.5)
CMplot
圆形曼哈顿图
开发
LiLin-Yin
ylilin@163.com
示例
# 安装并加载包
install.packages("CMplot")
library("CMplot")
# 绘图
data(pig60K)
CMplot(pig60K[sample(1:nrow(pig60K), 10000),c(1:4)],plot.type="c",threshold=c(0.01,0.05)/nrow(pig60K),threshold.col=c('red','orange'),multracks=FALSE, chr.den.col=NULL, file.output = F)
corrgram
相关系数矩阵
开发
Kevin Wright,
kw.stat@gmail.com
示例
# 安装并加载包
install.packages("iterators")
install.packages("corrgram")
library(iterators)
library(corrgram)
# 绘制相关系数矩阵图
corrgram(mtcars, order=TRUE, lower.panel=panel.shade, upper.panel=panel.pie,text.panel=panel.txt, main="Correlogram of mtcar intercorrelations")
corrplot
相关系数矩阵
开发
Taiyun Wei,
weitaiyun@gmail.com;
Viliam Simko,
viliam.simko@gmail.com
示例
# 安装并加载包
install.packages("corrplot")
library(corrplot)
# 计算相关系数
mycor <- cor(mtcars)
# 绘制相关系数矩阵图
corrplot.mixed(mycor, upper = "ellipse")
dygraphs
时间序列数据的可视化
开发
Dan Vanderkam;
JJ Allaire,
jj@rstudio.com;
Jonathan Owen;
Daniel Gromer;
Petr Shevtsov;
Benoit Thieurmel
示例
# 安装并加载包
install.packages("dygraphs")
library(dygraphs)
# 时间序列
data=data.frame( time=c( seq(0,20,0.5), 40), value=runif(42))
str(data)
dygraph(data)
ellipse
椭圆
开发
Duncan Murdoch,
murdoch@stats.uwo.ca;
E. D. Chow
示例
# 安装并加载包
install.packages("ellipse")
library(ellipse)
# 绘制相关系数矩阵图
data(mtcars)
fit <- lm(mpg ~ ., mtcars)
plotcorr(summary(fit, correlation = TRUE)$correlation)
fmsb
雷达图
开发
Minato Nakazawa
minato-nakazawa@umin.net
示例
# 安装并加载包
install.packages("fmsb")
library(fmsb)
# 生成数据
data=as.data.frame(matrix( sample( 2:20 , 10 , replace=T) , ncol=10))
colnames(data)=c("math" , "english" , "biology" , "music" , "R-coding", "data-viz" , "french" , "physic", "statistic", "sport" )
data=rbind(rep(20,10) , rep(0,10) , data)
# 雷达图参数设置
radarchart(data,axistype=1,pcol=rgb(0.2,0.5,0.5,0.9),pfcol=rgb(0.2,0.5,0.5,0.5), plwd=4, cglcol="grey", cglty=1, axislabcol="grey", caxislabels=seq(0,20,5), cglwd=0.8,vlcex=0.8)
forecast
时间序列分析
开发
Rob J Hyndman,
Rob.Hyndman@monash.edu
示例
# 安装并加载包
install.packages("forecast")
library(forecast)
# 使用英国每月死于肺病的人数数据
fit<- auto.arima(mdeaths)
# 设置置信区间
forecast(fit, level=c(80, 95, 99), h=3)
# 绘制时间序列趋势图
plot(forecast(fit), shadecols="oldstyle")
GGally
散点图矩阵
开发
Barret Schloerke,
schloerke@gmail.com;
Jason Crowley,
crowley.jason.s@gmail.com;
Di Cook,
dicook@monash.edu;
Heike Hofmann,
hofmann@iastate.edu;
Hadley Wickham,
h.wickham@gmail.com;
Francois Briatte,
f.briatte@gmail.com;
Moritz Marbach,
mmarbach@mail.uni-mannheim.de;
Edwin Thoen,
edwinthoen@gmail.com;
Amos Elberg,
amos.elberg@gmail.com;
Joseph Larmarange,
joseph@larmarange.net
示例
# 安装并加载包
install.packages("GGally")
library(GGally)
# 绘制相关系数矩阵图
ggpairs(mtcars, columns = c("mpg", "cyl", "disp"),upper = list(continuous = wrap("cor", size = 10)), lower =list(continuous = "smooth"))
ggplot2
丰富的数据可视化
开发
Hadley Wickham,
hadley@rstudio.com;
Winston Chang,
winston@rstudio.com
示例
# 安装并加载包
install.packages("ggplot2")
library(ggplot2)
# 生成数据
variety=rep(LETTERS[1:7], each=40)
treatment=rep(c("high","low"),each=20)
note=seq(1:280)+sample(1:150, 280, replace=T)
data=data.frame(variety, treatment , note)
# 绘制箱线图
ggplot(data, aes(x=variety, y=note, fill=treatment)) +
geom_boxplot()
# 生成数据
set.seed(345)
Sector <- rep(c("S01","S02","S03","S04","S05","S06","S07"),times=7)
Year <- as.numeric(rep(c("1950","1960","1970","1980","1990","2000","2010"),each=7))
Value <- runif(49, 10, 100)
data <- data.frame(Sector,Year,Value)
# 绘制区域图
ggplot(data, aes(x=Year, y=Value, fill=Sector)) +
geom_area()
# 安装并加载包
install.packages("plotly")
install.packages("gapminder")
library(plotly)
library(gapminder)
# 绘制气泡图
p <- gapminder %>%
filter(year==1977) %>%
ggplot( aes(gdpPercap, lifeExp, size = pop, color=continent)) +
geom_point() +
scale_x_log10() +
theme_bw()
ggplotly(p)
ggridges
叠嶂图(山峦图)
开发
Claus O. Wilke,
wilke@austin.utexas.edu
示例
# 安装并加载包
install.packages("ggridges")
install.packages("ggplot2")
library(ggridges)
library(ggplot2)
# 使用钻石数据
head(diamonds)
# 绘制叠嶂图
ggplot(diamonds, aes(x = price, y = cut, fill = cut)) +
geom_density_ridges() +
theme_ridges() +
theme(legend.position = "none")
hexbin
二维直方图
开发
Dan Carr,
dcarr@voxel.galaxy.gmu.edu;
Nicholas Lewin-Koh;
Martin Maechler,
maechler@stat.math.ethz.ch;
Deepayan Sarkar,
deepayan.sarkar@r-project.org
示例
# 安装并加载包
install.packages("hexbin")
install.packages("RColorBrewer")
library(hexbin)
library(RColorBrewer)
# 生成数据
x <- rnorm(mean=1.5, 5000)
y <- rnorm(mean=1.6, 5000)
# 绘制二维散点图
bin<-hexbin(x, y, xbins=40)
my_colors=colorRampPalette(rev(brewer.pal(11,'Spectral')))
plot(bin, main="", colramp=my_colors, legend=F)
igraph
网络图
示例
# 安装并加载包
install.packages("igraph")
library(igraph)
# 生成数据
data=matrix(sample(0:1, 400, replace=TRUE, prob=c(0.8,0.2)), nrow=20)
network=graph_from_adjacency_matrix(data , mode='undirected', diag=F )
# 输出网络
par(mfrow=c(2,2), mar=c(1,1,1,1))
plot(network, layout=layout.sphere, main="sphere")
lattice
点图、核密度图、直方图、柱状图、箱线图、散点图、带状图、平行箱线图、三维图、散点图矩阵等
开发
Deepayan Sarkar,
Deepayan.Sarkar@R-project.org
示例
# 安装并加载包
install.packages("lattice")
library(lattice)
# 查看火山数据
head(volcano)
# 绘制火山的三维等高线图
contourplot(volcano)
# 绘制火山的三维水平图
levelplot(volcano)
# 绘制火山的三维线框图
wireframe(volcano)
# 查看汽车数据
head(mtcars)
# 绘制箱线图
bwplot(mtcars$mpg)
# 绘制平行坐标图
parallelplot(mtcars[1:3])
# 绘制散点图矩阵
splom(mtcars[c(1,3,4,5)])
# 绘制带状图
stripplot(mtcars$mpg~factor(mtcars$cyl))
leaflet
交互式地图
开发
Joe Cheng,
joe@rstudio.com;
Bhaskar Karambelkar;
Yihui Xie;
Hadley Wickham;
Kenton Russell;
Kent Johnson;
Barret Schloerke;
Vladimir Agafonkin;
Brandon Copeland;
Joerg Dietrich;
Benjamin Becquet;
Norkart AS;
L. Voogdt;
Daniel Montague;
Kartena AB;
Robert Kajic;
Michael Bostock
示例
# 安装并加载包
install.packages("leaflet")
library(leaflet)
# 交互式地图
m=leaflet()
m=addTiles(m)
m
likert
李克特量表数据的可视化
开发
Jason Bryer,
jason@bryer.org;
Kimberly Speerschneider
kimkspeer@gmail.com
示例
# 安装并加载包
install.packages("likert")
library(likert)
# 使用PISA量表数据
data(pisaitems)
items28 <- pisaitems[, substr(names(pisaitems), 1, 5) == "ST24Q"]
# 绘制条形图
l28 <- likert(items28)
summary(l28)
plot(l28)
maps
地图
开发
Richard A. Becker;
Allan R. Wilks;
Ray Brownrigg
示例
# 安装并加载包
install.packages("maps")
install.packages("geosphere")
library(maps)
library(geosphere)
# 绘制世界地图
map("world")
maptools
地图
开发
Roger Bivand,
Roger.Bivand@nhh.no;
Nicholas Lewin-Koh;
Edzer Pebesma;
Eric Archer;
Adrian Baddeley;
Nick Bearman;
Hans-Jörg Bibiko;
Steven Brey;
Jonathan Callahan;
German Carrillo;
Stéphane Dray;
David Forrest;
Michael Friendly;
Patrick Giraudoux;
Duncan Golicher;
Virgilio Gómez Rubio;
Patrick Hausmann;
Karl Ove Hufthammer;
Thomas Jagger;
Kent Johnson;
Sebastian Luque;
Don MacQueen;
Andrew Niccolai;
Edzer Pebesma;
Oscar Perpiñán Lamigueiro;
Tom Short;
Greg Snow;
Ben Stabler;
Murray Stokely;
Rolf Turner
示例
# 安装并加载包
install.packages("maptools")
library(maptools)
# 绘制非洲边界
data(wrld_simpl)
afr=wrld_simpl[wrld_simpl$REGION==2,]
plot(afr)
performanceAnalytics
绩效指标计算与可视化
开发
Brian G. Peterson,
brian@braverock.com;
Peter Carl
示例
# 安装并加载包
install.packages("PerformanceAnalytics")
library(PerformanceAnalytics)
# 列出待计算变量
mydata <-mtcars[c('mpg','cyl','disp','hp','drat')]
# 绘制相关系数矩阵图
chart.Correlation(mydata, histogram=TRUE, pch=19)
plotly
交互式可视化
开发
Carson Siever,
cpsievert1@gmail.com;
Chris Parmer,
chris@plot.ly;
Toby Hocking,
tdhock5@gmail.com;
Scott Chamberlain,
myrmecocystus@gmail.com;
Karthik Ram,
karthik.ram@gmail.com;
Marianne Corvellec,
marianne@plot.ly;
Pedro Despouy,
pedro@plot.ly
示例
# 安装并加载包
install.packages("plotly")
library(plotly)
# 查看火山数据
head(volcano)
# 绘制火山的三维交互图
p=plot_ly(z = volcano, type = "surface")
p
qcc
统计质量控制
开发
Luca Scrucca,
luca.scrucca@unipg.it;
Greg Snow,
greg.snow@ihc.com;
Peter Bloomfield,
Peter_Bloomfield@ncsu.edu
示例
# 安装并加载包
install.packages("qcc")
library(qcc)
# 均值为10的序列,加上白噪声
x <- rep(10, 100) + rnorm(100)
# 测试序列,均值为11
new.x <- rep(11, 15) + rnorm(15)
# 标记出新的点
qcc(x, newdata=new.x, type="xbar.one")
qqman
曼哈顿图
开发
Stephen Turner
示例
# 安装并加载包
install.packages("qqman")
library(qqman)
# 使用gwasResults数据绘制曼哈顿图
manhattan(gwasResults, chr="CHR", bp="BP", snp="SNP", p="P" )
REmap
地图
开发
Dawei Lang,
dawei.lang@supstat.com
示例
# 安装并加载包
install.packages("devtools")
install_github("lchiffon/REmap")
library(devtools)
library(REmap)
# 标注起始点
origin<-c("济南","西安","成都")
destination<-c("西安","成都","济南")
# 制作迁徙地图
dat = data.frame(origin,destination)
out = remap(dat,title = "after-graduation trip ",subtitle= "zg434")
plot(out)
scatterplot3d
三维散点图
开发
Uwe Ligges,
ligges@statistik.tu-dortmund.de;
Martin Maechler;
Sarah Schnackenberg
示例
# 安装并加载包
install.packages("scatterplot3d")
library(scatterplot3d)
# 生成数据
x1=round(rnorm(100,mean=80,sd=1))
x2=round(rnorm(100,mean=80,sd=5))
x3=round(rnorm(100,mean=80,sd=10))
x=data.frame(x1,x2,x3)
# 绘制三维散点图
scatterplot3d(x[1:3])
TeachingDemos
脸谱图
开发
Greg Snow,
538280@gmail.com
示例
# 安装并加载包
install.packages("TeachingDemos")
library(TeachingDemos)
# 生成数据
x1=round(rnorm(100,mean=80,sd=1))
x2=round(rnorm(100,mean=80,sd=5))
x3=round(rnorm(100,mean=80,sd=10))
x=data.frame(x1,x2,x3)
# 绘制脸谱图
faces(x)
treemap
树图
开发
Martijn Tennekes,
mtennekes@gmail.com
示例
# 安装并加载包
install.packages("treemap")
library(treemap)
# 生成数据
group=c("group-1","group-2","group-3")
value=c(13,5,22)
data=data.frame(group,value)
# 绘制树图
treemap(data, index="group", vSize="value", type="index")
vioplot
小提琴图
开发
Daniel Adler,
dadler@uni-goettingen.de;
Romain Francois,
francoisromain@free.fr
示例
# 安装并加载包
install.packages("vioplot")
library(vioplot)
# 生成数据
treatment=c(rep("A", 40) , rep("B", 40) , rep("C", 40) )
value=c( sample(2:5, 40 , replace=T) , sample(c(1:5,12:17), 40 , replace=T), sample(1:7, 40 , replace=T) )
data=data.frame(treatment,value)
# 绘制小提琴图
with(data , vioplot( value[treatment=="A"] , value[treatment=="B"], value[treatment=="C"], col=rgb(0.1,0.4,0.7,0.7) , names=c("A","B","C") ))
运行环境信息
> sessionInfo()
R version 3.5.0 (2018-04-23)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=Chinese (Simplified)_China.936 LC_CTYPE=Chinese (Simplified)_China.936 LC_MONETARY=Chinese (Simplified)_China.936
[4] LC_NUMERIC=C LC_TIME=Chinese (Simplified)_China.936
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] CMplot_3.3.1 vioplot_0.2 sm_2.2-5.5 treemap_2.4-2
[5] TeachingDemos_2.10 scatterplot3d_0.3-41 REmap_0.3.2 devtools_1.13.5
[9] qqman_0.1.4 qcc_2.7 PerformanceAnalytics_1.5.2 xts_0.11-1
[13] zoo_1.8-3 maptools_0.9-3 geosphere_1.5-7 maps_3.3.0
[17] likert_1.3.5 xtable_1.8-2 leaflet_2.0.2 lattice_0.20-35
[21] igraph_1.2.1 RColorBrewer_1.1-2 hexbin_1.27.2 ggridges_0.5.0
[25] bindrcpp_0.2.2 gapminder_0.3.0 plotly_4.8.0 ggplot2_3.0.0
[29] sp_1.3-1
loaded via a namespace (and not attached):
[1] httr_1.3.1 tidyr_0.8.1 jsonlite_1.5 viridisLite_0.3.0 shiny_1.1.0 assertthat_0.2.0 yaml_2.1.19
[8] backports_1.1.2 pillar_1.2.3 glue_1.2.0 quadprog_1.5-5 digest_0.6.15 promises_1.0.1 colorspace_1.3-2
[15] htmltools_0.3.6 httpuv_1.4.3 plyr_1.8.4 psych_1.8.4 XML_3.98-1.16 pkgconfig_2.0.1 calibrate_1.7.2
[22] purrr_0.2.5 scales_1.0.0 later_0.7.2 git2r_0.21.0 tibble_1.4.2 withr_2.1.2 lazyeval_0.2.1
[29] cli_1.0.0 mnormt_1.5-5 magrittr_1.5 crayon_1.3.4 mime_0.5 evaluate_0.10.1 memoise_1.1.0
[36] nlme_3.1-137 MASS_7.3-49 foreign_0.8-70 tools_3.5.0 data.table_1.11.2 gridBase_0.4-7 stringr_1.3.1
[43] munsell_0.5.0 compiler_3.5.0 rlang_0.2.0 grid_3.5.0 rstudioapi_0.7 rjson_0.2.20 htmlwidgets_1.2
[50] crosstalk_1.0.0 rmarkdown_1.9 tcltk_3.5.0 labeling_0.3 gtable_0.2.0 curl_3.2 reshape2_1.4.3
[57] R6_2.2.2 gridExtra_2.3 knitr_1.20 dplyr_0.7.5 utf8_1.1.4 rprojroot_1.3-2 bindr_0.1.1
[64] stringi_1.1.7 parallel_3.5.0 Rcpp_0.12.18 tidyselect_0.2.4
猜你喜欢
10000+:菌群分析 宝宝与猫狗 梅毒狂想曲 提DNA发Nature Cell专刊 肠道指挥大脑
文献阅读 热心肠 SemanticScholar Geenmedical
16S功能预测 PICRUSt FAPROTAX Bugbase Tax4Fun
生物科普: 肠道细菌 人体上的生命 生命大跃进 细胞暗战 人体奥秘
写在后面
为鼓励读者交流、快速解决科研困难,我们建立了“宏基因组”专业讨论群,目前己有国内外2200+ 一线科研人员加入。参与讨论,获得专业解答,欢迎分享此文至朋友圈,并扫码加主编好友带你入群,务必备注“姓名-单位-研究方向-职称/年级”。技术问题寻求帮助,首先阅读《如何优雅的提问》学习解决问题思路,仍末解决群内讨论,问题不私聊,帮助同行。
学习16S扩增子、宏基因组科研思路和分析实战,关注“宏基因组”