R语言绘图:实用脑科学数据可视化包

文章来源于微信公众号(茗创科技),欢迎有兴趣的朋友搜索关注

导读

本文整理了R语言绘图中使用频率较高的程序包,每个程序包都附载相应的参考来源链接(链接里有实现绘图的脚本)和下载链接。另:茗创科技为大家提供免会员极速下载服务,需要相应程序包的小伙伴可以私信茗创科技周翊工程师微信号MCKJ-zhouyi或17373158786。

gganimate:在R中绘制动态图

gganimate在CRAN上可通过install.packages('gganimate')来安装。如果要安装开发版本,可以直接使用devtools从github安装:

# install.packages('devtools')devtools::install_github('thomasp85/gganimate')

示例1 及其代码

图片

library(ggplot2)library(gganimate)
ggplot(mtcars, aes(factor(cyl), mpg)) +  geom_boxplot() +  # Here comes the gganimate code  transition_states(    gear,    transition_length = 2,    state_length = 1  ) +  enter_fade() +  exit_shrink() +  ease_aes('sine-in-out')

示例2及其代码

图片

library(gapminder)
ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, colour = country)) +  geom_point(alpha = 0.7, show.legend = FALSE) +  scale_colour_manual(values = country_colors) +  scale_size(range = c(2, 12)) +  scale_x_log10() +  facet_wrap(~continent) +  # Here comes the gganimate specific bits  labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'life expectancy') +  transition_time(year) +  ease_aes('linear')

参考来源:

https://gganimate.com/

https://github.com/thomasp85/gganimate

gganimate程序包下载链接:

https://CRAN.R-project.org/package=gganimate

IPWboxplot:如何绘制缺失值的箱型图

IPW.boxplot函数使用IPW分位数绘制适用于丢失数据修改后的箱线图。应用该函数会得到一个统计概括列表。默认情况下,使用该函数得到经过调整的箱线图和通过逆概率加权计算的统计数据。

图片

参考来源:

https://cran.r-project.org/web/packages/IPWboxplot/IPWboxplot.pdf

https://cran.r-project.org/web/packages/IPWboxplot/vignettes/my-vignette.pdf

IPWboxplot程序包下载链接:

https://CRAN.R-project.org/package=IPWboxplot

ggplot2:用图形语法绘制精美数据图的程序包

ggplot2的核心理念是将绘图与数据分离,数据相关的绘图与数据无关的绘图分离,是按图层作图,有利于结构化思维,同时它保有命令式作图的调整函数,使其更具灵活性,绘制出来的图形美观,同时避免繁琐细节。

图片

更多创新思路,可以查看ggplot2中丰富的扩展生态系统。也能给自己提供更多绘图灵感。

图片

截图来源网址:

https://exts.ggplot2.tidyverse.org/gallery/

参考来源:

https://ggplot2.tidyverse.org/

https://github.com/tidyverse/ggplot2

ggplot2程序包下载链接:

https://CRAN.R-project.org/package=ggplot2

Corrplot:绘制相关矩阵可视化图

Corrplot使用起来非常方便,并提供了可视化方法、图形布局、颜色、文本标签等丰富的绘图选项。此外,还提供p值和置信区间,以帮助我们确定相关性的统计显著性。

图片

 

参考来源:

https://github.com/taiyun/corrplot

https://cran.r-project.org/web/packages/corrplot/corrplot.pdf

Corrplot程序包下载链接:

https://CRAN.R-project.org/package=corrplot

handyplots:快速绘制散点图、Q-Q图、箱型图、残差图、置信区间等

以下是handyplots程序包的使用手册,搭配作图过程使用。

图片

参考来源:

https://cran.r-project.org/web/packages/handyplots/handyplots.pdf

handyplots程序包下载链接:

https://CRAN.R-project.org/package=handyplots

OutliersO3:绘制数据集中的异常值O3图

OutliersO3能够识别数据集变量中所有组合的潜在异常值。下面这张图是来自Election(2005)数据集的四个变量的平行坐标图。

图片

# Prepare O3plot resultslibrary(OutliersO3)data(Election2005)data <- Election2005[, c(6, 10, 17, 28)]O3p <- O3prep(data, method=c("HDo", "PCS", "BAC", "adjOut", "DDC", "MCD"))O3p1 <- O3plotM(O3p)
#Create and add the new highlighting variable to the datasetty <- O3p1$outsTable %>% filter(Combination=="c1111")ouFx <- dataouFx$oh <- rep(0, nrow(data))ouFx[ty$Case,"oh"] <- "A"
#Use plotting code from O3plotMn1 <- ncol(data)ouFx <- ouFx %>% mutate(alev = ifelse(oh == 0, 0.5, 1))gp <- ggparcoord(ouFx %>% arrange(oh), scale = "uniminmax", columns=1:n1,groupColumn="oh", alphaLines="alev") + labs(x = NULL, y = NULL) +scale_colour_manual(values = c("grey70", "red")) +theme(plot.title = element_text(size = 18, hjust = 0.5),legend.position = "none", axis.ticks.y = element_blank(),axis.text.y = element_blank())gp

参考来源:

https://cran.r-project.org/web/packages/OutliersO3/OutliersO3.pdf

https://cran.r-project.org/web/packages/OutliersO3/vignettes/PCPsO3.html

OutliersO3程序包下载链接:

https://CRAN.R-project.org/package=OutliersO3

rainbow:条形图、箱型图、彩虹图绘制

该图来自Han Lin Shang和Rob J Hyndman提出的一些新的工具来可视化功能数据和识别功能异常值。利用了稳健的主成分分析、数据深度和最高密度区域。

图片

参考来源:

https://www.robjhyndman.com/papers/iwfos08.pdf

https://cran.r-project.org/web/packages/rainbow/rainbow.pdf

rainbow程序包下载链接:

https://CRAN.R-project.org/package=rainbow

lattice:高级绘图系统

lattice绘图灵感来自于网格图,强调多变量数据。网格图形函数从一个可自定义的“设置”列表中获取各种图形参数(颜色、行类型、字体等)的默认值。

图片

图片来自于书籍《Lattice: Multivariate Data Visualization with R》Chapter14 Figure14.2

参考来源:

http://lattice.r-forge.r-project.org/

lattice程序包下载链接:

https://CRAN.R-project.org/package=lattice

brainGraph:大脑MRI数据的图论分析

brainGraph用于执行图论分析的MRI数据。使用Freesurfer分析数据(皮层厚度、体素、局部褶皱指数、表面积)、弥散张量束成像数据(例如,来自FSL)和静息态fMRI数据(例如,来自DPABI)。brainGraph包含一个用于图形可视化和数据探索图形用户界面,以及用于生成有用图形的几个函数。

图片

参考来源:

https://github.com/cwatson/brainGraph

https://cran.r-project.org/web/packages/brainGraph/brainGraph.pdf

brainGraph程序包下载链接:

https://CRAN.R-project.org/package=brainGraph

brainR:绘制大脑影像的‘misc3d’和‘rgl’包

brainR包括使用‘WebGL’,‘rgl’和‘JavaScript’命令创建3D和4D图像的功能。这个包依赖于X工具包( X toolkit:‘XTK’)。

图片

图片来自于文章G. Grabner, A. L. Janke, M. M. Budge, D. Smith, J. Pruessner, and D. L. Collins, "Symmetric atlasing and model based segmentation: an application to the hippocampus in older adults", Med Image Comput Comput Assist Interv Int Conf Med Image Comput Comput Assist Interv, vol. 9, pp. 58-66, 2006.

参考来源:

https://cran.r-project.org/web/packages/brainR/brainR.pdf

https://gitlab.com/luke-tierney/misc3d

https://github.com/dmurdoch/rgl

brainR程序包的下载链接:

https://CRAN.R-project.org/package=brainR

vioplot:小提琴图

vioplot是箱图和核密度图的组合,该包可以自定义设置,可以画出非常漂亮的小提琴图。

图片

参考来源:

https://github.com/TomKellyGenetics/vioplot

https://rawgit.com/TomKellyGenetics/vioplot/vignettes/vignettes/violin_formulae.html

vioplot程序包的下载链接:

https://CRAN.R-project.org/package=vioplot

  

WVPlots:绘制常见数据的可视化图

WVPlots包是在‘ggplot2’和‘plotly’上实现标准化调用接口下的数据分析图。感兴趣的图包括:‘ROC曲线’、增益曲线、带有边际分布的散点图、带有边际密度的条件散点图、带有匹配理论分布的箱图和茎叶图、以及匹配理论分布的密度图。

图片

参考来源:

https://github.com/WinVector/WVPlots

https://cran.r-project.org/web/packages/WVPlots/WVPlots.pdf

WVPlots程序包的下载链接:

https://CRAN.R-project.org/package=WVPlots

treemapify:在ggplot2中绘制树状图

在treemap中,每个拼图代表一个单独的观察部分,拼图的面积与变量成比例。以“G20峰会”为例,每个拼图代表一个国家。拼图面积反应了该国的GDP,而拼图的填充颜色反应了HDI(人类发展指数)。还可以将其制作成随年限变化的动图,具体请参见参考来源附上的网址。

图片

参考来源:

https://wilkox.org/treemapify/

https://cran.r-project.org/web/packages/treemapify/treemapify.pdf

treemapify程序包下载链接:

https://CRAN.R-project.org/package=treemapify

gplots:绘制可视化数据的各种R编程工具

gplots包含绘制数据的各种R编程工具,如计算和绘制局部平滑的汇总函数(‘bandplot’,‘wapply’),标准绘制的增强版本(‘barplot2’,‘boxplot2’,‘heatmap.2’,‘smartlegend’),颜色设置(‘col2hex’,‘colorpanel’,‘redgreen’,‘greenred’,‘bluered’,‘redblue’,‘rich.colors’),计算和绘制二维数据汇总(‘ci2d’,‘hist2d’), 回归诊断图(‘lmplot2’,‘residplot’)等。

图片

图片来自于文章Robust locally weighted regression and smoothing scatterplots,<加权回归和平滑散点图>。

参考来源:

https://cran.r-project.org/web/packages/gplots/gplots.pdf

https://github.com/talgalili/gplots

gplots程序包下载链接:

https://CRAN.R-project.org/package=gplots

plotly:基于web的ggplot2交互地图

通过‘plot .js’创建交互式Web图形。Plotly的R图形库可以制作交互式的、具有出版质量的图形。图库中有关于制作条形图、散点图、面积图、误差条形图、直方图、热图、子图、多轴和3D(基于WebGL)的图形示例。如下:

图片

参考来源:

https://plotly-r.com

https://github.com/plotly/plotly.R

https://plotly.com/r/

plotly程序包下载链接:

https://CRAN.R-project.org/package=plotly

spacesRGB:标准和用户自定义的RGB颜色工具

标准RGB包括sRGB,‘Adobe’RGB,‘ProPhoto’RGB,BT.709等。用户自定义的RGB空间也是可以的。部分支持ACES颜色工作流。

图片

参考来源:

https://cran.r-project.org/web/packages/spacesRGB/spacesRGB.pdf

https://cran.r-project.org/web/packages/spacesRGB/vignettes/spacesRGB-guide.html

spacesRGB程序包下载链接:

https://CRAN.R-project.org/package=spacesRGB

r2d3:D3绘制可视化图形的工具

利用D3进行数据的动态和交互式的可视化。使用r2d3,需要编写一个D3脚本,然后使用r2d3()函数传递R数据。例如,利用下面的一个简单的D3脚本绘制如下图所示的条形图(“barchart.js”)。

图片

// !preview r2d3 data=c(0.3, 0.6, 0.8, 0.95, 0.40, 0.20)
var barHeight = Math.floor(height / data.length);
svg  .selectAll("rect")  .data(data)  .enter()  .append("rect")  .attr("width", function (d) {    return d * width;  })  .attr("height", barHeight)  .attr("y", function (d, i) {    return i * barHeight;  })  .attr("fill", "steelblue");

参考来源:

https://github.com/rstudio/r2d3

https://cran.r-project.org/web/packages/r2d3/r2d3.pdf

r2d3程序包下载链接:

https://CRAN.R-project.org/package=r2d3

ezplot:常见图表绘制函数集

使用ezplot可以快速创建演示图表,这对于探索性数据分析也十分有用。一般情况下,ezplot函数为x、group、facet_y和facet_x的重复类别聚合多个y值。

图片

参考来源:

https://cran.r-project.org/web/packages/ezplot/ezplot.pdf

https://cran.r-project.org/web/packages/ezplot/vignettes/overview.html

ezplot程序包的下载链接:

https://CRAN.R-project.org/package=ezplot

iheatmapr:可绘制复杂的、交互式的热图

iheatmapr是一个用于绘制复杂且交互式的热图R包,它可以实现沿主热图的行或列的子图添加关于每一行或每一列的更多信息。虽然已经有很多很棒的R包来制作热图,包括相对简单的交互式热图(heatmaply和d3heatmap)或复杂的静态热图(ComplexHeatmap),但iheatmapr能够让复杂的交互式热图的绘制变得简单。

图片

library(iheatmapr)data(measles, package = "iheatmapr")
main_heatmap(measles, name = "Measles<br>Cases", x_categorical = FALSE,             layout = list(font = list(size = 8))) %>%  add_col_groups(ifelse(1930:2001 < 1961,"No","Yes"),                  side = "bottom", name = "Vaccine<br>Introduced?",                  title = "Vaccine?",                  colors = c("lightgray","blue")) %>%  add_col_labels(ticktext = seq(1930,2000,10),font = list(size = 8)) %>%  add_row_labels(size = 0.3,font = list(size = 6)) %>%  add_col_summary(layout = list(title = "Average<br>across<br>states"),                  yname = "summary")  %>%                   add_col_title("Measles Cases from 1930 to 2001", side= "top") %>%  add_row_summary(groups = TRUE,                  type = "bar",                  layout = list(title = "Average<br>per<br>year",                                font = list(size = 8)))

参考来源:

https://github.com/ropensci/iheatmapr

https://cran.r-project.org/web/packages/iheatmapr/iheatmapr.pdf

iheatmapr程序包下载链接:

https://CRAN.R-project.org/package=iheatmapr

ggplotgui:通过GUI创建ggplot

ggplotgui可通过ggplot_shiny()函数来创建和优化ggplot2图形,并能够产生要求的R代码。

图片

参考来源:

https://github.com/gertstulp/ggplotgui/

https://cran.r-project.org/web/packages/ggplotgui/ggplotgui.pdf

ggplotgui程序包的下载链接:

https://CRAN.R-project.org/package=ggplotgui

更多资讯看下图

 

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
About this book Gives a comprehensive overview of the Lattice graphics system and shows how to use it effectively Includes numerous examples using data sets from various R packages All code and figures including color version also available online R is rapidly growing in popularity as the environment of choice for data analysis and graphics both in academia and industry. Lattice brings the proven design of Trellis graphics (originally developed for S by William S. Cleveland and colleagues at Bell Labs) to R, considerably expanding its capabilities in the process. Lattice is a powerful and elegant high level data visualization system that is sufficient for most everyday graphics needs, yet flexible enough to be easily extended to handle demands of cutting edge research. Written by the author of the lattice system, this book describes it in considerable depth, beginning with the essentials and systematically delving into specific low levels details as necessary. No prior experience with lattice is required to read the book, although basic familiarity with R is assumed. The book contains close to150 figures produced with lattice. Many of the examples emphasize principles of good graphical design; almost all use real data sets that are publicly available in various R packages. All code and figures in the book are also available online, along with supplementary material covering more advanced topics. Deepayan Sarkar won the 2004 John M. Chambers Statistical Software Award for writing lattice while he was a graduate student in Statistics at the University of Wisconsin-Madison. He is currently doing postdoctoral research in the Computational Biology program at the Fred Hutchinson Cancer Research Center, a member of the R Core Team, and an active participant on the R mailing lists. Written for: Researchers, grad students
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值