2

我正在尝试为两个因素的特定级别创建一个颜色列表。参数如下:

> df.coldata
        Condition  Tank
R235      Control    T6
R236  LowExposure    T6
R239 HighExposure    T6
R241      Control    T8
R242  LowExposure    T8
R245 HighExposure    T8
R247      Control T14_3
R248  LowExposure T14_3
R250 HighExposure T14_3

由于我不想手动填写储罐编号或条件,因此我试图使用分配的变量创建一个列表,如下所示:

### Specify colors ####
Tanks <- levels(df.coldata$Tank)
Conditions <- levels(df.coldata$Condition)

ann_colors <- list(
  Condition = c(Conditions[1]="lightskyblue", # This doenst work ... BUGS here!!!
                Conditions[3]="royalblue1",
                Conditions[2]="navyblue"),
  Tank = c(Tanks[1]="gray90",
           Tanks[2]="gray65",
           Tanks[3]="gray40")
)

但这会产生一个错误,告诉我:

Error: unexpected '=' in:
"ann_colors <- list(
  Condition = c(Conditions[1]="

当我运行代码时:

ann_colors <- list(
  Condition = c(Control="lightskyblue",
                LowExposure="royalblue1",
                HighExposure="navyblue"),
  Tank = c(T14_3="gray90",
           T6="gray65",
           T8="gray40")
)

它就像一个魅力。我究竟做错了什么?我错过了什么吗?

4

1 回答 1

5

改用setNames

ann_colors <- list(
  Condition = setNames(c("lightskyblue", "royalblue1", "navyblue"), Conditions),
  Tank = setNames(c("gray90", "gray65", "gray40"), Tanks)
  )

您的代码错误的原因是我们试图在c(). 下面会起作用,并将条件的第一个值替换为"lightskyblue",但这不是我们想要的:

Conditions[1] = "lightskyblue"
Conditions
# [1] "lightskyblue" "HighExposure" "LowExposure" 

并将其包装起来会c()引发错误:

c(Conditions[1] = "lightskyblue")
# Error: unexpected '=' in "c(Conditions[1] ="
于 2020-01-21T13:20:54.937 回答