ggplot2(R)中的Boxplot / Box&Whisker帮助需要删除重复项,同时还要在一个图上进行绘制

时间:2018-07-04 19:48:55

标签: r ggplot2 boxplot na

所以我想创建一个由6种不同的箱形图组成的图(所有图都在一张图中)。但是,我遇到了一些麻烦。 首先,有很多NA,它们随机地分布在数据集中,因此我不确定如何在这个问题上将所有变量绘制在一个图上。 其次,ggplot需要一个y变量。。。我对tidyverse相当陌生,但是不确定如何创建一个y变量来表示每个图。 这是到目前为止我得到的代码以及一些示例数据,只是有些废话...:

library(readr)
library(ggplot2)
library(tidyr)

Box_Whiskers_Plot <- read_delim("C:/Users/johnt/Downloads/Box & Whiskers Plot.txt", 
                                "\t", escape_double = FALSE, trim_ws = TRUE)


box_tidy <- gather(data = Box_Whiskers_Plot, 
                    key = Concern,
                    value = Value) 


ggplot(data = box_tidy, mapping = aes(x = Concern, y = Value)) +
  geom_boxplot()

样本数据:

structure(list(`1 concern` = c(NA, NA, NA, NA, "4.7051072361071977E-2", 
"0.19811079686050914", "0.15241809445883892", "9.3784616216209704E-2", 
NA, NA, "0.12902642667986841", NA, NA, NA, "-2.7995766112836051E-3", 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "0.16257072914439274", 
NA, NA, NA, NA, NA, NA, NA, "-0.32189822523240785", NA, NA, "8.8779492146409344E-2", 
NA, NA, NA, NA, "0.25876167614411516", NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, "0.12140341771652063", NA, 
NA, NA, NA, NA, NA, NA, "7.8315099203373872E-2", NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, "-2.4887790087301243E-2", NA, NA, "0.17817816702345479", 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "-0.45715764794257374", 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "0.1727380710391988", 
NA, NA, NA, NA, NA, "-", NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, "0.11810158539718096", NA, NA, NA, 
"0.27340288238873622", NA, NA, "0.31222498045287939", NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "1 concern", "8.3059150913641758E-2"
), `2 concerns` = c(NA, "0.30750786698302712", NA, NA, NA, NA, 
NA, NA, "0.19491094633578943", "0.14347068243793348", NA, NA, 
NA, NA, NA, "9.4003202704330935E-2", NA, NA, NA, NA, NA, NA, 
"5.8682039323707746E-2", NA, NA, NA, NA, NA, NA, "0.38837474884084", 
NA, NA, "9.9772158663856914E-2", NA, NA, NA, NA, "0.15369966808838376", 
NA, "-9.7591933707396827E-2", "7.5799891559719335E-2", NA, "0.74069094176638783", 
"0.18455079764897997", "0.35878241180217119", NA, NA, "9.7671065222774578E-2", 
NA, "-1", "1.9762661406333537E-2", NA, NA, NA, NA, NA, NA, "0.12110279127050561", 
"-8.8073972864920469E-2", NA, NA, "-5.3063552654085022E-2", "-0.19524178703281547", 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "0.19324044960582598", 
NA, NA, NA, NA, NA, "0.19558769095890249", "9.8480653359761305E-2", 
"-2.7258845509566809E-2", NA, NA, "4.2377241471322602E-2", NA, 
NA, NA, "-0.31089100169922018", NA, NA, "9.4259642624681561E-2", 
NA, NA, NA, NA, NA, NA, NA, NA, "2.9465956237787916E-2", "0.36028868638565514", 
"0.28696166852692623", "0.16026874768911181", NA, NA, NA, NA, 
NA, NA, NA, "0.17495242646710829", NA, NA, NA, "8.0174590835183634E-2", 
NA, NA, NA, NA, NA, "0.3741514609038552", NA, NA, NA, NA, NA, 
NA, NA, NA, NA, "0.18203421025448296", NA, NA, NA, "0.18032116561517086", 
NA, NA, "-0.24673024063961035", "8.3759133449436751E-2", NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "-0.12597421585178167", 
NA, NA, NA, "0.19902495793991903", NA, NA, NA, NA, "-9.2408051470944635E-2", 
NA, NA, NA, NA, NA, "1.3515493037121162E-2", NA, NA, "-2.2110562098653141E-2", 
NA, NA, NA, NA, "3.5029098159769845E-2", NA, NA, NA, NA, NA, 
NA, NA, "-0.38542680544884389", "0.2673937214255111", NA, NA, 
NA, NA, NA, NA, "0.1514695655588354", NA, "-0.19290183848262321", 
"0.19830470195985717", "0.25974088161209186", "0", "0.12635072134014091", 
"4.3529572197642308E-2", NA, "-2.811733193779542E-2", "5.2999441490886978E-2", 
"-1.5294438792050502E-2", "-0.1092036064257218", NA, NA, NA, 
NA, NA, NA, NA, "-8.4682877918448418E-2", NA, NA, NA, NA, NA, 
NA, "0.33060935555613358", "-0.26950721703104663", NA, NA, NA, 
NA, NA, NA, NA, NA, NA, "2 concerns", "6.5143247152538983E-2"
), `3 concerns` = c(NA, NA, NA, "-6.2005384615384615E-2", NA, 
NA, NA, NA, NA, NA, NA, NA, "0.16466373149154445", "0.14529429748819767", 
NA, NA, "3.3101080910433733E-3", NA, NA, "-0.14716333286324612", 
NA, NA, NA, "0.101855405108354", NA, NA, NA, "1.5624661137794593E-2", 
"4.089776650666388E-2", NA, "-", "0.14868399697158718", NA, NA, 
"8.4936940656134663E-2", "-7.3275278911856751E-5", NA, NA, "0.16209406140402915", 
NA, NA, NA, NA, NA, NA, "7.5733790938149026E-2", "7.7802906849214093E-2", 
NA, "0.29092905402715896", NA, NA, NA, "0.2433591777340911", 
NA, NA, "0.16878584978409417", "0.23450765393402495", NA, NA, 
NA, NA, NA, NA, "1.4972641033242029E-2", NA, NA, "0.15914858376902719", 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "0.23167642917280462", 
"-0.12014200114033269", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, "0.23296355648900113", "9.2737675544091028E-2", 
"-1.135676252608786E-2", "-2.5231545331790839E-3", "-1.831276418414618E-2", 
NA, "3.700270212564627E-2", NA, NA, NA, NA, NA, NA, NA, "0.12864133565206637", 
NA, "0.2713309994071611", NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, "0.33081997450170131", NA, NA, NA, NA, NA, NA, "0.10916148370698719", 
NA, NA, NA, NA, NA, NA, NA, NA, "-1", "-0.10648202245319915", 
NA, NA, NA, NA, NA, NA, "7.6583001375218229E-2", "0.11923826063359644", 
"0.1382325704168097", "4.411629139778972E-2", NA, NA, "-2.7571494462436785E-2", 
"-8.1186210791162505E-2", "0.36815766123347382", "0.21997253864625099", 
"9.5269593575127098E-2", NA, "0.40386694165317971", "0.1317061317077115", 
"8.4533840305895946E-2", NA, NA, NA, "7.064976326243011E-2", 
"8.2533081202996961E-2", NA, NA, NA, NA, "-6.5935523766861404E-2", 
"0.15935278497831473", NA, "0.1159060020401923", NA, NA, "0.11817005685670501", 
"6.1029901139001863E-2", "0.12692362698225845", "3.4415424790262605E-2", 
NA, NA, "0.23155179134453707", "0.14332216947092591", "7.4795242229677816E-2", 
"0", NA, NA, NA, NA, NA, NA, "0.14534924839754271", "0.27815354547396853", 
"0.19493428600637031", "0.1283055485269069", NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "-2.3773331360783301E-2", 
NA, "0.20660830748524073", NA, "0.23154616465669209", NA, "-0.80937062468163068", 
NA, NA, "0.41853447897377194", NA, NA, NA, "9.4089917760579844E-2", 
NA, NA, "6.3552512454774224E-3", "-0.43971479670164443", "0.15974143122420936", 
"-0.16029537031373786", NA, "3 concerns", "7.198593957320798E-2"
), `4 concerns` = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, "6.4352691220779912E-2", NA, NA, 
"0.21279729530946834", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, "-9.3690492677869663E-2", NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, "-7.1185121289991993E-2", NA, NA, "6.569732863463229E-2", 
NA, NA, "9.7222332805540157E-2", NA, NA, NA, NA, NA, NA, NA, 
"8.5074456366478923E-2", NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, "1.7663351832379881E-3", NA, NA, NA, "0.20859715043286409", 
"-4.0588246304824382E-2", NA, NA, NA, NA, NA, NA, NA, NA, "3.1799587621662351E-2", 
NA, "8.6166092731043253E-2", NA, NA, NA, NA, NA, NA, NA, "4.869038187032948E-2", 
"0.18071545075957585", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "4.2986578596766911E-2", 
NA, NA, NA, NA, "9.4277092317434086E-3", NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, "3.5496813684543493E-2", "-8.1501862554191895E-2", 
NA, NA, "9.9940934380241986E-2", NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, "4 concerns", "5.1777127158001604E-2"), `5 concerns` = c(NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, "8.9612836579635591E-2", "8.1063923186028175E-2", 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, "8.4668999169687842E-2", NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, "6.0739595493825904E-2", NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, "9.0340384993987888E-2", NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, "6.1693228854984072E-2", NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
"-0.20631919750140182", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, "5 concerns", "3.7399967253821095E-2"), `6 concerns` = c(NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, "0.32874505543754307", NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, "0.15408216010209475", NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "-4.8467807432570065E-2", 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, "6 concerns", "0.14478646936902259"
)), row.names = c(NA, -238L), class = c("tbl_df", "tbl", "data.frame"
))

1 个答案:

答案 0 :(得分:2)

ggplot要求您的数据“整洁”(有关详细信息,https://cran.r-project.org/web/packages/tidyr/vignettes/tidy-data.html,请参见此处)。 您可以使用gather包中的tidyr函数。由于您的数字值实际上是字符而不是数字,因此您还需要在将它们绘制出来之前将它们转换为数字。 ggplot将删除NA值(带有警告),因此您无需担心它们。 因此代码如下:

library(tidyverse)
Box_Whiskers_Plot <- Box_Whiskers_Plot %>% 
  gather(key = "concerns", "value") %>% 
  mutate(value = as.numeric(value))
ggplot(Box_Whiskers_Plot, mapping = aes(x = concerns, y = value)) +
  geom_boxplot()

结果:

enter image description here