Skip to content

Instantly share code, notes, and snippets.

@selva86
Created July 20, 2016 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save selva86/d9e8526280da41e457118f1e6ec5bc60 to your computer and use it in GitHub Desktop.
Save selva86/d9e8526280da41e457118f1e6ec5bc60 to your computer and use it in GitHub Desktop.
area_plot_in_base_graphics.R
# How to fill area under the line in base graphics
library(xts)
library(data.table)
library(lubridate)
set.seed(100)
date_seq <- seq.POSIXt(from=ymd("2016-01-01", tz="UTC"), length=100, by = "day")
y <- round(runif(100), 2)
df <- data.table(date=date_seq, y)
head(df)
# Convert to xts obj, so X-axis has dates.
df_xts <- as.xts(df)
# Get X and Y
x <- df$date
y <- as.numeric(df_xts$y)
# Plot it
plot(df_xts$y, cex.axis=0.4, main="Area plots with polygon()")
polygon(c(min(x), x, max(x)), c(0, y, 0), col=rgb(0, 1, 0, 0.7))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment