<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>R | Dr. Hui Lin</title><link>https://www.huilin.site/category/r/</link><atom:link href="https://www.huilin.site/category/r/index.xml" rel="self" type="application/rss+xml"/><description>R</description><generator>Wowchemy (https://wowchemy.com)</generator><language>en-us</language><image><url>https://www.huilin.site/media/icon_hu849bfb60811b9f998366b9def6f35d6e_33320_512x512_fill_lanczos_center_3.png</url><title>R</title><link>https://www.huilin.site/category/r/</link></image><item><title>Learning ggmap</title><link>https://www.huilin.site/post/2017-03-31-learning-ggmaps-package/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://www.huilin.site/post/2017-03-31-learning-ggmaps-package/</guid><description>&lt;p>This is a document for myself to review ggmap package, and how to get quick start.&lt;/p>
&lt;p>ggmap is powerful map plot package in R. Compared with maps package, plots exported from ggmaps are elegant.&lt;/p>
&lt;p>Two steps to plot a map: plot a map raster, decorate the base map with your own data.&lt;/p>
&lt;h3 id="step-1-download-your-base-map">Step 1: download your base map&lt;/h3>
&lt;p>You need to know the location you will plot. Define location in two ways showing below:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">library(ggmap)
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">Location1 &amp;lt;- &amp;#34;University of Wisconsin, Milwaukee&amp;#34; #Use your address as your defination
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">Location2 &amp;lt;- &amp;#34;c(lon = -95.3632715, lat = 29.7632836)&amp;#34; # Use longitude and latitude
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Use &lt;code>get_map&lt;/code> function to download the raster map in your location.
There are 3 map “sources” to obtain a map raster, and each of these sources has multiple “map types”&lt;/p>
&lt;blockquote>
&lt;p>stamen: “watercolor”, “toner”, &amp;ldquo;terrain&amp;rdquo;&lt;/p>
&lt;/blockquote>
&lt;p>
&lt;figure >
&lt;div class="d-flex justify-content-center">
&lt;div class="w-100" >&lt;img src="https://www.huilin.site/image/stamen.jpg" alt="stamen" loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;blockquote>
&lt;p>googlemap:&lt;/p>
&lt;/blockquote>
&lt;p>
&lt;figure >
&lt;div class="d-flex justify-content-center">
&lt;div class="w-100" >&lt;img alt="googlemap" srcset="
/media/image/googlemap_huc4bda9b1694f7f0bbbdd37e40951441f_70802_d2db8c23fb92364459e52bea05a23e0e.webp 400w,
/media/image/googlemap_huc4bda9b1694f7f0bbbdd37e40951441f_70802_e8e865b4a09150fb49e4a7c952a591fc.webp 760w,
/media/image/googlemap_huc4bda9b1694f7f0bbbdd37e40951441f_70802_1200x1200_fit_q75_h2_lanczos.webp 1200w"
src="https://www.huilin.site/media/image/googlemap_huc4bda9b1694f7f0bbbdd37e40951441f_70802_d2db8c23fb92364459e52bea05a23e0e.webp"
width="760"
height="224"
loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;blockquote>
&lt;p>osm:(sometimes their servers are unavailable)&lt;/p>
&lt;/blockquote>
&lt;p>
&lt;figure >
&lt;div class="d-flex justify-content-center">
&lt;div class="w-100" >&lt;img src="https://www.huilin.site/image/osm.jpg" alt="osm" loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">myMap &amp;lt;- get_map(location=myLocation, source=&amp;#34;stamen&amp;#34;, maptype=“watercolor&amp;#34;, crop=FALSE)
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">ggmap(myMap)
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">##zoom = integer from 3-21
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">##3 = continent, 10=city, 21=building
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">##(openstreetmap limit of 18)
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="step-2-decorate-your-map-with-data">Step 2 decorate your map with data&lt;/h3>
&lt;hr>
&lt;p>In addition, a developing R package need to be concerned: &lt;a href="http://rmaps.github.io/" target="_blank" rel="noopener">rMap&lt;/a>.&lt;/p>
&lt;h3 id="references">References:&lt;/h3>
&lt;p>&lt;a href="https://github.com/dkahle/ggmap" target="_blank" rel="noopener">ggmap github&lt;/a>&lt;/p>
&lt;p>&lt;a href="https://cran.r-project.org/web/packages/ggmap/ggmap.pdf" target="_blank" rel="noopener">ggmap document&lt;/a>&lt;/p>
&lt;p>&lt;a href="https://www.nceas.ucsb.edu/~frazier/RSpatialGuides/ggmap/ggmapCheatsheet.pdf" target="_blank" rel="noopener">ggmap quick start&lt;/a>&lt;/p>
&lt;p>[ggmap Introduction](&lt;a href="https://dl.dropboxusercontent.com/u/24648660/ggmap" target="_blank" rel="noopener">https://dl.dropboxusercontent.com/u/24648660/ggmap&lt;/a> useR 2012.pdf)&lt;/p></description></item><item><title>Learning ggplot2</title><link>https://www.huilin.site/post/2017-03-31-learning-ggplot2/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://www.huilin.site/post/2017-03-31-learning-ggplot2/</guid><description>&lt;p>本课程介绍三种R语言的绘图工具包：&lt;code>plot&lt;/code>,&lt;code>qplot&lt;/code>,&lt;code>ggplot&lt;/code>。三种绘图包的能够和语法均不相同。
&lt;code>plot&lt;/code>命令是R语言自带的绘图命令，绘图效果简单，适宜数据分析时绘图。
&lt;code>qplot&lt;/code>命令是R语言初级绘图语言包，能够提供符合出版物标准的简单绘图。得到的图形大方美观。&lt;/p>
&lt;h3 id="lesson-regression-models-introduction">Lesson: Regression Models Introduction&lt;/h3>
&lt;p>制图：plot(jitter(child,4) ~ parent,galton)&lt;/p>
&lt;p>建立回归函数：使用函数&lt;code>lm&lt;/code>(linear model)，例如：&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">regrline &amp;lt;- lm(child~parent, galton)
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>建立回归函数之后，使用&lt;code>abline&lt;/code>(add straight lines to a plot)函数将回归函数在图表中画出，例如&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">abline(regrline, lwd = 3, col = &amp;#34;red&amp;#34;)
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">#lwd = line width, col = line color
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>画出直线之后可以使用函数&lt;code>summary&lt;/code>查看回归函数的各类参数包括残差， 系数，相关系数等等。&lt;/p>
&lt;hr>
&lt;h3 id="qplot">qplot&lt;/h3>
&lt;p>&lt;code>qplot&lt;/code> is a basic function in ggplot2 package. It provides some basic plots (e.g. points, smooth, boxplot) for users to learn their database generally.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">qplot(hwy, displ, data = mpg)
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>qplot可调的参数有许多，如下展示&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">qplot(x, y, data=, color=, shape=, size=, alpha=, geom=, method=, formula=, facets=, xlim=, ylim= xlab=, ylab=, main=, sub=)
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>x, y, data, color, shape, size这些参数很容易理解。可以使用不同的变量实现变化。&lt;/p>
&lt;p>alpha用于调节透明度。0就是全透明，1就是实心。&lt;/p>
&lt;p>geom用于调节图表类型，有以下几个选项&amp;quot;point&amp;quot;, &amp;ldquo;smooth&amp;rdquo;, &amp;ldquo;boxplot&amp;rdquo;, &amp;ldquo;line&amp;rdquo;, &amp;ldquo;histogram&amp;rdquo;, &amp;ldquo;density&amp;rdquo;, &amp;ldquo;bar&amp;rdquo;, &amp;ldquo;jitter&amp;rdquo;.&lt;/p>
&lt;p>point表示散点图&lt;/p>
&lt;p>smooth做出拟合的曲线图&lt;/p>
&lt;p>&lt;a href="http://docs.ggplot2.org/0.9.3.1/geom_boxplot.html" target="_blank" rel="noopener">boxplot&lt;/a>做出股价图，此处不使用自定义的最高值，最低值和平均值，而是使用fill定义group，自动计算&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">qplot(year,averTLO, data = gt_sum, xlab = &amp;#34;Year&amp;#34;, ylab = &amp;#34;Land&amp;amp;Ocean Avg Temp&amp;#34;, geom = c(&amp;#34;boxplot&amp;#34;,&amp;#34;jitter&amp;#34;),fill=decade)
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>![boxplot](/image/Land&amp;amp;Ocean Avg Temp vs year_boxplot_jitter.jpeg)&lt;/p>
&lt;p>line做出折线图&lt;/p>
&lt;p>&lt;a href="http://docs.ggplot2.org/0.9.3.1/geom_histogram.html" target="_blank" rel="noopener">histogram&lt;/a>只针对单变量的柱状分布图，纵轴为count，横轴为该单变量。&lt;/p>
&lt;p>&lt;a href="http://docs.ggplot2.org/0.9.3.1/geom_density.html" target="_blank" rel="noopener">density&lt;/a>同样只针对单变量，画出该单变量的密度分布图。纵轴为density，横轴为该单变量。&lt;/p>
&lt;p>&lt;a href="http://docs.ggplot2.org/0.9.3.1/geom_bar.html" target="_blank" rel="noopener">bar&lt;/a>即为常规柱状分布图，定义两个变量,利用fill变量可以实现多种变化&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">qplot(factor(cyl), data=mtcars, geom=&amp;#34;bar&amp;#34;, fill=factor(gear))
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>
&lt;figure >
&lt;div class="d-flex justify-content-center">
&lt;div class="w-100" >&lt;img src="http://docs.ggplot2.org/0.9.3.1/geom_bar-18.png" alt="qplot" loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;p>jitter则是在x轴上产生随机变量从而避免图形重叠带来的困扰。例如&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">p &amp;lt;- ggplot(mpg, aes(displ, hwy))
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">p + geom_point()
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">不用jitter散点图的效果
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>
&lt;figure >
&lt;div class="d-flex justify-content-center">
&lt;div class="w-100" >&lt;img src="http://docs.ggplot2.org/0.9.3.1/geom_jitter-2.png" alt="points withou jitter" loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">p + geom_point(position = &amp;#34;jitter&amp;#34;)
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">使用上jitter的效果
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>method和formula这两个选项是针对smooth这个选项而出现的。当smooth选项被调用，默认的拟合方法为loess。还有其他拟合方式允许被调用，如&amp;rsquo;lm&amp;rsquo;:线性拟合，&amp;lsquo;gam&amp;rsquo;:generalized additive models,&amp;ldquo;rlm&amp;rdquo;: robust regression&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">For example, to add simple linear regression lines, you&amp;#39;d specify geom=&amp;#34;smooth&amp;#34;, method=&amp;#34;lm&amp;#34;, formula=y~x. Changing the formula to y~poly(x,2) would produce a quadratic fit. Note that the formula uses the letters x and y, not the names of the variables.
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">For method=&amp;#34;gam&amp;#34;, be sure to load the mgcv package. For method=&amp;#34;rml&amp;#34;, load the MASS package.
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>cited from &lt;a href="http://www.statmethods.net/advgraphs/ggplot2.html" target="_blank" rel="noopener">Quick-R&lt;/a>&lt;/p>
&lt;p>
&lt;figure >
&lt;div class="d-flex justify-content-center">
&lt;div class="w-100" >&lt;img src="http://docs.ggplot2.org/0.9.3.1/geom_jitter-4.png" alt="jitter" loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;p>facets这个选项可以利用变量生成不同的分图，该选项的表达方式为：&lt;code>facets=rowvar~colvar&lt;/code>，若rowvar或colvar不需要设置变量则用“.”代替。例如&lt;code>facets = .~colvar&lt;/code>&lt;/p>
&lt;p>利用coord_flip()可以将图表翻转&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">ggplot(diamonds, aes(color, fill=cut)) + geom_bar() + coord_flip()
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>
&lt;figure >
&lt;div class="d-flex justify-content-center">
&lt;div class="w-100" >&lt;img src="http://docs.ggplot2.org/0.9.3.1/geom_bar-22.png" alt="ggplot_bar" loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;p>需要叠加不同类型的图表是使用c指令，e.g. &lt;code>c(&amp;quot;point&amp;quot;, &amp;quot;smooth&amp;quot;)&lt;/code>&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">qplot(year,averT, data = gt_sum, xlab = &amp;#34;Year&amp;#34;, ylab = &amp;#34;Land Avg Temp&amp;#34;, geom = c(&amp;#34;point&amp;#34;,&amp;#34;smooth&amp;#34;))
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>![combine](/image/land avg temp vs year_point_smooth.jpeg)&lt;/p>
&lt;p>xlim, ylim, xlab, ylab均容易理解
main,sub用于调节主副标题&lt;/p>
&lt;h3 id="ggplot2">ggplot2&lt;/h3>
&lt;p>&lt;code>ggplot&lt;/code>采用图层式绘图方法，可根据自己的意图添加想要的图层，适合绘制复杂的大图。
下面是一个展示&lt;code>ggplot&lt;/code>绘图语法的例子，其中&lt;code>mpg&lt;/code>是&lt;code>ggplot&lt;/code>自带的一个关于汽车品牌，性质的数据库。&lt;code>hwy&lt;/code>和&lt;code>displ&lt;/code>分别是&lt;code>mpg&lt;/code>数据库中的字段，表示每加仑汽油行驶的里程数和汽车的排量。&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">&amp;gt;g&amp;lt;-ggplot(mpg, aes(hwy, displ))
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&amp;gt;g+geom_points()+geom_smooth()
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>How to export a table.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">write.table(dataframe, &amp;#34;pathway&amp;#34;)
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div></description></item></channel></rss>