如何手动将颜色映射到树图?

时间:2019-05-30 04:27:36

标签: r treemap

我正在使用treemap包中的treemap创建视觉表示。

简而言之,我从一个值表中创建一个treemap,并根据这些值将红色,灰色和绿色映射到treemap的各个部分。

在某些情况下,此方法有效,但在其他情况下,则无效,并且我收到错误消息,无法解决:

Error in do.call("format", args.legend) : second argument must be a list

到目前为止,似乎在设置type = "value"时,如果父组中的所有框的大小都为0,而您尝试指定颜色,则会发生错误。但是,如果在父组中有两个框,并且将1设置为零,则只要这两个组中的颜色规格不同,它仍将起作用。

我将在下面尝试概述。

这有效:根据在-1, 0列中为它们分配了1还是Colour,出现一个红色,灰色和绿色的框。

# This is just c(red, grey, green)
my_colour_pal <- c("#bc0303", "#4b5566", "#17a008")
df <- 
  data.frame(Group1 = c("one", "one", "two"),
             Group2 = c("val1", "val2", "val3"),
             Value = c(10, 10, 10),
             Colour = c(-1, 0, 1), stringsAsFactors = FALSE) 

treemap(df, index = c("Group1", "Group2"), type = "value",
        vSize = "Value", vColor = "Colour", palette = my_colour_pal)

这也起作用:以相同的方式,除了一个框消失了,因为Value等于0。正如预期的那样,颜色映射有效。

df <- 
  data.frame(Group1 = c("one", "one", "two"),
             Group2 = c("val1", "val2", "val3"),
             Value = c(10, 0, 10),
             Colour = c(-1, 0, 1), stringsAsFactors = FALSE) 

treemap(df, index = c("Group1", "Group2"), type = "value",
        vSize = "Value", vColor = "Colour", palette = my_colour_pal)

这不起作用:Group1的两个one分配都将Value设置为零

df <- 
  data.frame(Group1 = c("one", "one", "two"),
             Group2 = c("val1", "val2", "val3"),
             Value = c(0, 0, 10),
             Colour = c(-1, 0, 1), stringsAsFactors = FALSE) 

treemap(df, index = c("Group1", "Group2"), type = "value",
        vSize = "Value", vColor = "Colour", palette = my_colour_pal)

Error in do.call("format", args.legend) : second argument must be a list

但是,如果我执行相同的操作并将第三个值设置为0,那么它将正常工作

my_colour_pal <- c("#bc0303", "#4b5566", "#17a008")
df <- 
  data.frame(Group1 = c("one", "one", "two"),
             Group2 = c("val1", "val2", "val3"),
             Value = c(10, 10, 0),
             Colour = c(-1, 0, 1), stringsAsFactors = FALSE) 

treemap(df, index = c("Group1", "Group2"), type = "value",
        vSize = "Value", vColor = "Colour", palette = my_colour_pal)

最后,如果我做的与上次相同,但是将第1行和第2行的颜色设置为相同,例如-1代表“红色”,则出现相同的错误,

df <- 
  data.frame(Group1 = c("one", "one", "two"),
             Group2 = c("val1", "val2", "val3"),
             Value = c(10, 10, 0),
             Colour = c(-1, -1, 1), stringsAsFactors = FALSE) 

treemap(df, index = c("Group1", "Group2"), type = "value",
        vSize = "Value", vColor = "Colour", palette = my_colour_pal)

Error in do.call("format", args.legend) : second argument must be a list

摘要

如果父组中的两个框都等于零,并且父组中的两个框的“颜色”值设置为相同的数字,则似乎发生错误。

0 个答案:

没有答案