2

I am trying to create a stacked area chart of 4 categories of data using geom_area in R. I am able to construct line charts for the categories using geom_line() but when I use geom_area to show shaded regions, the vertical scale is incorrect. It looks like it may be doing some summing of the data. My line chart code looks like this and produces an accurate plot with a range of values from 0 to approximately $800,000:

lp3b<-ggplot(
  data=myHousing,aes(x=Year,y=Home.Value,color=State))+geom_line()
lp3b

My area plot code, below, produces a plot the visually is correct but the vertical scale goes from 0 - to about $1,600,000 which is definitely not the correct range of values. I checked the range of data values before and after plotting and the area chart definitely has the scale wrong.

lp3c<-ggplot()+geom_area(data=myHousing,
  aes(x=Year,y=Home.Value,fill=State),alpha=0.5,position="stack")
lp3c

Line chart which is correct Area chart with incorrect vertical scale

stefan
  • 90,330
  • 6
  • 25
  • 51
PeterG
  • 23
  • 3
  • 5
    It is stacking the areas (you even explicitly defined this, but I think it's also the default). Use `position = 'identity'` as an argument for `geom_area`. – Axeman Sep 29 '20 at 20:38
  • 1
    For some explanation here, `position="stack"` is the default behavior for `geom_area()`. What that does is take the y values for each of the things your stacking and adds them together. What you see on the y axis is therefore the *sum of all y values*. Setting to `position="identity"` will make it so that the position of y is plotted as the value of y, rather than a sum of y values "below" that. – chemdork123 Sep 29 '20 at 21:54
  • Yes that fixes the issue. Thank you for the help and insight! – PeterG Sep 30 '20 at 18:23

0 Answers0