我有如下数据:
完整资料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
到一个整数,它可以工作,但不会在级别之间提供太多颜色差异。
最佳答案
这是一种可以为您提供更多信息的方法。取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
关于r - ggplot2/rcolorbrewer 定性调色板,包含 125 个类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14775770/