【问题标题】:stacked barplot based color基于堆积条形图的颜色
【发布时间】:2014-04-02 12:45:44
【问题描述】:

我想要一个像1 这样的堆叠条形图,但是每个条形的颜色都不同,例如2 中的堆栈线。

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

【问题讨论】:

标签: r colors bar-chart


【解决方案1】:

我不知道如何在基本图形中做到这一点。但是如果你愿意使用ggplot2,这很容易做到。例如,您可以将透明度和颜色用作您想要更改的两个不同的东西。这是我使用的代码。

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))

这为您提供了下图。

当然,您可以随意更改颜色和透明度。只需更改 scale_alpha_discretescale_fill_discrete 函数调用即可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-03
    • 2020-01-30
    • 1970-01-01
    相关资源
    最近更新 更多