Back to Browse

Draw Multiple Variables as Lines to Same ggplot2 Plot in R (2 Examples) | geom_line & reshape2 melt

17.1K views
Dec 23, 2020
5:06

How to plot multiple lines in the same ggplot2 graphic using the R programming language. More details: https://statisticsglobe.com/r-draw-multiple-lines-to-same-ggplot2-plot R code of this video: set.seed(6532465) # Create example data data <- data.frame(x = 1:50, y1 = sort(rnorm(50)), y2 = sort(rnorm(50, 0.5))) head(data) # Head of example data install.packages("ggplot2") # Install ggplot2 package library("ggplot2") # Load ggplot2 package ggp1 <- ggplot(data, aes(x)) + # Create ggplot2 plot geom_line(aes(y = y1, color = "red")) + geom_line(aes(y = y2, color = "blue")) ggp1 # Draw ggplot2 plot install.packages("reshape2") # Install & load reshape2 package library("reshape2") data_long <- melt(data, id = "x") # Convert data to long format head(data_long) # Head of long data ggp2 <- ggplot(data_long, # Create ggplot2 plot aes(x = x, y = value, color = variable)) + geom_line() ggp2 # Draw ggplot2 plot Follow me on Social Media: Facebook: https://www.facebook.com/statisticsglobecom/ Patreon: https://www.patreon.com/statisticsglobe Pinterest: https://www.pinterest.de/JoachimSchork Reddit: https://www.reddit.com/user/JoachimSchork Twitter: https://twitter.com/JoachimSchork

Download

0 formats

No download links available.

Draw Multiple Variables as Lines to Same ggplot2 Plot in R (2 Examples) | geom_line & reshape2 melt | NatokHD