Search code examples
rggplot2geom-area

Specify colour to a stacked area graph


I would like to change specific colours (oder than rainbow default) to each column "Sector" in the code below

I've read that geom_area doesn't allow changes in colour? if so is there another way to plot this graph without using ggplot2 package?

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

Solution

  • There are a lot of ways to change the color scale in ggplot.

    Here's a way where you can specify exactly which colors you want:

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