How to draw multiple time series in the same plot in the R programming language. More details: [ Ссылка ]
R code of this video:
set.seed(1023172) # Create random example data
data <- round(data.frame(year = 2001:2025,
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)
install.packages("reshape2") # Install reshape2 package
library("reshape2") # Load reshape2 package
data_long <- melt(data, id.vars = "year") # Reshaping data to long format
install.packages("ggplot2") # Install ggplot2 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:
Facebook: [ Ссылка ]
LinkedIn: [ Ссылка ]
Patreon: [ Ссылка ]
Pinterest: [ Ссылка ]
Reddit: [ Ссылка ]
Twitter: [ Ссылка ]
Ещё видео!