ggplot2/colorbrewer qualitative pallette with 125 categories

北慕城南 提交于 2019-12-07 03:28:49

问题


I have data as follows:

  • 10 states
  • Each state has two types
  • Each type has between 1 and 29 entities
  • Each state-entity-type has a count

Complete data available as a gist.

I'm trying to visualize what proportion of the counts were made for each entity. To do that, I've used the following code:

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

Unfortunately, I'm losing a lot of information because state-types with lots of entities aren't displaying enough unique colors.

As mentioned above, I have 125 entities, but the most entities in a state-type is 29. Is there a way to force ggplot2 and colorbrewer to assign a unique (and hopefully fairly distinct) color within each entity-type?

The only way I've come up with so far is to coerce entity to an integer, which works but doesn't provide much color differentiation between levels.


回答1:


Here's an approach that gives you a little more information. Take the color wheel generated by rainbow, and for every other color, swap it with the opposite one on the wheel.

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



来源:https://stackoverflow.com/questions/14775770/ggplot2-colorbrewer-qualitative-pallette-with-125-categories

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!