0

I'm having a problem with ggplot and geom_area in R. My intention is to produce a stacked area chart, with time in x-axis and p_gen values on the y-axis like this one (made with Excel with same data):

Example Stacked Area Chart:

img

I try to plot the figure using the code shown below:

ggplot(pcpcen, aes(x=datetime, y=p_gen, fill = type)) + geom_area()

Where pcpcen corresponds to this data frame (not all data included, but same structure)

week,datetime,type,p_gen,is_ernc
376,2025-12-13 11:00:00,BESS,0.1,1
376,2025-12-13 11:00:00,BIO,302.49999999999994,1
376,2025-12-13 11:00:00,BOMBEO,0.0,1
376,2025-12-13 11:00:00,CARBON,2830.7999999999997,1
376,2025-12-13 11:00:00,COG,117.00000000000001,1
376,2025-12-13 11:00:00,DIESEL,0.0,1
376,2025-12-13 11:00:00,DIESEL_CC,0.0,1
376,2025-12-13 11:00:00,EOL,528.7,1
376,2025-12-13 11:00:00,GEO,48.0,1
376,2025-12-13 11:00:00,GLP,0.0,1
376,2025-12-13 11:00:00,GNL_CA,250.5,1
376,2025-12-13 11:00:00,GNL_CC,658.0,1
376,2025-12-13 11:00:00,HIDRO,2274.399999999999,1
376,2025-12-13 11:00:00,HIDRO_EMB,2352.2,0
376,2025-12-13 11:00:00,HIDRO_MINI,24.400000000000002,0
376,2025-12-13 11:00:00,SOL_CSP,31.5,1
376,2025-12-13 11:00:00,SOL_FV,2155.5,1
347,2025-05-22 10:00:00,BESS,0.1,1
347,2025-05-22 10:00:00,BIO,390.29999999999995,1
347,2025-05-22 10:00:00,BOMBEO,0.0,1
347,2025-05-22 10:00:00,CARBON,3865.5999999999995,1
347,2025-05-22 10:00:00,COG,117.00000000000001,1
347,2025-05-22 10:00:00,DIESEL,0.0,1
347,2025-05-22 10:00:00,DIESEL_CC,0.0,1
347,2025-05-22 10:00:00,EOL,862.8000000000001,1
347,2025-05-22 10:00:00,GEO,0.0,1
347,2025-05-22 10:00:00,GLP,37.0,1
347,2025-05-22 10:00:00,GNL_CA,255.1,1
347,2025-05-22 10:00:00,GNL_CC,1775.6,1
347,2025-05-22 10:00:00,HIDRO,1763.4000000000003,1
347,2025-05-22 10:00:00,HIDRO_EMB,2119.8999999999996,0
347,2025-05-22 10:00:00,HIDRO_MINI,25.000000000000004,0
347,2025-05-22 10:00:00,SOL_CSP,31.5,1
347,2025-05-22 10:00:00,SOL_FV,1501.1999999999991,1

But after I run the command, the resulting plot is not what I was expecting

R output chart

If I change the x-axis to be the "week" column, the output is even stranger:

enter image description here

I'm using RStudio on a Windows 10 x64 machine, this is the output of version:

platform       x86_64-w64-mingw32          
arch           x86_64                      
os             mingw32                     
system         x86_64, mingw32             
status                                     
major          3                           
minor          5.1                         
year           2018                        
month          07                          
day            02                          
svn rev        74947                       
language       R                           
version.string R version 3.5.1 (2018-07-02)
nickname       Feather Spray
camille
  • 16,432
  • 18
  • 38
  • 60
  • It's difficult to say for sure without seeing your data in a [reproducible format](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), but my guess is it's a data types issue. You probably have dates encoded as factors, which then can't be split properly for a datetime axis – camille Nov 05 '18 at 22:07
  • Try reading your data in with a `stringsAsFactors` set to `FALSE`, like so `read.csv("pcpcen.csv", sep = ",", stringsAsFactors = FALSE)`. Doing like this it looks normal to me. – Anonymous coward Nov 05 '18 at 22:11

1 Answers1

0

Set datetime as date, and/or don't set things unnecessarily to factors when importing data.

library(ggplot2)
pcpcen <- read.csv("df.txt", sep = ",", stringsAsFactors = FALSE)
pcpcen$datetime <- as.Date(pcpcen$datetime, "%Y-%m-%d")
str(pcpcen)
ggplot(pcpcen) + geom_area(aes(x=datetime, y=p_gen, color = type, fill = type), position = "stack") + scale_x_date(labels = date_format("%Y-%m-%d"))
ggplot(pcpcen) + geom_area(aes(x=week, y=p_gen, fill = type))

Anonymous coward
  • 2,061
  • 1
  • 16
  • 29
  • That worked well. I had to use as_datetime() function to get hourly resolution, but I could solve the problem with the datetime objects. – E. SierrBaez Nov 06 '18 at 15:29