人间漫游家 發表於 2022-6-9 14:12:32

R语言利用ggplot2绘制QQ图和箱线图详解

<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>绘制qq图</li><ul class="second_class_ul"><li>函数介绍</li><li>例子</li></ul><li>绘制boxplot</li><ul class="second_class_ul"><li>函数介绍</li><li>例子</li><li>利用分位点绘制箱线图</li></ul><li>将QQ图和箱线图进行融合</li><ul class="second_class_ul"><li>函数介绍</li><li>参数介绍</li><li>注意事项</li><li>例子</li></ul></ul></div><p class="maodian"></p><h2>绘制qq图</h2>
<p>在ggplot2中绘制qq图需要两步,<code>geom_qq()</code>将绘制样本分位点,<code>geom_qq_line()</code>将绘制标准正态线</p>
<p class="maodian"></p><p class="maodian"></p><p class="maodian"></p><h3>函数介绍</h3>
<p><strong>geom_qq()</strong></p>
<div class="jb51code"><pre class="brush:plain;">geom_qq(
mapping = NULL,
data = NULL,
geom = "point",
position = "identity",
...,
distribution = stats::qnorm,
dparams = list(),
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
</pre></div>
<div class="jb51code"><pre class="brush:plain;">geom_qq_line(
mapping = NULL,
data = NULL,
geom = "path",
position = "identity",
...,
distribution = stats::qnorm,
dparams = list(),
line.p = c(0.25, 0.75),
fullrange = FALSE,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
</pre></div>
<p><strong>参数介绍</strong></p>
<p>**aes()**中的映射参数必须包含sample,可选参数有group,x,y distribution</p>
<blockquote><p>Distribution function to use, if x not specified<br />dparams Additional parameters passed on to distribution function.<br />line.p Vector of quantiles to use when fitting the Q-Q line, defaults defaults to c(.25, .75).<br />fullrange Should the q-q line span the full range of the plot, or just the data</p></blockquote>
<p><strong>注意事项</strong></p>
<p>**aes()**中的映射参数必须包含sample</p>
<p class="maodian"></p><p class="maodian"></p><p class="maodian"></p><h3>例子</h3>
<p>Using to explore the distribution of a variable</p>
<div class="jb51code"><pre class="brush:plain;">ggplot(mtcars, aes(sample = mpg)) +
stat_qq() +
stat_qq_line()
ggplot(mtcars, aes(sample = mpg, colour = factor(cyl))) +
stat_qq() +
stat_qq_line()
</pre></div>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202206/2022060914022929.png" /></p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202206/2022060914022930.png" /></p>
<p class="maodian"></p><h2>绘制boxplot</h2>
<h3>函数介绍</h3>
<div class="jb51code"><pre class="brush:plain;">geom_boxplot(
mapping = NULL,
data = NULL,
stat = "boxplot",
position = "dodge2",
...,
outlier.colour = NULL,
outlier.color = NULL,
outlier.fill = NULL,
outlier.shape = 19,
outlier.size = 1.5,
outlier.stroke = 0.5,
outlier.alpha = NULL,
notch = FALSE,
notchwidth = 0.5,
varwidth = FALSE,
na.rm = FALSE,
orientation = NA,
show.legend = NA,
inherit.aes = TRUE
)
</pre></div>
<p><strong>参数介绍</strong></p>
<p>aes()可接收的参数有:</p>
<ul><li>x or y, 利用x将会是横向箱线图,y的是纵向</li><li>lower or xlower</li><li>upper or xupper</li><li>middle or xmiddle</li><li>ymin or xmin</li><li>ymax or xmax</li><li>alpha</li><li>colour</li><li>fill</li><li>group</li><li>linetype</li><li>shape</li><li>size</li><li>weight</li></ul>
<blockquote><p>notch If FALSE (default) make a standard box plot. If TRUE, make a notched box plot. Notches are used to compare groups; if the notches<br />of two boxes do not overlap, this suggests that the medians are<br />significantly different.<br />notchwidth For a notched box plot, width of the notch relative to the body (defaults to notchwidth = 0.5).<br />varwidth If FALSE (default) make a standard box plot. If TRUE, boxes are drawn with widths proportional to the square-roots of the<br />number of observations in the groups (possibly weighted, using the<br />weight aesthetic).</p></blockquote>
<h3>例子</h3>
<div class="jb51code"><pre class="brush:plain;">p &lt;- ggplot(mpg, aes(x=class, y=hwy))
p + geom_boxplot()
</pre></div>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202206/2022060914022931.png" /></p>
<div class="jb51code"><pre class="brush:plain;">ggplot(mpg, aes(x=hwy, y=class)) + geom_boxplot()
</pre></div>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202206/2022060914022932.png" /></p>
<div class="jb51code"><pre class="brush:plain;">p &lt;- ggplot(mpg, aes(x=class, y=hwy))
p + geom_boxplot(notch = TRUE,varwidth = TRUE,fill = "white", colour = "#3366FF")
</pre></div>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202206/2022060914023033.png" /></p>
<div class="jb51code"><pre class="brush:plain;">ggplot(diamonds, aes(carat, price)) +
geom_boxplot(aes(group = cut_width(carat, 0.25)))
</pre></div>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202206/2022060914023034.png" /></p>
<div class="jb51code"><pre class="brush:plain;">p &lt;- ggplot(mpg, aes(x=class, y=hwy))
p + geom_boxplot(outlier.shape = NA) + geom_jitter(width = 0.2)
</pre></div>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202206/2022060914023035.png" /></p>
<p class="maodian"></p><h3>利用分位点绘制箱线图</h3>
<div class="jb51code"><pre class="brush:plain;">y &lt;- rnorm(100)
df &lt;- data.frame(
x = 1,
y0 = min(y),
y25 = quantile(y, 0.25),
y50 = median(y),
y75 = quantile(y, 0.75),
y100 = max(y)
)
ggplot(df, aes(x)) +
geom_boxplot(
    aes(ymin = y0, lower = y25, middle = y50, upper = y75, ymax = y100),
    stat = "identity"
)
</pre></div>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202206/2022060914023036.png?202259141225" /></p>
<p class="maodian"></p><h2>将QQ图和箱线图进行融合</h2>
<h3>函数介绍</h3>
<p>该函数是来自于<code>qqboxplot</code>包,因此使用前需要安装</p>
<div class="jb51code"><pre class="brush:plain;">geom_qqboxplot(
mapping = NULL,
data = NULL,
stat = "qqboxplot",
position = "dodge2",
...,
outlier.colour = NULL,
outlier.color = NULL,
outlier.fill = NULL,
outlier.shape = 19,
outlier.size = 1.5,
outlier.stroke = 0.5,
outlier.alpha = NULL,
notch = FALSE,
notchwidth = 0.5,
varwidth = FALSE,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
</pre></div>
<p class="maodian"></p><h3>参数介绍</h3>
<p>大部分参数和geom_qq()和geom_boxplot()中的参数含义相同</p>
<p>reference_dist 表示参数比较的标准分布名称,如果有参数需要有dparams</p>
<p>compdata 用于比较的标准样本数据,是个向量</p>
<p class="maodian"></p><h3>注意事项</h3>
<p>aes()函数中的y不可缺</p>
<h3>例子</h3>
<div class="jb51code"><pre class="brush:plain;">library(dplyr)
library(ggplot2)
library(qqboxplot)

simulated_data=tibble(y=c(rnorm(1000, mean=2), rt(1000, 16), rt(500, 4),
                        rt(1000, 8), rt(1000, 32)),
                      group=c(rep("normal, mean=2", 1000),
                              rep("t distribution, df=16", 1000),
                              rep("t distribution, df=4", 500),
                              rep("t distribution, df=8", 1000),
                              rep("t distribution, df=32", 1000)))
p &lt;- ggplot2::ggplot(simulated_data, ggplot2::aes(factor(group,
                                                         levels=c("normal, mean=2", "t distribution, df=32", "t distribution, df=16",
                                                                  "t distribution, df=8", "t distribution, df=4")), y=y))
p + geom_qqboxplot()
p + geom_qqboxplot(reference_dist = "norm")


p + geom_qqboxplot(compdata = comparison_dataset)
</pre></div>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202206/2022060914023137.png" /></p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202206/2022060914023138.png" /></p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202206/2022060914023139.png" /></p>
<p>以上就是R语言利用ggplot2绘制QQ图和箱线图详解的详细内容,更多关于R语言绘制QQ图 箱线图的资料请关注琼殿技术社区其它相关文章!</p>
                           
                            <div class="art_xg">
                              <b>您可能感兴趣的文章:</b><ul><li>R语言中ggplot2绘制双坐标轴图</li><li>R语言ggplot2设置图例(legend)的操作大全</li><li>R语言ggplot2图例标签、标题、顺序修改和删除操作实例</li><li>R语言ggplot2拼图包patchwork安装使用</li><li>R语言可视化ggplot2绘制24小时动态血糖图</li><li>使用ggsignif优雅添加显著性标记详解</li></ul>
                            </div>

                        </div>
                        <!--endmain-->
頁: [1]
查看完整版本: R语言利用ggplot2绘制QQ图和箱线图详解