为堆叠区域图指定颜色

时间:2019-03-29 23:34:07

标签: r ggplot2 geom-area

我想在下面的代码中为“部门”的每一列更改特定颜色(比彩虹默认值更详细)

我已经读过geom_area不允许更改颜色?如果可以的话,还有另一种无需使用ggplot2软件包即可绘制该图的方法吗?

pip

1 个答案:

答案 0 :(得分:3)

有很多方法可以更改ggplot中的色标。

在这里,您可以精确指定所需的颜色:

Sector <- rep(c("S01","S02","S03","S04","S05","S06","S07"),times=7)
Year <- as.numeric(rep(c("1950","1960","1970","1980","1990","2000","2010"),each=7)) 
Value <- runif(49, 10, 100) 
data <- data.frame(Sector,Year,Value)
data

ggplot(data, aes(x=Year, y=Value, fill=Sector)) + geom_area(stat="identity") +
  scale_fill_manual(values = c("black","blue","gray","orange","tan","purple","darkgray"))