ggplot2 / colorbrewer定性pallette与125个类别 [英] ggplot2/colorbrewer qualitative pallette with 125 categories
问题描述
- 10个州
- 每个州有两种类型
- 每种类型都有1到29个实体
- 每个状态实体类型都有一个计数
完整的数据可作为要点。
我试图想象每个实体的计数比例。为了做到这一点,我使用了下面的代码:
pre $ ic $ cc ic变换(icc,state = factor(state) ,实体=因子(实体),类型=因子(类型))
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在每个实体内分配一个唯一的颜色(希望是相当不同的颜色) type?
到目前为止,唯一的方法是将 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)
混合< - 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
< img src =https://i.stack.imgur.com/BmEAL.pngalt =在这里输入图片描述>
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.
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
这篇关于ggplot2 / colorbrewer定性pallette与125个类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!