rggplot2

ggplot2 plotting a 100% stacked area chart


I want to plot a 100% stacked area chart out of a dataframe, where each row of the df sums up to 1. An example dataframe is here: https://pastebin.com/ADMQP6Nx

What i want ultimately is something like this: enter image description here

Most solutions i found so far here on SO have a different data.frame structure, where the grouping variables are defined in rows rather than as columns, e.g.:

+--------+-------+-------+
| Period | Group | Value |
+--------+-------+-------+
| 1      | t1    | 0.3   |
| 1      | t2    | 0.1   |
| 1      | t3    | 0.4   |
| 1      | t4    | 0.2   |
| 2      | t1    | 0.5   |
| 2      | t2    | 0.1   |
| 2      | t3    | 0.3   |
| 2      | t4    | 0.2   |
| ...    | ...   | ...   |
+--------+-------+-------+

And they use then ggplot like this:

ggplot(data, aes(x=Period, y=Value, fill=Group)) +  geom_area()

Is there a solution without transforming the data frame?


Solution

  • Transforming the data with gather:

    library(tidyverse)
    
    df <- structure(
      list(
        Period = 1:4,
        t1 = c(0.3, 0.5, 0.1, 0.4),
        t2 = c(0.1, 0.1, 0.3,
               0.2),
        t3 = c(0.4, 0.2, 0.4, 0.3),
        t4 = c(0.2, 0.2, 0.2, 0.1)
      ),
      .Names = c("Period", "t1",
                 "t2", "t3", "t4"),
      row.names = c(NA, -4L),
      class = "data.frame"
    )
    
    
    df %>% 
      gather(Group, Value, t1:t4) %>% 
      ggplot(aes(x=Period, y=Value, fill=Group)) + 
      geom_area()
    

    enter image description here