0

using the data below, and the line of code below, I am trying to produce a stacked area plot showing planned spend by project across the quarters specified in the data. Capex on the Y axis, quarters on the X axis. I have looked at many examples here and elsewhere, and I just cannot understand why it is failing. I'd like to post a screenshot of the result - but cant see a way to do that. Basically, it has the legend, and the axes look correct. But the main area of the chart is simply a grey grid, empty.

Code:

ggplot(short, aes(x=Quarter,y=Capex, fill=ProjectName, )) + geom_area(position = "stack") + ylim (1, 100000)

data:

ProjectName Quarter Capex
a   F01 Jul 41709
a   F02 Aug 41696
a   F03 Sep 41667
a   F04 Oct 41712
a   F05 Nov 41676
a   F06 Dec 41674
a   F07 Jan 41694
a   F08 Feb 41693
a   F09 Mar 41698
a   F10 Apr 41710
a   F11 May 41694
a   F12 Jun 41671
b   F01 Jul 265197
b   F02 Aug 265200
b   F03 Sep 265187
b   F04 Oct 265190
b   F05 Nov 265179
b   F06 Dec 265170
b   F07 Jan 265167
b   F08 Feb 265174
b   F09 Mar 265187
b   F10 Apr 265169
b   F11 May 265186
b   F12 Jun 265208
c   F01 Jul 233335
c   F02 Aug 233352
c   F03 Sep 233344
c   F04 Oct 233344
c   F05 Nov 233344
c   F06 Dec 233350
c   F07 Jan 32
c   F08 Feb 31
c   F09 Mar 23
c   F10 Apr 5046
c   F11 May 5005
c   F12 Jun 50
d   F01 Jul 40
d   F02 Aug 43
d   F03 Sep 30
d   F04 Oct 5038
d   F05 Nov 45
d   F06 Dec 8
d   F07 Jan 45
d   F08 Feb 20034
d   F09 Mar 40
d   F10 Apr 40
d   F11 May 2
d   F12 Jun 500045
e   F01 Jul 300011
hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
  • Can you post a reproducible form of your data (hard to tell which column "F10" etc are in). – mnel Jul 31 '13 at 01:45

1 Answers1

1

I'm pretty sure you want a stacked bar chart, not an area chart? Is this what you're after?

ggplot(short, aes(x=Quarter,y=Capex, fill=ProjectName, )) + 
  geom_bar(stat = "identity")

enter image description here

I'm not sure why you've got those y axis limits, they cut off your data, but this should be done with scale_y_continuous(limits = c(min, max)).

As a note, it's better to use the output from dput(data) when sharing your data, as it brings the structure of the data along with it. Have a look at How to make a great R reproducible example?

Community
  • 1
  • 1
alexwhan
  • 15,636
  • 5
  • 52
  • 66
  • Thanks for your help!! And thanks for the tip on dput(data). I will use that in future. I am actually interested in representing the data as geom_area .. as the actual dataset is much larger (815 observations)- and the stacked area chart allows the eye to track left to right better as it follows the ribbon of colour. And when I change geom_bar to geom_area, I get back to the blank grid again. Thanks too for the tip about the Y axis. I dont actually need that limit I guess. – Nicholas James Thorpe Jul 31 '13 at 02:40
  • OK I understand why you want an area chart - the reason it's not working with your data is because of the way `Quarter` is coded. For an area chart, my understanding is it will need to be continuous (numeric, date), not categorical. – alexwhan Jul 31 '13 at 03:02
  • Ahhhhhhhhhh....Thanks so much. So I will represent my "quarters" data in a format R understands as a date. – Nicholas James Thorpe Jul 31 '13 at 04:43