——书名:《A.4——视觉化》作者:马丁·摩根 日期:“2016年5月16 - 17日”输出:BiocStyle::html_document: toc: true toc_depth: 2 vignette: > % \VignetteIndexEntry{A。4——Visualization} % \VignetteEngine{knitr::rmarkdown}——' ' ' {r style, echo = FALSE, results = 'asis'}选项(width=100)采用“KNITR_EVAL”,“真正的”)),缓存= as.logical (Sys。采用“KNITR_CACHE”,“真正的”)))``` ```{r echo=FALSE} suppressPackageStartupMessages({library(RColorBrewer) library(ggplot2)})颜色应反映数据的性质,并经过仔细选择,以向所有浏览者传达同等的信息。[RColorBrewer][]包提供了一种简单的选择颜色的方法;参见[colorbrewer2][]网站。' ' ' {r colorbrewer} library(RColorBrewer) display.brewer.all()我们将使用来自“定性”系列的配色方案,以代表不同层次的因素和颜色的选择。我们会得到前四种颜色。```{r color-choice}调色板<- brewer。Dark2“朋友(4) ``` [RColorBrewer]: https://cran.r-project.org/package=RColorBrewer [colorbrewer2]: http://colorbrewer2.org # 'Base' Graphics We'll illustrate 'base' graphics using the built-in `mtcars` data set ```{r mtcars} data(mtcars) # load the data set head(mtcars) ``` The basic model is to plot data, e.g., the relationshiop between miles per gallon and horsepower. ```{r plot-mtcars} plot(mpg ~ hp, mtcars) ``` The appearance can be influenced by arguments, see `?plot` then `?plot.default` and `par`. ```{r plot-mtcars-2} plot(mpg ~ hp, mtcars, pch=20, cex=2, col=palette[1]) ``` More complicated plots can be composed via a series of commands, e.g., to plot a linear regression, make the plot, and add the regression line using `abline()`. ```{r plot-mtcars-regression} plot(mpg ~ hp, mtcars) fit <- lm(mpg ~ hp, mtcars) abline(fit, col=palette[1], lwd=3) ``` # _ggplot2_ Graphics Start by loading the _ggplot2_ library ```{r ggplot2} library(ggplot2) ``` ## Basics Tell _ggplot2_ what to plot using `ggplot()` and `aes()`; we'll use the columns `hp` (horsepower) and `mpg` (miles per gallon). ```{r ggplot, eval=FALSE} ggplot(mtcars, aes(x=hp, y=mpg)) ``` Note the neutral gray background with white gridlines to provide unobtrusive orientation. Note the relatively small size of the axis and tick labels, to avoid distracting from the pattern provided by the data. _ggplot2_ uses different `geom_*` to add to the basic plot. Add points ```{r ggplot-point} ggplot(mtcars, aes(x=hp, y=mpg)) + geom_point() ``` Add a linear regression line and standard error... ```{r ggplot-lm, warning=FALSE} ggplot(mtcars, aes(x=hp, y=mpg)) + geom_point() + geom_smooth(method=lm, col=palette[1]) ``` ...and a locally smoothed regression ```{r ggplot-smooth, warning=FALSE} ggplot(mtcars, aes(x=hp, y=mpg)) + geom_point() + geom_smooth(method=lm, col=palette[1]) + geom_smooth(col=palette[2]) ``` ## Density plots To illustrate additional features, load the BRFSS data subset ```{r file.choose, eval=FALSE} path <- file.choose() ``` ```{r system.file, echo=FALSE} path <- system.file(package="BiocIntroRPCI", "extdata", "BRFSS-subset.csv") ``` ```{r brfss-input} brfss <- read.csv(path) ``` Plot the distribution of weights using `geom_density()` ```{r brfss-density, warning=FALSE} ggplot(brfss, aes(x=Weight)) + geom_density() ``` Plot the weights separately for each year, using `fill=factor(Year)` and `alpha=.5` arguments in the `aes()` argument ```{r brfss-density-by-year, warning=FALSE} ggplot(brfss, aes(x=Weight, fill=factor(Year))) + geom_density(alpha=0.5) ``` Americans are getting heavier, and the variation in weights is increasing. ## Facets Create separate panels for each sex using `facet_grid()`, with a formula describing the factor(s) to use for rows (left-hand side of the formula) and columns (right-hand side). ```{r brfss-facet, warning=FALSE} ggplot(brfss, aes(x=Weight, fill=factor(Year))) + geom_density() + facet_grid(Sex ~ .) ```