Before you begin, note that, in the header, the output format of this document is html_notebook. When you save this file, it automatically creates another file with the same file name but with .nb.html extension in the same directory. This is the file you will submit as your homework solution along with the .Rmd file.

Warnings:

  1. Don’t delete the nb.html file.
  2. Don’t knit your .Rmd file to html. If you want to look at the output, just open the nb.html in the browser. Alternatively, click on the “Preview” button on top of the document:

If you delete nb.html file, you may have to create a new .Rmd file and restart from there. If you knit your .Rmd file to html, you will not be able to retain any of the interactivity in the plots. This means the TAs will not be able to grade you!

The objective of this homework is to give you more practice on interactive visualizations using plotly and highcharter.

As always recreate these visualizations exactly. Q1 and Q2 use plotly while Q3-Q5 use highcharter.

Note: The output width of the plotly plots is set to 100% but they still don’t fill up the full width of the HTML document. Don’t worry about it.

Q1 (3 points)

Use mpg data set from ggplot2 to create a static visualization and then use ggplotly() to create a limited interactive plot.

Hint: You will need to supply only frame. No ids used.

Q2 (3 points)

For this visualization you will use plot_ly() function. There is only one more function in the plot: layout(). This function allows you to set the title of the plot as well as axes titles. You will use it only for changing the titles and nothing else. To create this plot, we will use economics data set from ggplot2.

Hint: Download Plotly cheatsheet

As an aside, the Y axis in this line graph doesn’t start from 0, which is commonly frowned upon. There is usually no hard and fast rule about this but generally if you are just interested in the trend, truncating the Y axis is OK.

For the next three questions, you will use highcharter.

Q3 (3 points)

This example creates a heatmap similar to the one shown here.

Use mpg data and hchart() function. We want to create a heatmap of average highway mileage for different class and cyl. This plot removes all the observations with five cylinders or with 2seater class. Also note that I am treating cyl as a character (string) variable. This is essentially to create this plot.

## `summarise()` has grouped output by 'class'. You can override using the `.groups` argument.

Q4 (3 points)

For this example, use a randomly selected subset of diamonds data set from ggplot2:

set.seed(2020)
d1 <- diamonds[sample(nrow(diamonds), 1000),]

Next use d1 to create the following plot.

I have used hc_theme_flat() for this plot. Please use this theme for your plot too! You can add a theme to the plot using hc_add_theme() function.

Q5 (3 points)

Recreate the plot in Q2 using hchart(). I used hc_theme_chalk(). You can use any theme you want. You can check out the themes here.

Q6 (0 points)

Between plotly and highcharter, which package do you like more?

Hint: The correct answer is highcharter

Highcharter! More fun themes