如何将颜色分配给变量子集ggplot2 [英] how to assign colour to subset of variables ggplot2
问题描述
我有一个379838行数据框和13个变量列(13个临床样本):
> str(df)
'data.frame':379838 obs。 13个变量:
$ V1:数字0.8146 0.7433 0.0174 0.177 0 ...
$ V2:数字0.7465 0.5833 0.0848 0.5899 0.0161 ...
$ V3:数字0.788 0.843 0.333 0.801 0.156。 ..
$ V4:num 0.601 0.958 0.319 0.807 0.429 ...
$ V5:num 0.792 0.49 0.341 0.865 1 ...
$ V6:0.676 0.801 0.229 0.822 0.282 ...
$ V7:0.783 0.732 0.223 0.653 0.507 ...
$ V8:0.69 0.773 0.108 0.69 0.16 ...
$ V9:0.4014 0.5959 0.0551 0.7578 0.2784 ...
$ V10:0.703 0.784 0.131 0.698 0.204 ...
$ V11:0.6731 0.8224 0.125 0.6021 0.0772 ...
$ V12:0.7889 0.7907 0.0881 0.7175 0.2392 ...
$ V13:num 0.6731 0.8221 0.0341 0.4059 0 ...
我试着制作一个ggplot2盒子将变量分为三组:V1-V5,V6-V9和V10-V13,并为每组变量分配不同的颜色。
我正在尝试以下代码:
df1 = as.vector (df [,c(V1,V2,V3,V4,V5)])
df2 = as.vector(df [,c(V6,V7 ,V8,V9)])
df3 = as.vector(df [,c(V10,V11,V12,V13)])
sample = (df,varnames =sample)
str(meltData1)
'data.frame':4937894 obs。 2个变量:
$变量:具有13个级别的因子V1,V2,V3,..:1 1 1 1 1 1 1 1 1 1 ...
$ value :数字0.8146 0.7433 0.0174 0.177 0 ...
p = ggplot(data = meltData1,aes(variable,value,fill = x $ sample))
p + geom_boxplot()
这给了我白色方块图。我如何将颜色分配给三组变量?提前谢谢了! / code>至 V13
。
df< -as .data.frame(matrix(rnorm(1300),ncol = 13))
从库变量
和值
。
library(reshape2)
dflong< -melt(df)
添加长格式新列 sample
。在这里,我重复命名 group1
, group2
, group3
根据
dflong $ sample< -c(rep( rep1(group2),nrow(df)* 5),rep(group2,nrow(df)* 4),rep(group3,nrow(df)* 4))
新栏目用于参数 fill =
根据分组设置颜色。
library(ggplot2)
ggplot(data = dflong,aes(variable,value,fill = sample))+ geom_boxplot()
I have a data frame of 379838 rows and 13 variables in columns( 13 clinical samples) :
> str( df)
'data.frame': 379838 obs. of 13 variables:
$ V1 : num 0.8146 0.7433 0.0174 0.177 0 ...
$ V2 : num 0.7465 0.5833 0.0848 0.5899 0.0161 ...
$ V3 : num 0.788 0.843 0.333 0.801 0.156 ...
$ V4 : num 0.601 0.958 0.319 0.807 0.429 ...
$ V5 : num 0.792 0.49 0.341 0.865 1 ...
$ V6 : num 0.676 0.801 0.229 0.822 0.282 ...
$ V7 : num 0.783 0.732 0.223 0.653 0.507 ...
$ V8 : num 0.69 0.773 0.108 0.69 0.16 ...
$ V9 : num 0.4014 0.5959 0.0551 0.7578 0.2784 ...
$ V10: num 0.703 0.784 0.131 0.698 0.204 ...
$ V11: num 0.6731 0.8224 0.125 0.6021 0.0772 ...
$ V12: num 0.7889 0.7907 0.0881 0.7175 0.2392 ...
$ V13: num 0.6731 0.8221 0.0341 0.4059 0 ...
and I am trying to make a ggplot2 box plot grouping variables into three groups: V1-V5 , V6-V9 and V10-V13 and assigning different color to variables of each group.
I am trying the following code:
df1= as.vector(df[, c("V1", "V2", "V3","V4", "V5")])
df2= as.vector(df[, c("V6","V7", "V8","V9")])
df3=as.vector(df[, c( "V10","V11", "V12","V13")])
sample= c(df1,df2,df3)
library(reshape2)
meltData1 <- melt(df, varnames="sample")
str(meltData1)
'data.frame': 4937894 obs. of 2 variables:
$ variable: Factor w/ 13 levels "V1","V2","V3",..: 1 1 1 1 1 1 1 1 1 1 ...
$ value : num 0.8146 0.7433 0.0174 0.177 0 ...
p=ggplot(data=meltData1,aes(variable,value, fill=x$sample))
p+geom_boxplot()
That gives me white box plots. How can I assign a colour to three groups of variables? Many thanks in advance!
As sample data were not provided, made new data frame containing 13 columns with names from V1
to V13
.
df<-as.data.frame(matrix(rnorm(1300),ncol=13))
With function melt()
from library reshape2
data are transformed from wide to long format. Now data frame has two columns: variable
and value
.
library(reshape2)
dflong<-melt(df)
To the long format new column sample
is added. Here I repeated names group1
, group2
, group3
according to number of row in original data frame and number of original columns in each group.
dflong$sample<-c(rep("group1",nrow(df)*5),rep("group2",nrow(df)*4),rep("group3",nrow(df)*4))
New column is used with argument fill=
to set colors according to grouping.
library(ggplot2)
ggplot(data=dflong,aes(variable,value, fill=sample))+geom_boxplot()
这篇关于如何将颜色分配给变量子集ggplot2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!