Line charts

By Data Tricks, 15 June 2018

Create data and set 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 line chart

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

Multiple line chart

#Multiple line chart
data2 <- dcast(data, Month ~ Category, value.var="Value")
gg <- ggplot(data2, aes(x=Month))
gg <- gg + geom_line(aes(y=data2$'1'), colour="#d1d1d1")
gg <- gg + geom_line(aes(y=data2$'2'), colour="#d1d1d1")
gg <- gg + geom_line(aes(y=data2$'3'))
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.