Search code examples
rggplot2area

Struggling with Stacked Area Chart


I have been struggling to create an area chart using ggplot for a while now and to no avail!

Here is my code:


strings <- cbind("rstarUS","rstarUK","rstarJAP","rstarGER","rstarFRA","rstarITA","rstarCA")

time <- as.numeric(rep(seq(1,50),each=7))
rstar <- rep(strings,times=50)

v <- variance.decomposition$rstarUS*100

data <- data.frame(time,v)
data <- data.frame(time, percent=as.vector(t(data[-1])), rstar)

percent <- as.numeric(data$percent)

plot.us <- ggplot(data, aes(x=time, y=percent, fill=rstar)) + geom_area()

plot.us

My data is already in percentages, they are FEVD - but every time I run my code I keep getting lines instead of the shaded area FEVD.

I am essentially trying to get a stacked area percentage chart


Solution

  • Perhaps the issue is with variance.decomposition$rstarUS; if you change the value of v to something else it appears to run as expected:

    library(tidyverse)
    strings <- cbind("rstarUS","rstarUK","rstarJAP","rstarGER","rstarFRA","rstarITA","rstarCA")
    
    time <- as.numeric(rep(seq(1,50),each=7))
    rstar <- rep(strings,times=50)
    
    v <- rpois(350, 10)
    
    data <- data.frame(time,v)
    data <- data.frame(time, percent=as.vector(t(data[-1])), rstar)
    
    percent <- as.numeric(data$percent)
    
    plot.us <- ggplot(data, aes(x=time, y=percent, fill=rstar)) + 
      geom_area()
    
    plot.us
    

    Created on 2022-07-15 by the reprex package (v2.0.1)

    What is variance.decomposition$rstarUS and are you sure you have calculated it properly?