-1

I'm trying to generate a stacked line/area graph utilizing the ggplot and geom_area functions. I have my data loaded into R correctly from what I can tell. Every time I generate the plot, the graph is empty (even though the axis looks correct except for the months being organized in alpha).

I've tried utilizing the data.frame function to define my variables but was unable to generate my plot. I've also looked around Stack Overflow and other websites, but no one seems to have the issue of no errors but still an empty plot.

Here's my data set:

enter image description here

Here's the code I'm using currently:

ggplot(OHV, aes(x=Month)) + 
  geom_area(aes(y=A+B+Unknown, fill="A")) + 
  geom_area(aes(y=B, fill="B")) + 
  geom_area(aes(y=Unknown, fill="Unknown"))

Here's the output at the end:

enter image description here

I have zero error messages, simply just no data being plotted on my graph.

M--
  • 25,431
  • 8
  • 61
  • 93
  • The canonical way to work with ggplot is to first reshape your data so that each dimension you want to map to an aesthetic (e.g. color of area chart) exists in its own variable. Try `library(tidyr); OHV %>% gather(col, val, A:Unknown) %>% ggplot(aes(x = Month, y = val, color = col)) + geom_area()` – Jon Spring May 29 '19 at 21:00
  • 2
    Hi there and welcome to SO! Thanks for the effort to put together a good question. The main tip to help you get better answers is that providing data using `dput()` will help others evaluate where issue you're having is coming from. You can rename columns, make up similar numbers, etc. to anonymize your data. Screen shots of data are much less useful. it will also help to get tips on restructuring your data, as @JonSpring points out. – phalteman May 29 '19 at 21:03
  • Another hint here is that in your figure, it's clear that your `Month`s are factors, and in the wrong order (you'll need to specify the correct order using `factor()`. However, `geom_area` works best with continuous variables, so you'll need to figure out how to express those months as continuous rather than discrete. – phalteman May 29 '19 at 23:17

1 Answers1

0

Your dates are being interpreted as a factor. You must transform them.

ibrary(tidyverse)
set.seed(1)
df <- data.frame(Month = seq(lubridate::ymd('2018-01-01'),
                             lubridate::ymd('2018-12-01'), by = '1 month'),
                 Unknow = sample(17, replace = T, size = 12), 
                 V1 = floor(runif(12, min = 35, max = 127)),
                 V2 = floor(runif(12, min = 75, max = 275)))

df <- df %>% 
  dplyr::mutate(Month = format(Month, '%b')) %>% 
  tidyr::gather(key = "Variable", value = "Value", -Month)

ggplot2::ggplot(df) +
  geom_area(aes(x = Month, y = Value, fill = Variable), 
            position = 'stack')

enter image description here

Note that I used tidyr::gather to be able to stack the areas in an easier way.

Now assuming your year of analysis is 2018, you need to transform the date of your data frame to something continuous, in the interpretation of r.

df2 <- df %>% 
  dplyr::mutate(Month = paste0("2018-", Month, "-01"),
                Month = lubridate::parse_date_time(Month,"y-b-d"),
                Month = as.Date(Month))

library(scales)
ggplot2::ggplot(df2) +
  geom_area(aes(x = Month, y = Value, fill = Variable), 
            position = 'stack') +
  scale_x_date(labels = scales::date_format("%b"))

enter image description here

bbiasi
  • 1,549
  • 2
  • 15
  • 31