(图片来源网络,侵删)
作者:郑连虎来源:阿虎定量笔记本文约2500字,建议阅读20+分钟本文为大家介绍了关于R语言绘图方面28个实用程序包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-Yinylilin@163.com示例# 安装并加载包install.packages(\"CMplot\")library(\"CMplot\")# 绘图CMplot(gwasResults, plot.type=\"c\", r=1.6, cir.legend=TRUE, outward=TRUE, cir.legend.col=\"black\", cir.chr.h=.1 ,chr.den.col=\"orange\", file=\"jpg\", memo=\"\", dpi=300, chr.labels=seq(1,22))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 Nakazawaminato-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)mlikert李克特量表数据的可视化开发Jason Bryer,jason@bryer.org;Kimberly Speerschneiderkimkspeer@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\")pqcc统计质量控制开发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)# 测试序列,均值为11new.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\") ))
0 评论