<div id="js_content">
<p style="text-align: center"><img src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-962a2db5e5080d01eeae22393b07ab47"></p>
<p><strong>可先阅读文章:</strong><strong><a href="http://mp.weixin.qq.com/s?__biz=MzI5ODA1ODYxNA%3D%3D&chksm=f756c835c0214123157dbc92caf0d1b6ffb82a36f660808f122b201843e2eddbff4102ed2176&idx=1&mid=2651455020&scene=21&sn=ad8894cf6fc5bc49bc0c7456d08790f0#wechat_redirect">R绘图笔记 | R语言绘图系统与常见绘图函数及参数</a></strong></p>
<p><strong>1.利用plot()绘制散点图</strong></p>
<p>R语言中plot()函数的基本格式如下:<br></p>
<pre class="blockcode"><code class="language-go">plot(x,y,...)
</code></pre>
<p>plot函数中,x和y分别表示所绘图形的横坐标和纵坐标;函数中的...为附加的参数。plot函数默认的使用格式如下:</p>
<pre class="blockcode"><code class="language-cpp">plot(x, y = NULL, type = "p", xlim = NULL, ylim = NULL, log = "", main = NULL, sub = NULL, xlab = NULL, ylab = NULL, ann = par("ann"), axes = TRUE, frame.plot = axes, panel.first = NULL, panel.last = NULL, asp = NA, ...)
</code></pre>
<p>主要参数的含义如下:</p>
<p>(1)type为一个字符的字符串,用于给定绘图的类型,可选的值如下:</p>
<ul><li><p>"p":绘点(默认值);</p></li><li><p>"l":绘制线;</p></li><li><p>"b":同时绘制点和线;</p></li><li><p>"c":仅绘制参数"b"所示的线;</p></li><li><p>"o":同时绘制点和线,且线穿过点;</p></li><li><p>"h":绘制出点到横坐标轴的垂直线;</p></li><li><p>"s":绘制出阶梯图(先横后纵);</p></li><li><p>"S":绘制出阶梯图(先纵后竖);</p></li><li><p>"n":作空图。</p></li></ul>
<p>(2)main参数 字符串,给出图形的标题;</p>
<p>(3)sub参数 字符串,给出图形的子标题;</p>
<p>(4)xlab 和 ylab参数 字符串,用于给出x轴和y轴的标签。</p>
<p>(5)xlim 和 ylim参数 都是二维向量,分别表示x轴和y轴的取值范围。</p>
<p>(6)pch参数。</p>
<p style="text-align: center"><img src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-ad5b92b069dce62065d8a596b196fb8a"></p>
<p>绘制第一个散点图<br></p>
<pre class="blockcode"><code class="language-nginx">####第一个图
x <- runif(50,0,2)
y <- runif(50,0,2)
plot(x, y, main="我的第一个散点图", sub="subtitle",
xlab="横坐标", ylab="纵坐标", pch=16)
</code></pre>
<p style="text-align: center"><img src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-f9ca7e0593892ced08115cc005d10e3e"></p>
<p style="text-align: left">添加文本和线<br></p>
<pre class="blockcode"><code class="language-javascript">text(0.6,0.6,"(0.6,0.6)")
abline(h=.6,v=.6, col='red')
</code></pre>
<p style="text-align: center"><img src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-c0d004fe595df8b4fd6413a9ce5fd6c1"></p>
<p style="text-align: left">第二个散点图<br></p>
<pre class="blockcode"><code class="language-perl">####第二个图
x <- runif(50,0,2)
y <- runif(50,0,2)
plot(x, y, type="n", xlab="", ylab="", axes=F)
points(x,y) #添加坐标点
axis(1) #添加横轴
axis(at=seq(0,2,0.5), side=2) #添加纵轴
box() #补齐散点图的边框
title(main="散点图", sub="subtitle", xlab="x轴", ylab="y轴")
abline(h=0.6,v=0.6,col="red")
</code></pre>
<p style="text-align: center"><img src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-f5ff09ea7b5d91ee84bdab7bd3090e66"></p>
<p><strong>2.利用ggpolt2绘图</strong><br></p>
<pre class="blockcode"><code class="language-powershell">data(trees) # 加载数据集
head(trees) # 预览数据集
</code></pre>
<p style="text-align: center"><img src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-eea3dd838cabab577e2ea68e00e69a3a"></p>
<p>绘图<br></p>
<pre class="blockcode"><code class="language-go">ggplot(trees, aes(x=Girth,y=Height)) +
geom_point()
</code></pre>
<p style="text-align: center"><img src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-89f1f6c7569431edfad1a0decc93a8a4"></p>
<pre class="blockcode"><code class="language-go">ggplot(trees, aes(x=Girth,y=Height)) +
geom_point(alpha=0.5)
</code></pre>
<p style="text-align: center"><img src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-5b612a82c13400372f7ad7a0a967198e"></p>
<pre class="blockcode"><code class="language-go">ggplot(trees, aes(x=Girth,y=Height)) +
stat_bin2d()
</code></pre>
<p style="text-align: center"><img src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-80956152d37371460054abc7273606a7"></p>
<pre class="blockcode"><code class="language-swift">ggplot |
|