定制小提琴图(Customising Violin Plots)_2020-02-10_M

定制小提琴图(Customising Violin Plots)

# vioplot包测试_20200210Monday

 

## 1.设置当前工作目录

setwd("./vioplot/")

 

## 2.安装和导入R包:vioplot

# install.packages(vioplot)

library(vioplot)

# 载入需要的程辑包:sm

# Package 'sm', version 2.2-5.6: type help(sm) for summary information

# 载入需要的程辑包:zoo

#

# 载入程辑包:‘zoo’

#

# The following objects are masked from ‘package:base’:

#   

#   as.Date, as.Date.numeric

 

## 3.R包:vioplot简介

 

### 3.1 Description

# Package: vioplot

# Title: Violin Plot

# Version: 0.3.4

# Date: 2019-11-30

# Authors@R: c(person("Daniel", "Adler", email = "dadler@uni-goettingen.de", role = c("aut", "cph")),

#              person("S. Thomas", "Kelly", email = "tom.kelly@riken.jp", role = c("aut", "cre")),

#              person("Tom M.", "Elliott", email = "tom.elliott@auckland.ac.nz", role = c("ctb")))

# Description: A violin plot is a combination of a box plot and a kernel density plot. This package allows extensive customisation of violin plots.

# Depends: sm, zoo

# License: BSD_3_clause + file LICENSE

# URL: https://github.com/TomKellyGenetics/vioplot

# BugReports: https://github.com/TomKellyGenetics/vioplot/issues

# LazyData: true

# RoxygenNote: 7.0.1

# Suggests: ggplot2, RColorBrewer, knitr, rmarkdown, testthat

# Language: en-GB

# VignetteBuilder: knitr

# Encoding: UTF-8

# NeedsCompilation: no

# Packaged: 2019-11-29 14:54:09 UTC; tom

# Author: Daniel Adler [aut, cph],

# S. Thomas Kelly [aut, cre],

# Tom M. Elliott [ctb]

# Maintainer: S. Thomas Kelly <tom.kelly@riken.jp>

#   Repository: CRAN

# Date/Publication: 2019-11-29 18:00:02 UTC

# Built: R 3.6.1; ; 2019-12-03 13:51:08 UTC; windows

 

### 3.2 Main function

ls(package:vioplot) # vioplot包中只有一个函数,说明这个函数中的参数可能有很多

 

# [1] "vioplot"

# Warning message:

  # In ls(package:vioplot) : ‘package:vioplot’被转换成字符串

 

 

## 4.测试:Customising Violin Plots

#@ vioplot包是专门用来绘制小提琴图的R包,我们今天来测试一下它的功能和用法

 

#@ Therefore violin plots are a powerful tool to assist researchers to visualise data, particularly in the quality checking and exploratory parts of an analysis. Violin plots have many benefits:

  

# 1.Greater flexibility for plotting variation than boxplots

# 2.More familiarity to boxplot users than density plots

# 3.Easier to directly compare data types than existing plots

 

#@ 测试数据:鸢尾花数据iris

data("iris") # 载入R内置数据

class(iris) # 查看iris的数据集类型

# [1] "data.frame"

dim(iris) # 获取iris的数据维度信息

# [1] 150   5

head(iris) # 获取iris的前6行

# Sepal.Length Sepal.Width Petal.Length Petal.Width Species

# 1          5.1         3.5          1.4         0.2  setosa

# 2          4.9         3.0          1.4         0.2  setosa

# 3          4.7         3.2          1.3         0.2  setosa

# 4          4.6         3.1          1.5         0.2  setosa

# 5          5.0         3.6          1.4         0.2  setosa

# 6          5.4         3.9          1.7         0.4  setosa

 

str(iris) # 查看iris的数据集结构

# 'data.frame': 150 obs. of  5 variables:

#   $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...

# $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...

# $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...

# $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...

# $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

 

### 4.1 对比箱线图和小提琴图

#### 4.1.1 绘制箱线图

boxplot(iris$Sepal.Length[iris$Species == "setosa"], iris$Sepal.Length[iris$Species == "versicolor"], iris$Sepal.Length[iris$Species == "virginica"], names = c("setosa", "versicolor", "virginica"))

 

#### 4.1.2 绘制小提琴图

vioplot(iris$Sepal.Length[iris$Species=="setosa"], iris$Sepal.Length[iris$Species=="versicolor"], iris$Sepal.Length[iris$Species=="virginica"], names=c("setosa", "versicolor", "virginica"))

 

### 4.2 默认绘图

#@ 然而,正如我们在这里可以看到的,按照默认参数绘制出来的小提琴图是不美观的,一个相当显眼的颜色方案不适合专业或学术使用。因此,图片默认颜色效果如下:

 

vioplot(iris$Sepal.Length[iris$Species=="setosa"], iris$Sepal.Length[iris$Species=="versicolor"], iris$Sepal.Length[iris$Species=="virginica"], names=c("setosa", "versicolor", "virginica"), main = "Sepal Length")

 

### 4.3 图片颜色:小提琴的填充

#@ 图片的颜色可以用参数col进行自定义,举例如下:

vioplot(iris$Sepal.Length[iris$Species=="setosa"], iris$Sepal.Length[iris$Species=="versicolor"], iris$Sepal.Length[iris$Species=="virginica"], names=c("setosa", "versicolor", "virginica"), main = "Sepal Length", col="lightblue") # 我们用lightblue对小提琴进行填充,当然还可以是其他任意的颜色,只要你喜欢即可

 

#@ green vioplot

vioplot(iris$Sepal.Length[iris$Species=="setosa"], iris$Sepal.Length[iris$Species=="versicolor"], iris$Sepal.Length[iris$Species=="virginica"], names=c("setosa", "versicolor", "virginica"), main = "Sepal Length", col="green")

 

#@ blue vioplot

vioplot(iris$Sepal.Length[iris$Species=="setosa"], iris$Sepal.Length[iris$Species=="versicolor"], iris$Sepal.Length[iris$Species=="virginica"], names=c("setosa", "versicolor", "virginica"), main = "Sepal Length", col="blue")

 

#@ brown vioplot

vioplot(iris$Sepal.Length[iris$Species=="setosa"], iris$Sepal.Length[iris$Species=="versicolor"], iris$Sepal.Length[iris$Species=="virginica"], names=c("setosa", "versicolor", "virginica"), main = "Sepal Length", col="brown")

 

### 4.4 参数:向量化传参(Vectorisation)

#@ 上面的代码可以对小提琴图的颜色进行自定义,但还不够完美,如果可以用不同颜色可以去区别每个小提琴图的话那就更佳了,方法如下:

vioplot(iris$Sepal.Length[iris$Species=="setosa"], iris$Sepal.Length[iris$Species=="versicolor"], iris$Sepal.Length[iris$Species=="virginica"], names=c("setosa", "versicolor", "virginica"), main = "Sepal Length", col=c("lightgreen", "lightblue", "palevioletred")) # 同样还是对参数col进行调整,但是我们本次传入的参数是由三种颜色组成的向量,对应三个小提琴,分别对这三个小提琴图进行颜色填充

 

#@ 填加图例说明

legend("topleft", legend=c("setosa", "versicolor", "virginica"), fill=c("lightgreen", "lightblue", "palevioletred"), cex = 1) # 图例的位置、内容和字体大小都是可以灵活定制的,用到的函数是R基础函数:legend()

 

### 4.5 绘图颜色:小提琴线和箱线图

#@ 我们还可以用参数col和border对小提琴图的颜色填充和边界线颜色进行定制,举例如下:

vioplot(iris$Sepal.Length[iris$Species=="setosa"], iris$Sepal.Length[iris$Species=="versicolor"], iris$Sepal.Length[iris$Species=="virginica"], names=c("setosa", "versicolor", "virginica"), main = "Sepal Length", col="lightblue", border="royalblue") # 填充颜色:lightblue;边界颜色:royalblue(宝石蓝)

 

#@ 同样,参数lineCol和rectCol指定了boxplot轮廓和矩形填充的颜色。为了简单起见,盒子和盒图的胡须总是有相同的颜色(二者颜色由一个参数决定:lineCol)。用法举例如下:

vioplot(iris$Sepal.Length[iris$Species=="setosa"], iris$Sepal.Length[iris$Species=="versicolor"], iris$Sepal.Length[iris$Species=="virginica"], names=c("setosa", "versicolor", "virginica"), main = "Sepal Length", rectCol="lightblue", lineCol="violetred") # 设置参数lineCol,小提琴图的boxplot轮廓颜色为:violetred紫罗蓝色;设置参数rectCol,将boxplot填充颜色设置为:lightblue亮蓝色

 

#@ 同样的,通过参数colMed,我们还可以对中点的颜色进行设置。

vioplot(iris$Sepal.Length[iris$Species=="setosa"], iris$Sepal.Length[iris$Species=="versicolor"], iris$Sepal.Length[iris$Species=="virginica"], names=c("setosa", "versicolor", "virginica"), main = "Sepal Length", colMed="red") # 中点颜色可以设置为红色

 

### 4.6 整合vioplot的参数定制小提琴图

#@ 上面我们对vioplot的各个参数分别进行测试,现在我们将这些参数整合在一个vioplot函数中进行绘制,效果如下:

 

vioplot(iris$Sepal.Length[iris$Species=="setosa"], iris$Sepal.Length[iris$Species=="versicolor"], iris$Sepal.Length[iris$Species=="virginica"], names=c("setosa", "versicolor", "virginica"), main = "Sepal Length", col="lightblue", border="black", rectCol="lightblue", lineCol="violetred", colMed="red")

 

### 4.7 自定义小提琴中点的形状和颜色

#@ 不光可以对中点的颜色进行定制,还可以将中点设置为不同的形状,方法举例如下:

vioplot(iris$Sepal.Length[iris$Species=="setosa"], iris$Sepal.Length[iris$Species=="versicolor"], iris$Sepal.Length[iris$Species=="virginica"], names=c("setosa", "versicolor", "virginica"), main="Sepal Length (Equal Area)", areaEqual = T, col=c("lightgreen", "lightblue", "palevioletred"), border=c("darkolivegreen4", "royalblue4", "violetred4"), rectCol=c("forestgreen", "blue", "palevioletred3"), lineCol=c("darkolivegreen", "royalblue", "violetred4"), colMed=c("green", "cyan", "magenta"), pchMed=c(15, 17, 19)) # 参数colMed设置中点的颜色;参数pchMed设置中点的形状

 

### 4.8 优化参数定制美观的小提琴图

#@ 最后我们再加上图例和对每个小提琴进行不同颜色区别的参数,定制美观的小提琴图

vioplot(iris$Sepal.Length[iris$Species=="setosa"], iris$Sepal.Length[iris$Species=="versicolor"], iris$Sepal.Length[iris$Species=="virginica"], names=c("setosa", "versicolor", "virginica"), main="Sepal Length (Equal Area)", areaEqual = T, col=c("lightgreen", "lightblue", "palevioletred"), border=c("darkolivegreen4", "royalblue4", "violetred4"), rectCol=c("forestgreen", "blue", "palevioletred3"), lineCol=c("darkolivegreen", "royalblue", "violetred4"), colMed=c("green", "cyan", "magenta"), pchMed=c(15, 17, 19)) # 绘制小提琴图

legend("topleft", legend=c("setosa", "versicolor", "virginica"), fill=c("lightgreen", "lightblue", "palevioletred"), cex = 1) # 填加图例

 

## 5.结束

sessionInfo()

# R version 3.6.2 (2019-12-12)

# Platform: x86_64-w64-mingw32/x64 (64-bit)

# Running under: Windows 10 x64 (build 18363)

#

# Matrix products: default

#

# locale:

# [1] LC_COLLATE=Chinese (Simplified)_China.936

# [2] LC_CTYPE=Chinese (Simplified)_China.936   

# [3] LC_MONETARY=Chinese (Simplified)_China.936

# [4] LC_NUMERIC=C                              

# [5] LC_TIME=Chinese (Simplified)_China.936    

#

# attached base packages:

# [1] stats     graphics  grDevices utils     datasets  methods  

# [7] base     

#

# other attached packages:

# [1] vioplot_0.3.4 zoo_1.8-7     sm_2.2-5.6   

#

# loaded via a namespace (and not attached):

# [1] compiler_3.6.2  tools_3.6.2     grid_3.6.2     

# [4] packrat_0.5.0   lattice_0.20-38 tcltk_3.6.2

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值