How can I make a stacked area chart?

时间:2018-11-27 00:47:47

标签: r ggplot2

I am trying to make a stacked area chart:

library(ggplot2)

ggplot(data, aes(x = variable, y = value, fill = term)) +
   geom_area()

But this leads to a blank plot.

blank plot

Expected plot (example)

expected

How can I do this?

data <- structure(list(term = c("models", "percentiles", "models:percentiles", 
                                "models", "percentiles", "models:percentiles", 
                                "models", "percentiles", "models:percentiles"), 
                       variable = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L), 
                                            .Label = c("R2", "R5", "R10"), class = "factor"), 
                       value = c(0.435697205847009, 0.533615307749147, 0.0306874864038442, 
                                 0.441369621882273, 0.520198994695284, 0.0384313834224421, 
                                 0.394491546635206, 0.579421546902868, 0.0260869064619254)), 
                  row.names = c(NA, -9L), class = "data.frame")

1 个答案:

答案 0 :(得分:4)

You are only missing the group aesthetic:

ggplot(data, aes(x = variable, y = value, group = term, fill = term)) +
    geom_area(color = "black")

enter image description here