【问题标题】:ggplot2/colorbrewer qualitative pallette with 125 categories具有 125 个类别的 ggplot2/rcolorbrewer 定性调色板
【发布时间】:2013-01-24 09:47:15
【问题描述】:

我的数据如下:

  • 10 个州
  • 每个状态有两种类型
  • 每种类型都有 1 到 29 个实体
  • 每个状态实体类型都有一个计数

完整数据available as a gist

我试图想象每个实体的计数比例。为此,我使用了以下代码:

icc <- transform( icc, state=factor(state), entity=factor(entity), type=factor(type) )
p <- ggplot( icc, aes( x=state, y=count, fill=entity ) ) +
  geom_bar( stat="identity", position="stack" ) +
  facet_grid( type ~ . )
custom_theme <- theme_update(legend.position="none")
p

不幸的是,我丢失了很多信息,因为具有大量实体的状态类型没有显示足够的独特颜色。

如上所述,我有 125 个实体,但状态类型中的实体最多为 29 个。有没有办法强制 ggplot2 和 colorbrewer 在每个实体类型中分配唯一(并且希望相当不同)颜色?

到目前为止,我想出的唯一方法是将entity 强制转换为一个整数,这可行,但不会在级别之间提供太多颜色差异。

【问题讨论】:

  • @Arun 制作这样一个具有良好分歧方案的调色板并非易事(可能是一个很好的答案,眨眼)。需要在每个状态类型的实体上运行颜色 brewer(这本身就有问题,因为似乎大多数调色板都超过了 12 个),然后按正确的顺序放置。
  • 很难想出 29 种不同的颜色。为什么不直接用文本标记实体?
  • @hadley 注意到。文本会很棒,但最终可能会变得一团糟。我想我的要求实际上要简单得多:颜色甚至不必在状态类型中是唯一的,只需交替即可。我可以尝试scale_fill_manual( values=c(rep(c('red','blue'),125/2),'red') ) 之类的方法,但不能保证它们在每个小节中交替出现。
  • 您可以使用alternate &lt;- function(x) factor(match(x, unique(x)) %% 2)) 创建一个新变量,并使用ave 在组中应用它:ave(entity, state, FUN = alternate)(未经测试)

标签: r ggplot2 colorbrewer


【解决方案1】:

这是一种可以为您提供更多信息的方法。取rainbow 生成的色轮,对于其他所有颜色,将其与色轮上相反的颜色交换。

col <- rainbow(30)
col.index <- ifelse(seq(col) %% 2, 
                    seq(col), 
                    (seq(ceiling(length(col)/2), length.out=length(col)) %% length(col)) + 1)
mixed <- col[col.index]

p <- ggplot(icc, aes(x=state, y=count, fill=entity)) +
  geom_bar(stat="identity", position="stack") +
  facet_grid( type ~ . ) + 
  scale_fill_manual(values=rep(mixed, length.out=nrow(icc)))

custom_theme <- theme_update(legend.position='none')
p

【讨论】:

    猜你喜欢
    • 2012-10-21
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 2021-06-15
    • 1970-01-01
    • 2017-01-24
    • 2020-07-07
    • 1970-01-01
    相关资源
    最近更新 更多