ggplot line graph example are provided to illustrate different format options.
Data
The Orange
data frame ships with R and depicts the growth of fruit trees as a function of age (days). The data object has 35 rows and 3 columns in long data format.
Basic Line Plot Syntax
1 2 3 4 5 6 |
library(ggplot2) # Plot skeleton p <- ggplot(Orange, aes(age, circumference, group=Tree)) # Plot1: Basic line chart of circumference vs. age p + geom_line() |
Line Plot Aesthetics
Line chart aesthetics control selected x and y data, color (by name), linetype and size, transparency or alpha level. Line symbols or points are added using geom_point().
1 2 3 4 5 6 7 8 9 10 11 |
# Plot2: Map color to Tree and add points ggplot(Orange, aes(age, circumference, group=Tree, color=Tree)) + geom_line() + geom_point() # Plot3: Map point shape to Trees; set point and line color ggplot(Orange, aes(age, circumference, group = Tree, color = Tree)) + geom_line(size=1.2) + geom_point(aes(shape=Tree), size = 2.5, fill = "white") + scale_shape_manual(values = c(22,21,22,23,24,25)) + scale_color_brewer(palette = 3) |
Layered Line Plots
It is common to layer line plots and geom_line() on top of other data plotted using another geom or plot top. In this case, it is essential when you add the new line data to be layered into the chart using the data = argument. This will avoid a common error: ggplot2 doesn’t know how to deal with data of class uneval. The following example layers point and line data while avoiding the common error.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# Functions to convert raw data into moving averages lag <- function(x, k){ return( c(rep(NA,k), x[1:(length(x) - k)]) ) } Mov.Avg <- function(x, k){ temp.df <- data.frame(x = x) for(i in 1:k){ temp.lag <- lag(x, i) temp.df[, i + 1] <- temp.lag } ma <- rowMeans(temp.df) return(ma) } # Raw data set.seed(100) x <- 1:100 y <- vector("numeric", 100) for(i in 1:100){ ifelse(i == 1, y[i] <- 10, y[i] <- y[i-1] + (rnorm(1, 0, 0.75) * i) + rnorm(1, 0, 10)) } test <- data.frame(x = x, y = y, MA5 = Mov.Avg(y, 5), MA10 = Mov.Avg(y, 10)) # Plot point and line data ggplot(test, aes(x, y)) + geom_point() + geom_line(data = test, aes(x, MA5, stat="identity"), color="red") + geom_line(data = test, aes(x, MA10, stat="identity"), color="green") + theme_gray() |
An Example: Fully Loaded
The following chart combines several plot layers and many finished aesthetics, and is followed by code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
ggplot(KISR_LCOE_All, aes(x = year, y = LCOE), group = scenario) + geom_line(aes(color=factor(scenario)), size = 1.5) + geom_point(color="grey25", size = 4.5, show_guide = TRUE) + geom_point(colour="white", size = 4, show_guide = TRUE) + geom_point(aes(shape = factor(scenario))) + scale_color_manual(values = c("#8856A7", "#B3CDE3", "#FEC44F")) + scale_y_continuous(limits = c(128, 166), breaks = seq(126, 166, by = 5)) + scale_x_continuous(limits = c(2013, 2030), breaks = seq(2013, 2030, by = 1)) + labs(title = expression(atop(bold("Kuwait LCOE"), atop(italic("Total Generation Mix: Scenario By Year"),""))), x = "Production Year", y = "LCOE ($/MWh)") + theme(plot.title = element_text(size = 25), axis.title.y = element_text(size = 16, vjust = 0.25, face = "bold"), axis.text.y = element_text(size = 13, color = "grey15"), axis.title.x = element_text(size = 16, vjust = -0.25, face = "bold"), axis.text.x = element_text(size = 13, color = "grey15", angle = -90, vjust = 0.5), legend.title = element_blank(), legend.text = element_text(size = 12), panel.border = element_rect(fill = NA, colour = "grey25")) |