此刻进修R Graph Cookbook,关于这本书的歌咏,我已经在前面三篇文章中先容了,这里就不再歌咏了。下面用到的照旧书中的代码,我只是领略了之后发到博客上来,许多领略不透的我就没有写了。
想不到昨晚写着写着就累,到此刻才从头开始写。好吧,继承尽力吧!纵然我此刻失败了3年,我尚有5个月的时间可以挽救呢。
此刻是第四章了,第一个例子是都市降雨图,和时间序列有那么一点儿干系吧。照旧直接上代码吧,代码内里的参数我临时还没有完全搞大白,不外看一下辅佐文档应该就可以或许大白了。
|
|
这样,降雨量,都市名,降雨月份就可以或许很清晰得揭示出来了,照旧很不错的。不外貌似上面写了plot又写了许多line的方法很费时艰辛,作者说mtplot这个函数会相当的利便,临时没有时间试,今后再说。
下面说另一个画图方法,好比我们画一个趋势线,需要表白这个趋势线属于谁的,除了上面的画法,还可以有下面的方法,见代码和图:
png(“a.png”)
gdp <– read.table(“gdp_long.txt”,header=T)
library(RColorBrewer)
pal <– brewer.pal(5,“Set1”) #将调色板配置为Set1的五种颜色
par(mar = par()$mar+c(0,0,0,2),bty = “l”) #这里临时不太大白,是给右边多加一点空间
plot(Canada~Year,data=gdp,type = “l”,lwd = 2, lty = 1, ylim = c(30,60),
col = pal[1],main = “Percentage change in GDP”,ylab = ” “) #绘制加拿大的GDP,颜色取pal[1]
mtext(side=4,at = gdp$Canada[length(gdp$Canada)],text = “Canada”,
col = pal[1],line= 0.3,las = 2) #side=4配置Canada在右边,at参数是配置Canada在Y轴上的位置,
<h2>at后头能每条线的最后一个值,这样Canada就能和线对上了</h2>
lines (gdp$France~gdp$Year,col = pal[2],lwd = 2)
mtext(side = 4, at = gdp$France[length(gdp$France)],text = “France”,
col = pal[2],line = 0.3,las = 2)
lines(gdp$Germany~gdp$Year,col = pal[3],lwd = 2)
mtext(side=4,at = gdp$Germany[length(gdp$Germany)],text = “Germany”,
col = pal[3],line = 0.3,las = 2)
lines(gdp$Britain~gdp$Year,col = pal[4],lwd = 2)
mtext(side = 4,at = gdp$Britain[length(gdp$Britain)],text = “Britain”,
col = pal[4],line = 0.3,las = 2)
lines(gdp$USA~gdp$Year,col = pal[5],lwd = 2)
mtext(side=4,at = gdp$USA[length(gdp$USA)]–2, #为了防备和Canada重叠,减去2
text= “USA”,col = pal[5],line = 0.3,las = 2)
dev.off()
绘制出来的图,直接在末端标明白所属的国度,这样绘制出来的图会越发的清楚,不外需要在数据不多且重叠不严重的环境下。
像上面这种方法,主要是用来比拟和查察趋势的,假如有许多刻度线构成的网格,就能很容易找到线上的每一个点对应的值了(用python的matplotlib绘制出来的图,鼠标放上去能立即显示坐标)下面是带网格的画法:
|
|
只是简朴得用了一个grid()函数,就能实现这样的结果了,照旧挺不错的。grid函数有许多参数可以配置,下面是一个简朴的例子:
|
|
通过一些参数的配置,就能做出本身想要的功效了。
下面要讲的是abline函数。有时候我们仅仅是想画一条可能几条线,在某个指定的位置上,用abline就可以很好得办理了。代码如下:
|
|
这样就在指定的点上绘制出来一条线了。参数V暗示垂直偏向上,在第九个位置绘制一条线。在abline下面加第二句:abline(h=150,col=”red”,lty=2)就能获得如下的结果了:
在程度偏向上就多出来一条红线了。
下面要说的是Sparklines,我也不知道这个词该怎么翻译,不外先容说这种图可以在较小的区域内总结数据的趋势,照旧直接代码和图吧,有用的时候会找到这个看看的:
png(“a.png”)
rain <– read.csv(“cityrain.csv”)
par(mfrow=c(4,1),mar = c(5,7,4,2),omi=c(0.2,2,0.2,2))
for(i in 2:5)
{
plot(rain[,i],ann = FALSE,axes = FALSE,type=“l”,
col = “gray”,lwd = 2)
<pre><code>mtext(side=2,at = mean(rain[,i]),names(rain[i]),
las = 2, col = “black”)
mtext(side = 4,at = mean(rain[,i]),mean(rain[i]),
las = 2,col = “black”)
points(which.min(rain[,i]),min(rain[,i]),pch = 19,col = “blue”)
points(which.max(rain[,i]),max(rain[,i]),pch = 19,col = “red”)
</code>
</pre>
<p>}</p>
<p>dev.off()</p>
<p><img alt=“” src=”http://www.shahuwang.com/wp–content/uploads/2012/04/a8.png” width=”480” height=”480“><br>
mfrow配置画板为4行1列,mar和omi仿佛是将图像限制在较小的范畴内,用which画出指定的点,今朝我也就是可以或许领略到这一点上了。</p>
<p>上面讲的都是对原始数据举办画图,而没有对数据举办数学上的处理惩罚,如绘制a与b的差值曲线图等。下面我们来绘制一条函数的图形:</p>
<pre class=“brush:[python]”>png(“a.png”)
x <– 1:100
y <– x^3–6*x^2+5*x+10
plot(y~x,type=“l”,main=expression(f(x)==x^3–6*x^2+5*x+10))
dev.off()
</pre>
<p><img alt=“” src=”http://www.shahuwang.com/wp–content/uploads/2012/04/a9.png” width=”480” height=”480“></p>
<p>expression函数可以将函数转化为我们正常看到的那种形式。</p>
<p>下面报告一个重磅的对象,用R绘制股票走势图。首先要凭据2个包,</p>
<p>install.packages(“quantmod”)</p>
<p>install.packages(“tseries”)</p>
<p>代码如下,可是这个需要联网,貌似是从yahoo上面下载下来的,所以有点儿慢:</p>
<pre class=“brush:[python]”>png(“a.png”)
library(quantmod)
library(tseries)
aapl <– get.hist.quote(instrument = “appl”,quote = c(“Cl”,“Vol”))
goog <– get.hist.quote(instrument = “goog”,quote = c(“Cl”,“Vol”))
msft <– get.hist.quote(instrument = “msft”,quote = c(“Cl”,“Vol”))
plot(msft$Close,main=“Stock Price Comparison”,
ylim = c(0,800),col = “red”,type = “l”,lwd = 0.5,
pch = 19,cex = 0.6,xlab = “Date”,ylab = “Stock Price (USD)”)
lines(goog$Close,col = “blue”,lwd = 0.5)
lines(aapl$Close,col = “orange”,lwd = 0.5)
legend(“top”,horiz=T,legend=c(“Microsoft”,“Google”,“Apple”),
col = c(“red”,“blue”,“orange”),lty = 1,bty = “n”)
dev.off()
</pre>
<p> </p>
<p><img alt=“” src=”http://www.shahuwang.com/wp–content/uploads/2012/04/a11.png” width=”480” height=”480“></p>
<div class=“wumii-hook”>
<input name=“wurl” value=“http://www.shahuwang.com/?p=628” type=“hidden”><br>
<input name=“wtitle” value=“R语言画图4” type=“hidden”><br>
<input name=“wpic” value=“” type=”hidden”>
</div>
<div style=“clear: both; margin-top: 5px; margin-bottom: 5px;”></div><div style=“float: left;”><!–– JiaThis Button BEGIN ––>
<div id=“ckepop”>
<span class=“jiathis_txt”>分享到:</span>
<a class=“jiathis_button_qzone”>QQ空间</a>
<a class=“jiathis_button_tsina”>新浪微博</a>
<a class=“jiathis_button_tqq”>腾讯微博</a>
<a class=“jiathis_button_renren”>人人网</a>
<a class=“jiathis jiathis_txt jiathis_separator jtico jtico_jiathis” href=“http://www.jiathis.com/share?uid=1546939” target=“_blank”>更多</a>
<a class=“jiathis_counter_style”></a>
</div>
<script type=“text/Javascript”>var jiathis_config = {data_track_clickback:true};</script>
<script type=“text/javascript” charset=“utf-8” src=“http://v2.jiathis.com/code/jia.js?uid=1546939”></script><script type=“text/javascript” charset=“utf-8” src=“http://v2.jiathis.com/code/plugin.client.js”></script><div style=“width: 0px; height: 0px; position: absolute;”><object id=“JIATHISSWF” codeBase=“http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab” classid=“clsid:D27CDB6E-AE6D-11cf-96B8-444553540000” width=“0” height=“0”><PARAM NAME=“_cx” VALUE=“26”><PARAM NAME=“_cy” VALUE=“26”><PARAM NAME=“FlashVars” VALUE=“z=a”><PARAM NAME=“Movie” VALUE=“http://www.jiathis.com/code/swf/m.swf”><PARAM NAME=“Src” VALUE=“http://www.jiathis.com/code/swf/m.swf”><PARAM NAME=“WMode” VALUE=“Window”><PARAM NAME=“Play” VALUE=“0”><PARAM NAME=“Loop” VALUE=“-1”><PARAM NAME=“Quality” VALUE=“High”><PARAM NAME=“SAlign” VALUE=““><PARAM NAME=”Menu” VALUE=”–1“><PARAM NAME=”Base” VALUE=”“><PARAM NAME=”AllowScriptAccess” VALUE=”always“><PARAM NAME=”Scale” VALUE=”ShowAll“><PARAM NAME=”DeviceFont” VALUE=”0“><PARAM NAME=”EmbedMovie” VALUE=”0“><PARAM NAME=”BGColor” VALUE=”“><PARAM NAME=”SWRemote” VALUE=”“><PARAM NAME=”MovieData” VALUE=”“><PARAM NAME=”SeamlessTabbing” VALUE=”1“><PARAM NAME=”Profile” VALUE=”0“><PARAM NAME=”ProfileAddress” VALUE=”“><PARAM NAME=”ProfilePort” VALUE=”0“><PARAM NAME=”AllowNetworking” VALUE=”all“><PARAM NAME=”AllowFullScreen” VALUE=”false“><param name=”allowScriptAccess” value=”always“><param name=”swLiveConnect” value=”true“><param name=”movie” value=”http://www.jiathis.com/code/swf/m.swf“><param name=”FlashVars” value=”z=a“><embed height=”0” name=”JIATHISSWF” type=”application/x–shockwave–flash” pluginspage=”http://www.macromedia.com/go/getflashplayer” width=”0” src=”http://www.jiathis.com/code/swf/m.swf” swLiveConnect=”true” allowscriptaccess=”always” FlashVars=”z=a”></object></div>
<!–– JiaThis Button END ––></div><div style=“clear: both; margin-top: 5px; margin-bottom: 5px;”></div><p>Related posts:</p><ol>
<li><a title关键字:
