Highcharts for R users – Highcharts Blog (2024)

Welcome to the first in a series of posts aimed at users of the R programming language who wish to become more familiar with Highcharts and the R package highcharter. If you love doing data science with R and creating interactive data visualizations, these posts are for you.

The content for the first couple of posts is adapted from my book Reproducible Finance with R but these posts will delve deeper into highcharter while the book also covers a broader range of topics like data wrangling, transforming, modeling and building Shiny applications.

Today, we will visualize asset returns with line charts, column charts and scatter plots. Next time we will move to some more complex work by adding regression lines and simulation results. Future posts will visualize macroeconomic data like the BLS jobs reports and GDP numbers.

Let’s get to it!

For our data, we will work with 5 years of monthly returns for the 5 ETFs below.

+ SPY (S&P500 fund)+ EFA (a non-US equities fund)+ IJS (a small-cap value fund)+ EEM (an emerging-mkts fund)+ AGG (a bond fund)

And we will need the following packages.

library(tidyverse)library(timetk)library(kableExtra)library(highcharter)

Since this isn’t a post about wrangling and transforming data with R, I won’t go through the logic of how to import the price data and transform to monthly returns (for the curious see this post), but the full code to do so is here.

symbols <- c("SPY","EFA", "IJS", "EEM","AGG")prices <- getSymbols(symbols, src = 'yahoo', from = "2013-01-01", to = "2017-12-31", auto.assign = TRUE, warnings = FALSE) %>% map(~Ad(get(.))) %>% reduce(merge) %>% `colnames<-`(symbols)prices_monthly <- to.monthly(prices, indexAt = "last", OHLC = FALSE)asset_returns_xts <- na.omit(Return.calculate(prices_monthly, method = "log"))asset_returns_xts <- asset_returns_xts * 100 asset_returns_long <- prices %>% to.monthly(indexAt = "last", OHLC = FALSE) %>% tk_tbl(preserve_index = TRUE, rename_index = "date") %>% gather(asset, returns, -date) %>% group_by(asset) %>% mutate(returns = (log(returns) - log(lag(returns))) *100 ) %>% na.omit()

We will be working with the two data objects that hold our monthly asset returns. The first is called asset_returns_xts. Have a look at the first few rows.

 SPY EFA IJS EEM AGG2013-02-28 1.267821 -1.296938 1.6175381 -2.310525 0.589115562013-03-28 3.726766 1.296938 4.0257940 -1.023505 0.098497722013-04-30 1.903006 4.896773 0.1222544 1.208504 0.963896782013-05-31 2.333571 -3.065563 4.1976371 -4.948359 -2.021365792013-06-28 -1.343432 -2.715331 -0.1402974 -5.473912 -1.577865342013-07-31 5.038578 5.186029 6.3541287 1.315986 0.26879677

There is one column for each of our ETF monthly returns, but notice the date is in a nameless column. In fact, the date is not in a column at all, it’s considered the time based index of this matrix.

Compare that to asset_returns_long.

dateasset returns
2013-02-28 SPY 1.267821
2013-03-28 SPY 3.726766
2013-04-30 SPY 1.903007
2013-05-31 SPY 2.333571
2013-06-28 SPY-1.343432
2013-07-31 SPY 5.038578

This object has a column called date, a column called asset and a column called returns. It’s considered a long data object because the ETFs are stacked in rows, whereas asset_returns_xts is considered a wide data object because each ETF has its own column and makes the object wider. asset_returns_long and asset_returns_xts hold the exact same information, but in different formats. We will examine how to visualize both with highcharter because both are popular data formats in the financial world.

Let’s start with asset_returns_xts and use the Highstock part of the Highcharts toolchain. To call this functionality from R, we start with highchart(type = "stock") and then supply the data to be charted. Note we specify type = “line” but that’s not necessary because the default is a line chart.

highchart(type = "stock") %>% hc_add_series(asset_returns_xts$SPY, type = "line")

Two short lines of code and a pretty nice interactive time series! Have a look at the chart and notice it has a navigator on the bottom and a date range selector at the top right. We did not have to code those, highchart(type = "stock") creates them by default as a very nice feature for our end users.

If we prefer a different color, we can change it with color = "green".

highchart(type = "stock") %>% hc_add_series(asset_returns_xts$SPY, type = "line", color = "green")

What if we want a column chart instead of a line chart? It’s the same code flow, except we specify type = "column".

highchart(type = "stock") %>% hc_add_series(asset_returns_xts$SPY, type = "column")

Hover on both of those charts and notice how the tooltip automatically pulls up the date from the x-axis.

For a scatter plot, we use the same code flow, changing to type = "scatter", but let’s also add in the returns for the EFA ETF by appending hc_add_series(asset_returns_xts$EFA). We could do this for all 5 of our ETF, adding them line by line, to put all 5 on the same scatter plot.

highchart(type = "stock") %>% hc_add_series(asset_returns_xts$SPY, type = "scatter") %>% hc_add_series(asset_returns_xts$EFA, type = "scatter")
NA

That chart looks how we want, but mouse over a point and notice a few suboptimal aesthetic features. First, the tooltip is not displaying the date the same that it appears on the x-ais. Second, it is displaying the full y-axis values out to more than 10 decimal places and, third, it is referring to the two ETFs as series 1 and series 2, because we have not supplied a name. Let’s fix these three issues.

First, the names of the series are easy to add with name = "SPY" and name = "EFA".

highchart(type = "stock") %>% hc_add_series(asset_returns_xts$SPY, type = "scatter", name = "SPY") %>% hc_add_series(asset_returns_xts$EFA, type = "scatter", name = "EFA")

Next, we modify the tooltip format by adding hc_tooltip(pointFormat = ) to the flow and changing the format of the x-point display with {point.x:%Y-%m-%d}. This is telling the tooltip to use a date format.

We then change the y-point display with {point.y:.4f}%, which is telling the tooltip to round to four decimal places and then put a % sign after it. Try changing this to {point.y:.8f} and see the results.

highchart(type = "stock") %>% hc_add_series(asset_returns_xts$SPY, type = "scatter", name = "SPY") %>% hc_add_series(asset_returns_xts$EFA, type = "scatter", name = "EFA") %>% hc_tooltip(pointFormat = '{point.x: %Y-%m-%d} 
{point.y:.4f}%')

I like the default colors chosen by highcharter but we can customize them with the color argument.

highchart(type = "stock") %>% hc_add_series(asset_returns_xts$SPY, type = "scatter", name = "SPY", color = "lightblue") %>% hc_add_series(asset_returns_xts$EFA, type = "scatter", name = "EFA", color = "pink") %>% hc_tooltip(pointFormat = '{point.x: %Y-%m-%d} 
{point.y:.4f}% ')

That’s all for data held in asset_returns_xts, let’s explore how to build these same visualizations from long formatted data.

Recall the structure of asset_returns_long.

dateasset returns
2013-02-28 SPY 1.267821
2013-03-28 SPY 3.726766
2013-04-30 SPY 1.903007
2013-05-31 SPY 2.333571
2013-06-28 SPY-1.343432
2013-07-31 SPY 5.038578

If we wish to create a line chart of the returns for SPY, we cannot select the SPY column because there is no column called SPY. We need to filter for when the asset column equals SPY by calling filter(asset == "SPY").

asset_returns_long %>% filter(asset == "SPY") %>% head()
date <date>asset <chr> returns <dbl>
2013-02-28 SPY 1.267821
2013-03-28 SPY 3.726766
2013-04-30 SPY 1.903007
2013-05-31 SPY 2.333571
2013-06-28 SPY-1.343432
2013-07-31 SPY 5.038578

Now we pass that data to hchart() using the %>% operator, which passes data from function to function.

When we run asset_returns_long %>% filter(asset == "SPY"), we are filtering to the SPY observations and then passing them to hchart() with hchart(.). That . inside the parentheses is telling hchart to use the filtered data. This might seem a bit verbose but with this flow, we can quickly filter on different conditions and then pass the result to hchart(), which can come in quite handy if we wish to filter on something like date instead of asset (as we will later).

We are not done yet, though, because we need to specify the type of chart again with type = “line” and then map our aesthetics with hcaes(x = date, y = returns). Why do we need to call hcaes here but we did not need to do so previously? It all goes back to the data structure. asset_returns_xts has no date column, the dates are in the index and highcharter defaults to using that index. asset_returns_long has a date column, but highcharter has no way of knowing we want that date column on the x-axis unless we explicitly tell it so.

asset_returns_long %>% filter(asset == "SPY") %>% hchart(., type = "line", hcaes(x = date, y = returns))

Notice a few differences between this chart and the previous line chart. First, there is no navigator or date selector here. We lost that when we took away highchart(type = "stock"). Also, the y-axis defaulted to being on the left-hand side. I like that better – maybe you hate it! It can be changed back to the right side with hc_yAxis(opposite = TRUE). For good measure, let’s add a % label to the y-axis as well, by calling labels = list(format = “{value}%”)

asset_returns_long %>% filter(asset == "SPY") %>% hchart(., type = "line", hcaes(x = date, y = returns)) %>% hc_yAxis(opposite = TRUE, labels = list(format = "{value}%"))

We can use the exact same code flow to create a column chart, by changing to type = "column". I also want the name of the data to appear in the tooltip, which can be done with name = "SPY".

asset_returns_long %>% filter(asset == "SPY") %>% hchart(., type = "column", hcaes(x = date, y = returns), name = "SPY") %>% hc_yAxis(opposite = TRUE, labels = list(format = "{value}%"))

Finally, let’s create a scatter plot from our tidy, long formatted data. We change to type = "scatter" and again need to specify our tooltip format with hc_tooltip(pointFormat = '{point.x:%Y-%m-%d}...).

asset_returns_long %>% filter(asset == "SPY") %>% hchart(., type = "scatter", hcaes(x = date, y = returns), name = "SPY") %>% hc_yAxis(opposite = TRUE, labels = list(format = "{value}%")) %>% hc_tooltip(pointFormat = '{point.x:%Y-%m-%d} 
{point.y: .4f}%')

Alright, that was quite a bit of work to build the same charts twice. Let’s do something different and examine how our tidy, long data can be used to efficiently create more interesting charts.

Suppose we wish to scatter all of our ETF data on one charty and color by group. We could go back to original flow and use hc_add_series for each ETF but that would get a bit tedious. With our tidy data flow, we can delete the filter(asset == "SPY") and then add hcaes(...group = asset). This tells highcharter to color each ETF differently, according to its group. Notice how it also creates a legend and includes the ETF name in the tooltip.

asset_returns_long %>% hchart(., type = "scatter", hcaes(x = date, y = returns, group = asset)) %>% hc_yAxis(opposite = TRUE, labels = list(format = "{value}%")) %>% hc_tooltip(pointFormat = '{point.x:%Y-%m-%d} 
{point.y: .4f}%')

We can quickly see that the lowest monthly return was EEM in August of 2015.

It might be nice to create a column chart, but only for the monthly returns in 2017. Here is where the flexibility of filtering helps. We can call filter(date >= "2017-01-01" & date <= "2018-01-01") to isolate the months in 2017, and then build our column chart.

asset_returns_long %>% filter(date >= "2017-01-01" & date < "2018-01-01") %>% hchart(., type = "column", hcaes(x = date, y = returns, group = asset)) %>% hc_yAxis(opposite = FALSE, labels = list(format = "{value}%")) %>% hc_tooltip(pointFormat = '{point.x: %Y-%m-%d} 
{point.y:.4f}% ')

Notice how the returns are naturally grouped together by date in the column format, which helps with comparing each ETF by month. IJS looks to have been the most volatile in 2017 and this looks like a good candidate for a Shiny application where an end user can choose a custom date period and set of ETFs to chart.

That’s all for today. Next time we will explore visualizing the Capital Asset Pricing Model (which is a linear regeression) and the results of a Monte Carlo simulation. See you then and thanks for reading!

Highcharts for R users – Highcharts Blog (2024)

FAQs

What is highchart in R? ›

Highcharter is a R wrapper for Highcharts javascript library and its modules. Highcharts is very flexible and customizable javascript charting library and it has a great and powerful API. The main features of highcharter are: Chart various R objects with one function: with hchart(x) you can chart data.

How many data points can Highcharts handle? ›

Re: High Charts Data Limit

By default it is set to 1000, you should increase this value in your chart.

What is HighSoft Highcharts? ›

Highcharts. WHAT IS HIGHCHARTS? Highcharts is a charting library written in pure JavaScript, offering an easy way of adding interactive charts to your web site or web application.

How do you set height and width on Highcharts? ›

Height and width is set either by setting a height and width of the container div, or by setting the chart. height and chart. width Highcharts options. The plot area is the square area within the axes, where the series are drawn.

What are the differences between ggplot2 vs Highcharts? ›

On the other hand, ggplot2 focuses more on static visuals and may not have the same level of interactivity and animation capabilities. Integration with Web Applications: Highcharts is built specifically for JavaScript and can be easily integrated into web applications, making it a preferred choice for web developers.

How do you redraw a Highchart? ›

ChartRedrawCallbackFunction. Fires when the chart is redrawn, either after a call to chart. redraw() or after an axis, series or point is modified with the redraw option set to true .

How many data points is enough? ›

You need at least thirty data points to get a statistically significant result. One data point tells you nothing. In a business decision, the monetary value of data is more important than its statistical significance.

How do I scrape data from Highcharts? ›

The Part Where We Scrape
  1. You'll call the target site.
  2. Wait until HighCharts plot appears.
  3. Parse data with linked JS functions.
  4. Store it to dataframe and save it to a local folder.
Jan 6, 2022

What are the limitations of radar charts? ›

Data set size

Radar charts are helpful for small-to-moderate-sized multivariate data sets. Their primary weakness is that their effectiveness is limited to data sets with less than a few hundred points. After that, they tend to be overwhelming.

What is better than Highcharts? ›

The best overall Highcharts alternative is Chart. Js. Other similar apps like Highcharts are FusionCharts, amCharts, Google Chart Tools, and Infogram. Highcharts alternatives can be found in Data Visualization Libraries Software but may also be in Data Visualization Tools or Component Libraries Software.

Is Highcharts free or paid? ›

Highcharts is a software library for charting written in pure JavaScript, first released in 2009. The license is proprietary. It is free for personal/non-commercial uses and paid for commercial applications.

Should I use Highcharts? ›

Overall, users have had positive experiences with Highcharts, finding it quick, easy to use, and saving them time in data visualization tasks. Additionally, Highcharts is valued for its ability to create interactive dashboards and visualize data from databases.

How to remove highcharts watermark? ›

If your problem is just the watermark and the chart is working for you, you can just remove the watermark from the chart by adding: "credits: { enabled: false }" to the configuration that you already have.

What is styled mode in Highcharts? ›

When in styled mode, no presentational attributes or CSS are applied to the chart SVG. Instead, CSS rules are required to style the chart. The default style sheet is available from https://code.highcharts.com/css/highcharts.css .

What is reflow in highcharts? ›

reflow: boolean

Whether to reflow the chart to fit the width of the container div on resizing the window. Defaults to true .

What is the use of Highcharts? ›

Highcharts is a pure JavaScript based charting library meant to enhance web applications by adding interactive charting capability. Highcharts provides a wide variety of charts. For example, line charts, spline charts, area charts, bar charts, pie charts and so on.

What is the difference between Plotly and Highcharts? ›

In summary, Highcharts is suitable for developers who seek a wide range of chart types and integration options with older browsers, while Plotly focuses on interactivity, advanced features, and integration with scientific computing languages.

What is the difference between Apache ECharts and Highcharts? ›

Highcharts has a commercial license for most use cases, although it offers a free version for non-commercial projects. ECharts, on the other hand, is released under the Apache License 2.0, which allows for commercial use and offers greater flexibility for certain scenarios.

What is the difference between chart JS and Highchart JS? ›

In summary, Chart. js is a lightweight, open-source library with limited features, while Highcharts offers a wider range of advanced features, better performance, and extensive documentation and support. I've used Highcharts with both Angular Js Reactive applications (render as ReactJs) and also a bit of D3.

Top Articles
6 Sustainable Supply Chain Practices: Examples, Definition & More
12 Project Management Methodologies in The IT Field
Swimgs Yuzzle Wuzzle Yups Wits Sadie Plant Tune 3 Tabs Winnie The Pooh Halloween Bob The Builder Christmas Autumns Cow Dog Pig Tim Cook’s Birthday Buff Work It Out Wombats Pineview Playtime Chronicles Day Of The Dead The Alpha Baa Baa Twinkle
Fredatmcd.read.inkling.com
Jailbase Orlando
Category: Star Wars: Galaxy of Heroes | EA Forums
What is IXL and How Does it Work?
Erskine Plus Portal
Does Pappadeaux Pay Weekly
Jet Ski Rental Conneaut Lake Pa
Rapv Springfield Ma
Dallas’ 10 Best Dressed Women Turn Out for Crystal Charity Ball Event at Neiman Marcus
Magicseaweed Capitola
Hoe kom ik bij mijn medische gegevens van de huisarts? - HKN Huisartsen
Moonshiner Tyler Wood Net Worth
Tnt Forum Activeboard
Navy Female Prt Standards 30 34
Spergo Net Worth 2022
Costco Gas Foster City
Nail Salon Goodman Plaza
north jersey garage & moving sales - craigslist
Atdhe Net
What Are The Symptoms Of A Bad Solenoid Pack E4od?
Reser Funeral Home Obituaries
Drift Hunters - Play Unblocked Game Online
JVID Rina sauce set1
Arlington Museum of Art to show shining, shimmering, splendid costumes from Disney Archives
Hwy 57 Nursery Michie Tn
130Nm In Ft Lbs
Busch Gardens Wait Times
Puffin Asmr Leak
Delta Rastrear Vuelo
Otis Offender Michigan
Police Academy Butler Tech
Craigslist Greencastle
Dr Adj Redist Cadv Prin Amex Charge
Crazy Balls 3D Racing . Online Games . BrightestGames.com
301 Priest Dr, KILLEEN, TX 76541 - HAR.com
Mid America Irish Dance Voy
Discover Things To Do In Lubbock
Nail Salon Open On Monday Near Me
Mbfs Com Login
ACTUALIZACIÓN #8.1.0 DE BATTLEFIELD 2042
Unitedhealthcare Community Plan Eye Doctors
Unveiling Gali_gool Leaks: Discoveries And Insights
Powerspec G512
Mitchell Kronish Obituary
Patricia And Aaron Toro
Windy Bee Favor
Sams Gas Price San Angelo
Qvc Com Blogs
Latest Posts
Article information

Author: Wyatt Volkman LLD

Last Updated:

Views: 6539

Rating: 4.6 / 5 (66 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Wyatt Volkman LLD

Birthday: 1992-02-16

Address: Suite 851 78549 Lubowitz Well, Wardside, TX 98080-8615

Phone: +67618977178100

Job: Manufacturing Director

Hobby: Running, Mountaineering, Inline skating, Writing, Baton twirling, Computer programming, Stone skipping

Introduction: My name is Wyatt Volkman LLD, I am a handsome, rich, comfortable, lively, zealous, graceful, gifted person who loves writing and wants to share my knowledge and understanding with you.