How to draw a time series in R using ggplot2? | Single, Multiple | StatswithR | Arnab Hazra

Опубликовано: 24 Июль 2026
на канале: StatswithR
712
21

Here we explain how to generate a presentation/publication-quality time series plot in R/R-studio using ggplot2. The codes for the steps explained in the video are as follows. Copy and paste them into R, run them one-by-one and try to understand what each argument is doing.

#datascience #datavisualization #visualization #ggplot2 #tidyverse #single #multiple #function #rstudio #rcoding #timeseries #temperature #India #Kolkata #Mumbai

Data link- https://academic.udayton.edu/kissock/...

The data is only for research and educational purposes. I do not own this data, just using for the illustration purposes, and please follow the guidelines before using. Make the world better.

#---------
rm(list = ls())

Kolkata = read.table("INCALCUT.txt")
Mumbai = read.table("INBOMBAY.txt")

head(Kolkata)

library(dplyr)
library(lubridate)
library(ggplot2)

replace "greater than" by mathematical symbol before use.

Kolkata.new = Kolkata %"greater than"% mutate(date = make_date(V3, V1, V2))
Mumbai.new = Mumbai %"greater than"% mutate(date = make_date(V3, V1, V2))

Kolkata.dates = Kolkata.new$date
Mumbai.dates = Mumbai.new$date

Kolkata.temp = (Kolkata.new$V4 - 32) * 5 / 9
Mumbai.temp = (Mumbai.new$V4 - 32) * 5 / 9

adjust missing values

Kolkata.temp[Kolkata.new$V4 == -99] = NA
Mumbai.temp[Mumbai.new$V4 == -99] = NA

rm(Kolkata, Mumbai, Kolkata.new, Mumbai.new)

library(ggplot2)

p = ggplot() + geom_line(aes(x = Kolkata.dates, y = Kolkata.temp), size = 1) +
xlab("Time") + ylab(expression(paste("Temperature (",degree, "C)", sep = ""))) +
ggtitle("Daily temperature in Kolkata, India (1995-2020)") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(axis.text=element_text(size=20),
axis.title=element_text(size=20),
plot.title = element_text(size=20))

ggsave(p, filename = "ggplot_kolkata.pdf", height = 6, width = 9)

p = ggplot() + geom_line(aes(x = Mumbai.dates, y = Mumbai.temp), size = 1) +
xlab("Time") + ylab(expression(paste("Temperature (",degree, "C)", sep = ""))) +
ggtitle("Daily temperature in Mumbai, India (1995-2020)") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(axis.text=element_text(size=20),
axis.title=element_text(size=20),
plot.title = element_text(size=20))

ggsave(p, filename = "ggplot_mumbai.pdf", height = 6, width = 9)

#--------------

Draw together

p = ggplot() +
geom_line(aes(x = Kolkata.dates, y = Kolkata.temp, color = "red"), size = 1) +
geom_line(aes(x = Mumbai.dates, y = Mumbai.temp, color = "blue"), size = 1) +
xlab("Time") + ylab(expression(paste("Temperature (",degree, "C)", sep = ""))) +
ggtitle("Daily temperature (1995-2020)") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(axis.text=element_text(size=20),
axis.title=element_text(size=20),
plot.title = element_text(size=20)) +
scale_color_identity(breaks = c("red", "blue"),
guide = "legend",
name = "City",
labels = c("Kolkata", "Mumbai")) +
theme(legend.text=element_text(size=20),
legend.title = element_text(size=20, hjust = 0.5),
legend.key.width = unit(1,"cm"))

ggsave(p, filename = "ggplot_joint.pdf", height = 6, width = 9)