Ari*_*man 6 r ggplot2 colorbrewer
我的数据如下:
完整的数据作为要点.
我试图想象出每个实体的计数比例.为此,我使用了以下代码:
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
