Area charts

By Data Tricks, 15 June 2018

Create data and set-up theme

library(ggplot2)
library(reshape2)
library(dplyr)
set.seed(575)

data <- data.frame(Month = rep(1:100, each=1, times=3))
data$Add <- sample(-10:15, 300, replace=TRUE)
data$Category <- rep(1:3, each=100, time=1)

data <- data %>%
group_by(Category) %>%
mutate(Value=cumsum(Add))

theme <- theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line.x = element_line(color="#919191", size = 0.1),
axis.line.y = element_line(color="#919191", size = 0.1))

Basic area chart

#Area chart
gg <- ggplot(data[1:100,])
gg <- gg + geom_area(aes(x = Month, y = Value), stat = "identity")
gg <- gg + theme
print(gg)

Multiple area chart

#Multiple area chart
data$Category <- as.factor(data$Category)
gg <- ggplot(data, aes(x = Month, y = Value))
gg <- gg + geom_area(aes(fill=Category))
gg <- gg + theme
print(gg)

Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

Please note that your first comment on this site will be moderated, after which you will be able to comment freely.