本文轉載自“ 阿虎定量筆記“,作者鄭連虎,轉載己獲授權。其他人轉載請聯繫原作者,點我跳轉原文。
全文註釋
# 示例參考幫助文檔
# 我用Rstudio重現了所有示例
# 初識R語言,請戳我的原創推文“R語言入門”與“一起來學R”
# 使用Stata繪圖,請戳我的原創推文“Stata繪圖:37條外部命令”
# 本文以字母順序排序程序包
# 更多用於R語言繪圖的程序包,歡迎在留言區補充
cartogram
扭曲的地圖,以傳達統計信息
開發
Sebastian Jeworutzki,
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,
示例
# 安裝並加載包
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
示例
# 安裝並加載包
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=, file.output = F)
corrgram
相關係數矩陣
開發
Kevin Wright,
示例
# 安裝並加載包
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,
Viliam Simko,
示例
# 安裝並加載包
install.packages("corrplot")
library(corrplot)
# 計算相關係數
mycor <- cor(mtcars)
# 繪製相關係數矩陣圖
corrplot.mixed(mycor, upper = "ellipse")
dygraphs
時間序列數據的可視化
開發
Dan Vanderkam;
JJ Allaire,
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,
E. D. Chow
示例
# 安裝並加載包
install.packages("ellipse")
library(ellipse)
# 繪製相關係數矩陣圖
data(mtcars)
fit <- lm(mpg ~ ., mtcars)
plotcorr(summary(fit, correlation = TRUE)$correlation)
fmsb
雷達圖
開發
Minato Nakazawa
示例
# 安裝並加載包
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,
示例
# 安裝並加載包
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,
Jason Crowley,
Di Cook,
Heike Hofmann,
Hadley Wickham,
Francois Briatte,
Moritz Marbach,
Edwin Thoen,
Amos Elberg,
Joseph Larmarange,
示例
# 安裝並加載包
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,
Winston Chang,
示例
# 安裝並加載包
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,
示例
# 安裝並加載包
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,
Nicholas Lewin-Koh;
Martin Maechler,
Deepayan Sarkar,
示例
# 安裝並加載包
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,
示例
# 安裝並加載包
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,
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,
Kimberly Speerschneider
示例
# 安裝並加載包
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,
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,
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,
Chris Parmer,
Toby Hocking,
Scott Chamberlain,
Karthik Ram,
Marianne Corvellec,
Pedro Despouy,
示例
# 安裝並加載包
install.packages("plotly")
library(plotly)
# 查看火山數據
head(volcano)
# 繪製火山的三維交互圖
p=plot_ly(z = volcano, type = "surface")
p
qcc
統計質量控制
開發
Luca Scrucca,
Greg Snow,
Peter Bloomfield,
示例
# 安裝並加載包
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,
示例
# 安裝並加載包
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,
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,
示例
# 安裝並加載包
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,
示例
# 安裝並加載包
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,
Romain Francois,
示例
# 安裝並加載包
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專刊 腸道指揮大腦
系列教程:微生物組入門 Biostar 微生物組 宏基因組
專業技能:學術圖表高分文章生信寶典 不可或缺的人
一文讀懂:宏基因組 寄生蟲益處 進化樹
必備技能:提問 搜索 Endnote
文獻閱讀 熱心腸 SemanticScholar Geenmedical
擴增子分析:圖表解讀 分析流程 統計繪圖
16S功能預測 PICRUSt FAPROTAX Bugbase Tax4Fun
在線工具:16S預測培養基 生信繪圖
科研經驗:雲筆記 雲協作 公眾號
編程模板: Shell R Perl
生物科普: 腸道細菌人體上的生命生命大躍進 細胞暗戰 人體奧祕
寫在後面
為鼓勵讀者交流、快速解決科研困難,我們建立了“宏基因組”專業討論群,目前己有國內外5000+ 一線科研人員加入。參與討論,獲得專業解答,歡迎分享此文至朋友圈,並掃碼加主編好友帶你入群,務必備註“姓名-單位-研究方向-職稱/年級”。PI請明示身份,另有海內外微生物相關PI群供大佬合作交流。技術問題尋求幫助,首先閱讀《如何優雅的提問》學習解決問題思路,仍未解決群內討論,問題不私聊,幫助同行。
學習16S擴增子、宏基因組科研思路和分析實戰,關注“宏基因組”
點擊閱讀原文,跳轉最新文章目錄閱讀