【问题标题】:how to assign colour to subset of variables ggplot2如何将颜色分配给变量ggplot2的子集
【发布时间】:2013-02-15 06:01:09
【问题描述】:

我有一个包含 379838 行和 13 个列变量的数据框(13 个临床样本):

 >  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 ...

我正在尝试制作一个 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= 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()

这给了我白盒图。如何为三组变量分配颜色?提前谢谢了!

【问题讨论】:

  • 欢迎来到 SO !在您的问题中添加数据样本可能会很有用。例如,您可以为此使用dput(head(df))

标签: r ggplot2


【解决方案1】:

由于未提供示例数据,因此制作了包含 13 列的新数据框,名称从 V1V13

df<-as.data.frame(matrix(rnorm(1300),ncol=13))

使用库reshape2 中的函数melt() 将数据从宽格式转换为长格式。现在数据框有两列:variablevalue

library(reshape2)
dflong<-melt(df)

向长格式添加新列sample。这里我根据原始数据框中的行数和每组的原始列数重复了名称group1group2group3

dflong$sample<-c(rep("group1",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()

【讨论】:

  • (+1) 如果在第一行将data.frame(.) 更改为as.data.frame(.),则不必使用colnames 设置列名。
  • @Arun (+1) 不知道 data.frame() 和 as.data.frame() 之间的区别。
  • 当然,NP。如果您在 R 终端输入 as.data.frame.matrix,您会看到 names(value) &lt;- paste0("V", ic) 已明确设置。但是如果你输入data.frame,那么你会看到row.names被复制回来了(只是为了说明原因)。
  • 它只适用于 data.matrix 而不是 matrix(x)。非常感谢!
【解决方案2】:

这是 Didzis Elferts 的后续作品。

目的:将样品分成3个颜色组,颜色组内的色度不同。

第一部分代码相同:

df<-as.data.frame(matrix(rnorm(1300),ncol=13))
library(reshape2)
dflong<-melt(df)
dflong$sample<-c(rep("group1",nrow(df)*5),rep("group2",nrow(df)*4),rep("group3",nrow(df)*4))
library(ggplot2)

现在,使用 RColorBrewer 包来选择色调

library(RColorBrewer)

按颜色类创建颜色列表

col.g <- c(brewer.pal(9,"Greens"))[5:9] # select 5 colors from class Greens
col.r <- c(brewer.pal(9,"Reds"))[6:9] # select 4 colors from class Reds
col.b <- c(brewer.pal(9,"Blues"))[6:9] # select 4 colors from class Blues
my.cols <- c(col.g,col.r,col.b)

看看选择的颜色:

image(1:13,1,as.matrix(1:13), col=my.cols, xlab="my palette", ylab="", xaxt="n", yaxt="n", bty="n")

现在使用我们创建的颜色进行绘图

ggplot(data=dflong,aes(variable,value,colour=variable))+geom_boxplot()+scale_colour_manual(values = my.cols)

在上面,使用 color 和 scale_colour_manual 命令,只有线条被着色。下面,我们使用fill和scale_fill_manual:

   ggplot(data=dflong,aes(variable,value,fill=variable))+geom_boxplot()+scale_fill_manual(values = my.cols)

附:我是一个完全的新手,我自己也在学习 R。我认为这个问题是一个应用我刚刚学到的东西的机会。

【讨论】:

  • 太好了,帕特里克,非常感谢!我的主要头痛是使用 rep 命令对变量进行分组......
猜你喜欢
  • 2019-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-13
  • 2015-07-30
  • 1970-01-01
  • 2011-10-18
相关资源
最近更新 更多