0

I've been trying to plot a stacked area plot using ggplot2. My problem is that the y-axis of my plot is not accurately showing the range of value. My largest number for value is 100.

  Date          A         B          C         D      E
2019-12-31     0.0       0.0        0.0       0.0    0.0
2019-12-24     0.0       0.0        0.0       0.0    0.0
...
2016-12-27    18.09     81.91      40.21      9.70   1.7
...
2014-01-28    92.11     7.89       0.00       0.00   0.00
...
2000-01-01     0.0       0.0        0.0        0.0    0.0

I formated the data from wide to long

long_data <- melt(data,measure.vars = c("A","B","C","D","E"), variable.name = "Intensity") 

the plottedggplot(long_data,aes(x = Date,y = value,fill = Intensity)) + geom_area() My plot looked like this

[stacked area plot][1] [1]: https://i.stack.imgur.com/i5ksF.png I have done this in excel and the graph is the same. I've tried adding a limit to the y-axis but it doesn't help. But, one thing I noticed is that if I do geom_line() the y-axis is scaled correctly.

  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Share data in a way that can be copy/pasted into R (don't use "..." because that's not reproducible). If it's stacked, the height will be the sum of all the categories at a time. So do you not want it stacked? Do you just want it to overlap like the lines do? – MrFlick Jul 17 '20 at 00:28

1 Answers1

1

The geom_area's default is position = "stacked" while you need it to be position = "identity". The caveat here is you need to also make your fill variable a factor, basing the levels on the y value, otherwise some of the values may be hidden behind a bigger value. Another alternative is to set alpha = .3 for geom_area

Without a data set it is hard for me to test beforehand. Try using dput(data) and copy and pasting the output into your next question

library(tidyverse)
library(forcats)

long_data %>%
mutate(Intensity = fct_reorder(Intensity, value, .desc = TRUE)) %>%
ggplot(long_data,aes(x = Date, 
                     y = value,
                     fill = Intensity)) + 
geom_area(position = "identity")
NotThatKindODr
  • 729
  • 4
  • 14