Draw Multiple Time Series in Same Plot in R | Using Base R & ggplot2 | lines & geom_line Functions

preview_player
Показать описание
R code of this video:

ts1 = 1:25 + rnorm(25),
ts2 = 30:6 + runif(25, 0, 10),
ts3 = rnorm(25, 5, 5)))

plot(data$year, # Draw first time series
data$ts1,
type = "l",
col = 2,
ylim = c(- 15, 40),
xlab = "Year",
ylab = "Values")
lines(data$year, # Draw second time series
data$ts2,
type = "l",
col = 3)
lines(data$year, # Draw third time series
data$ts3,
type = "l",
col = 4)
legend("topright", # Add legend to plot
c("ts1", "ts2", "ts3"),
lty = 1,
col = 2:4)

library("reshape2") # Load reshape2 package

library("ggplot2") # Load ggplot2 package

ggplot(data_long, # Draw ggplot2 time series plot
aes(x = year,
y = value,
col = variable)) +
geom_line()

Follow me on Social Media:
Рекомендации по теме
Комментарии
Автор

Thank you much for making a series of videos of high quality. I am s student who is learning R recently, I find your channel is one of the most helpful channels as aspect of the R language. I learned a lot from your videos to deal with the real-life problem I encountered. Thank you!

shirleyxiao
Автор

I am currently trying to learn R, and this video was exactly what I needed. Thank you for all you do!

gabrielleannedeocampo
Автор

Dear Sir Joaquim, your tutorials are awesome, very informative and well detailed including at your web site.
Keep it up.

afonsoosorio
Автор

Thanks, super video - short and to the point!

markedwards
Автор

Thanks, what a great info, you have a new suscriber. Cheers ¡¡

bryanism
Автор

Thank you so much. what if the length of the two data was different?

alibayat