3

I'm having a bit of a problem creating my stacked area chart in ggplot2 in R, for some reason it is returning as a stacked line;

e.g. data:

library(ggplot2)

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

and then using ggplot2 to create a traditional stacked area chart;

ggplot(df,aes(Year, Value,fill=Sector))+geom_area(aes(colour=Sector),position="stack")

for me, that returns a stacked line as per below. Is it my version of R? or my code? the code for ggplot2 looks like every other eg of a stacked area I can find online but I get odd results and I can't find any info.

enter image description here

closed as off-topic by Aaron Jan 24 '18 at 2:05

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • @Aaron Since this post is off-topic, couldn't it be migrated to stack overflow? I'm certainly not at a moderator level, but I've seen questions marked as "migrated" or some equivalent. It is a useful question+answer but definitely doesn't belong here. – user3386170 Jan 24 '18 at 1:35
  • @user3386170 Thanks for the heads-up. Unfortunately, this question is too old to migrate to SO as it needs to be within 60 days of the post. More details here: meta.stackexchange.com/questions/10249/… – Aaron Jan 24 '18 at 2:06
  • @Aaron Thanks for the info on migration. Now that I've read the info you linked, I'll keep in mind that I can flag posts as off-topic for attention. – user3386170 Jan 24 '18 at 15:56
6
# make it reproducible
set.seed(1492)
Sector <- rep(c("S01","S02","S03","S04","S05","S06","S07"),times=7)
Year <- rep(c("1950","1960","1970","1980","1990","2000","2010"),each=7)
Value <- runif(49, 10, 100)
df <- data.frame(Sector,Year,Value)

gg <- ggplot(df, aes(x=as.numeric(as.character(Year)), y=Value))
gg <- gg + geom_area(aes(colour=Sector, fill=Sector))
gg

enter image description here

This is an alternate way:

gg <- ggplot(df, aes(x=as.numeric(Year), y=Value))
gg <- gg + geom_area(aes(colour=Sector, fill=Sector))
gg <- gg + scale_x_discrete(labels=levels(df$Year))
gg
  • looks good, is it purely because the Year needs to be represented as a numeric and not a factor? – Sam Sep 16 '15 at 12:34
  • aye. that's a req for the x aesthetic in geom_area. you could just make Year numeric in df as well. – hrbrmstr Sep 16 '15 at 12:35
  • lovely thanks, i shall now mess with the colours etc! – Sam Sep 16 '15 at 14:08
  • Why was the Year variable constructed with the quotation marks around each number? setting it as Year <- rep(c(1950,1960,1970,1980,1990,2000,2010),each=7) solves the issue of being interpreted as character. But does it lead to other issues to be aware of? – user3386170 Jan 24 '18 at 1:44
  • 1
    b/c the OP had it that way and it was likely read in that way but they posted it the way they did to make a short, reproducible example. In reality, it should be a really be a Date object vs a a simple numeric year – hrbrmstr Jan 24 '18 at 2:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.