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.
scale_fill_manual( values=c(rep(c('red','blue'),125/2),'red') )
, but then they're not guaranteed to be alternating within each bar. – Northropalternate <- function(x) factor(match(x, unique(x)) %% 2))
, and apply it within groups withave
:ave(entity, state, FUN = alternate)
(untested) – Stinky