Reputation: 1
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.sstatic.net/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.
Upvotes: 0
Views: 270
Reputation: 719
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")
Upvotes: 1