Creating a basic time series plot

To create a basic time series plot using Highcharter, we need to first convert our data into a format that Highcharter can understand. We will use the xts package to convert our data into a time series object, and then use the hchart() function in Highcharter to create the plot:

R




library(highcharter)
library(datasets)
  
# Convert the nottem data to a time series object
ts_data <- ts(nottem, start = c(1920, 1), frequency = 12)
  
# Convert the time series object to a data frame
df <- data.frame(date = time(ts_data),
                 value = as.numeric(ts_data))
  
# Create a highcharter time series plot
hchart(df, "line", hcaes(x = date,
                         y = value)) %>% 
  hc_title(text = "Monthly Average Temperatures at Nottingham Castle") %>% 
  hc_yAxis(title = list(text = "Temperature (Fahrenheit)")) %>% 
  hc_xAxis(type = "datetime",
           labels = list(format = "%b %Y"))


Output:

Basic time series plot using highcharter library

How to Plot Timeseries using highcharter library in R

Time series data is a sequence of data points that are measured at regular intervals over time. Plotting time series data is a useful way to visualize trends and patterns over time. Highcharter is an R package that provides an interface to the Highcharts JavaScript charting library. Highcharts is a popular web application library for creating interactive charts and graphs. In this article, we will explore how to use Highcharter to plot time series data in R.

Before we can use Highcharter, we need to install it. We can do this using the install.packages() function in R Programming Language:

# Installing the required R packages
install.packages("highcharter")

We will use the AirPassengers dataset, which contains monthly totals of international airline passengers from January 1949 to December 1960. This dataset is included with the base R installation and can be loaded using the data() function:

R




data(AirPassengers)
force(AirPassengers)


Output:

     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1949 112 118 132 129 121 135 148 148 136 119 104 118
1950 115 126 141 135 125 149 170 170 158 133 114 140
1951 145 150 178 163 172 178 199 199 184 162 146 166
1952 171 180 193 181 183 218 230 242 209 191 172 194
1953 196 196 236 235 229 243 264 272 237 211 180 201
1954 204 188 235 227 234 264 302 293 259 229 203 229
1955 242 233 267 269 270 315 364 347 312 274 237 278
1956 284 277 317 313 318 374 413 405 355 306 271 306
1957 315 301 356 348 355 422 465 467 404 347 305 336
1958 340 318 362 348 363 435 491 505 404 359 310 337
1959 360 342 406 396 420 472 548 559 463 407 362 405
1960 417 391 419 461 472 535 622 606 508 461 390 432

Similar Reads

Creating a basic time series plot:

...

Adding plot options

To create a basic time series plot using Highcharter, we need to first convert our data into a format that Highcharter can understand. We will use the xts package to convert our data into a time series object, and then use the hchart() function in Highcharter to create the plot:...

Customizing Time Series Plot using highcharter

...

Contact Us