Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2/colorbrewer qualitative pallette with 125 categories

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

plot

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.

like image 734
Ari B. Friedman Avatar asked Feb 08 '13 15:02

Ari B. Friedman


1 Answers

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

enter image description here

like image 155
Matthew Plourde Avatar answered Oct 20 '22 01:10

Matthew Plourde