简体   繁体   English

基于堆叠条形图的颜色

[英]stacked barplot based color

I would like to have a stacked barplot like 1 but where the color is different for each bar such as in 2 with the stack line. 我想有一个类似1的堆积条形图,但是每个条形的颜色都不同,例如2在堆积线中。

x<-matrix(runif(40),ncol=10)
barplot(x,legend=c('part1','part2','part3','part4'), col=rainbow(10))

颜色错误的固定图条形图具有良好的颜色但没有堆栈

I don't know how to do this in base graphics. 我不知道如何在基本图形中执行此操作。 But if you are willing to use ggplot2 , it's pretty easy to do. 但是,如果您愿意使用ggplot2 ,那就很容易做到。 You can for example use the transparency and the color as two distinct things you would like to change. 例如,您可以将透明度和颜色用作您要更改的两个不同的东西。 Here's the code I used. 这是我使用的代码。

require(ggplot2)
# defining the data
set.seed(1)
x<-matrix(runif(40),ncol=10)
df <- data.frame(x=factor(rep(1:ncol(x), each=nrow(x))), 
                 y=as.numeric(x), 
                 part=factor(paste0("part", 1:nrow(x)), levels=paste0("part", nrow(x):1)))
# ggplot call...
ggplot(df, aes(x=x, y=y, fill=x, alpha=part)) + 
  geom_bar(stat="identity") +
  theme_bw(base_size=30) + 
  scale_x_discrete(name="", breaks=NULL) +
  scale_y_continuous(name="") +
  scale_fill_discrete(name="", guide="none") +
  scale_alpha_discrete(name="", range=c(.3,1))

This gives you the following figure. 这给您下图。 在此处输入图片说明

Of course you can then change the colors and transparencies at will. 当然,您可以随意更改颜色和透明胶片。 Just change the scale_alpha_discrete and scale_fill_discrete function calls. 只需更改scale_alpha_discretescale_fill_discrete函数调用即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM