Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.