将多组值组合成熔化格式

时间:2016-03-29 19:41:41

标签: r ggplot2

GG CC TTT中包含三组值 我想在facet_grid中制作这三组的箱线图。我不知道如何将其转换为熔化形式列。

 > GG
 [1]  1.3813117  1.5163896  0.8453227  0.9759973  1.2785990  1.1531961  0.6262850  0.9461095 -0.6565960  0.5073075
[11] -0.5743223  0.8198548 -0.8382672 -0.8611467  0.8043137 -0.4957297 -0.4407040  0.5400756
> CC
[1]  1.9218347  1.3246420 -1.2941452 -0.8447140 -0.8006692  0.9272802  0.9116373  0.5005044
> TTT
 [1]  1.1537972 -1.3378830 -1.4823257  1.1091238  1.2039213  1.3535458 -0.7855620  1.3526200  1.0546998 -1.1473508
[11]  0.9604922  1.2368439  0.8762957  1.0700588 -1.0390931  1.0316614  0.9468012  0.6362604  0.9870086 -0.7457397
[21]  0.9022420 -0.8685273  0.8720614  1.0322280  0.4624016 -0.8120936  0.3801744 -0.7628217 -0.6289436  0.6722584
[31] -0.5488791  0.6531975 -0.5396668  0.5503613 -0.7208796  0.6721789  0.5905945  0.6541330 -0.5690710  0.5785989
[41]  0.5545739  0.5375973  0.2840381

2 个答案:

答案 0 :(得分:2)

这是一个选项:

<div>
  <p class="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  </p>
</div>

此处不需要构面,但如果需要,您可以按以下方式进行构面:

library(ggplot2)

# Create some fake-data vectors
set.seed(195)
GG = rnorm(30)
CC = rnorm(20)
TTT = rnorm(50)

# Combine into a data frame
df = data.frame(source=rep(c("GG","CC","TTT"), sapply(list(GG,CC,TTT), length)),
                values=c(GG,CC,TTT))

ggplot(df, aes(source, values)) +
  geom_boxplot()

答案 1 :(得分:1)

我们可以将其放在list中并使用boxplot中的base R

boxplot(list(GG, CC, TTT))

或使用ggplot2dplyr

library(dplyr)
library(reshape2)
library(ggplot2)
melt(mget(c("GG", "CC", "TTT"))) %>%
         ggplot(., aes(x=L1, y=value))+
                          geom_boxplot()