绘制美观的小提琴图_公式方式进行数据分组和排序问题_2020-02-13

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

setwd("./vioplot/")

## 2.导入vioplot包

library(vioplot)

## 3.测试

### 3.1 数据分组:公式方法的应用

#@ 导入R内置数据集

data(iris)

#@ 查看前10行和后10行内容

head(iris, n = 10)
# 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
# 7           4.6         3.4          1.4         0.3  setosa
# 8           5.0         3.4          1.5         0.2  setosa
# 9           4.4         2.9          1.4         0.2  setosa
# 10          4.9         3.1          1.5         0.1  setosa
tail(iris, n = 10)
# Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
# 141          6.7         3.1          5.6         2.4 virginica
# 142          6.9         3.1          5.1         2.3 virginica
# 143          5.8         2.7          5.1         1.9 virginica
# 144          6.8         3.2          5.9         2.3 virginica
# 145          6.7         3.3          5.7         2.5 virginica
# 146          6.7         3.0          5.2         2.3 virginica
# 147          6.3         2.5          5.0         1.9 virginica
# 148          6.5         3.0          5.2         2.0 virginica
# 149          6.2         3.4          5.4         2.3 virginica
# 150          5.9         3.0          5.1         1.8 virginica

#@ 绘图(默认参数)

# boxplot #
boxplot(Sepal.Length~Species, data = iris)

# vioplot #
vioplot(Sepal.Length~Species, data = iris)

### 3.2 其他R内置数据集测试


#@ 获取R内置数据集中所有的数据包名称

ls(package:datasets)
# [1] "ability.cov"           "airmiles"             
# [3] "AirPassengers"         "airquality"           
# [5] "anscombe"              "attenu"               
# [7] "attitude"              "austres"              
# [9] "beaver1"               "beaver2"              
# [11] "BJsales"               "BJsales.lead"         
# [13] "BOD"                   "cars"                 
# [15] "ChickWeight"           "chickwts"             
# [17] "co2"                   "CO2"                  
# [19] "crimtab"               "discoveries"          
# [21] "DNase"                 "esoph"                
# [23] "euro"                  "euro.cross"           
# [25] "eurodist"              "EuStockMarkets"       
# [27] "faithful"              "fdeaths"              
# [29] "Formaldehyde"          "freeny"               
# [31] "freeny.x"              "freeny.y"             
# [33] "HairEyeColor"          "Harman23.cor"         
# [35] "Harman74.cor"          "Indometh"             
# [37] "infert"                "InsectSprays"         
# [39] "iris"                  "iris3"                
# [41] "islands"               "JohnsonJohnson"       
# [43] "LakeHuron"             "ldeaths"              
# [45] "lh"                    "LifeCycleSavings"     
# [47] "Loblolly"              "longley"              
# [49] "lynx"                  "mdeaths"              
# [51] "morley"                "mtcars"               
# [53] "nhtemp"                "Nile"                 
# [55] "nottem"                "npk"                  
# [57] "occupationalStatus"    "Orange"               
# [59] "OrchardSprays"         "PlantGrowth"          
# [61] "precip"                "presidents"           
# [63] "pressure"              "Puromycin"            
# [65] "quakes"                "randu"                
# [67] "rivers"                "rock"                 
# [69] "Seatbelts"             "sleep"                
# [71] "stack.loss"            "stack.x"              
# [73] "stackloss"             "state.abb"            
# [75] "state.area"            "state.center"         
# [77] "state.division"        "state.name"           
# [79] "state.region"          "state.x77"            
# [81] "sunspot.month"         "sunspot.year"         
# [83] "sunspots"              "swiss"                
# [85] "Theoph"                "Titanic"              
# [87] "ToothGrowth"           "treering"             
# [89] "trees"                 "UCBAdmissions"        
# [91] "UKDriverDeaths"        "UKgas"                
# [93] "USAccDeaths"           "USArrests"            
# [95] "UScitiesD"             "USJudgeRatings"       
# [97] "USPersonalExpenditure" "uspop"                
# [99] "VADeaths"              "volcano"              
# [101] "warpbreaks"            "women"                
# [103] "WorldPhones"           "WWWusage"             
data(DNase) # 载入数据
head(DNase) # 查看前6行内容
# Run       conc density
# 1   1 0.04882812   0.017
# 2   1 0.04882812   0.018
# 3   1 0.19531250   0.121
# 4   1 0.19531250   0.124
# 5   1 0.39062500   0.206
# 6   1 0.39062500   0.215

tail(DNase) # 查看后6行
# Run   conc density
# 171  11  3.125   0.994
# 172  11  3.125   0.980
# 173  11  6.250   1.421
# 174  11  6.250   1.385
# 175  11 12.500   1.715
# 176  11 12.500   1.721
str(DNase) # 查看DNase的数据结构
# Classes ‘nfnGroupedData’, ‘nfGroupedData’, ‘groupedData’ and 'data.frame':    176 obs. of  3 variables:
#   $ Run    : Ord.factor w/ 11 levels "10"<"11"<"9"<..: 4 4 4 4 4 4 4 4 4 4 ...
# $ conc   : num  0.0488 0.0488 0.1953 0.1953 0.3906 ...
# $ density: num  0.017 0.018 0.121 0.124 0.206 0.215 0.377 0.374 0.614 0.609 ...
# - attr(*, "formula")=Class 'formula'  language density ~ conc | Run
# .. ..- attr(*, ".Environment")=<environment: R_EmptyEnv> 
#   - attr(*, "labels")=List of 2
# ..$ x: chr "DNase concentration"
# ..$ y: chr "Optical density"
# - attr(*, "units")=List of 1
# ..$ x: chr "(ng/ml)"

View(DNase) # 用Rstudio查看数据

#@ 绘制小提琴图(默认参数绘制)

# density~Run
vioplot(density~Run, data = DNase)

# conc~Run
vioplot(conc~Run, data = DNase)

#@ 给些颜色(统一颜色),让图更吸引人(默认排序)

# density~Run
vioplot(density~Run, data = DNase, col = "tomato")

# conc~Run
vioplot(conc~Run, data = DNase, col = "deepskyblue")

#@ 给些颜色(不同颜色),让图更吸引人(默认排序)

# density~Run
vioplot(density~Run, data = DNase, col = c("aquamarine","tomato","tan","lawngreen","cornflowerblue","hotpink","firebrick","darkviolet","orangered", "orchid","darkmagenta" ))

# conc~Run
vioplot(conc~Run, data = DNase, col = c("aquamarine","tomato","tan","lawngreen","cornflowerblue","hotpink","firebrick","darkviolet","orangered", "orchid","darkmagenta" ))

#@ 设置分组组名排列顺序(自定义排列顺序:解决数字排序的问题)

Run <- factor(DNase$Run, levels = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"))
# density~Run
vioplot(DNase$density[DNase$Run == "1"],
        DNase$density[DNase$Run == "2"],
        DNase$density[DNase$Run == "3"],
        DNase$density[DNase$Run == "4"],
        DNase$density[DNase$Run == "5"],
        DNase$density[DNase$Run == "6"],
        DNase$density[DNase$Run == "7"],
        DNase$density[DNase$Run == "8"],
        DNase$density[DNase$Run == "9"],
        DNase$density[DNase$Run == "10"],
        DNase$density[DNase$Run == "11"],
        data = DNase, col = c("aquamarine","tomato","tan","lawngreen","cornflowerblue","hotpink","firebrick","darkviolet","orangered", "orchid","darkmagenta" ))

# conc~Run
vioplot(DNase$conc[DNase$Run == "1"], 
        DNase$conc[DNase$Run == "2"],
        DNase$conc[DNase$Run == "3"],
        DNase$conc[DNase$Run == "4"],
        DNase$conc[DNase$Run == "5"],
        DNase$conc[DNase$Run == "6"],
        DNase$conc[DNase$Run == "7"],
        DNase$conc[DNase$Run == "8"],
        DNase$conc[DNase$Run == "9"],
        DNase$conc[DNase$Run == "10"],
        DNase$conc[DNase$Run == "11"],
        data = DNase, col = c("aquamarine","tomato","tan","lawngreen","cornflowerblue","hotpink","firebrick","darkviolet","orangered", "orchid","darkmagenta" ))

## 4.结束

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
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值