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:
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
If I change the x-axis to be the "week" column, the output is even stranger:
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