r - ggplot2/rcolorbrewer 定性调色板,包含 125 个类别

标签 r ggplot2 colorbrewer

我有如下数据:

  • 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
    

    plot

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

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

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

    最佳答案

    这是一种可以为您提供更多信息的方法。取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
    

    enter image description here

    关于r - ggplot2/rcolorbrewer 定性调色板,包含 125 个类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14775770/

    相关文章:

    r - R 中的土耳其语字符编码

    r - data.table R 中的子集 ID 和日期

    r - 将一个或多个系数设置为特定整数

    r - 在ggplot2中创建后更改比例图例标签

    r - 显示 ggplot2 使用的颜色的十六进制代码

    r - 如何为 R Plotly 散点图中的每个点显式分配唯一的颜色?

    r - 从 HMM 高斯混合分布中获取下一个观察结果

    r - 如何将图像转换为ggplot2散点图的点数据

    r - 在 R 中,如何制作一个六条形图,其中每个条形包含一系列数据?

    r - ggplot 中十分位数的低、中高颜色