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.
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.
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.
gg <- mpg %>% ggplot( aes(cty, hwy)) +
geom_point(aes(color = class, frame = class)) +
theme(legend.position = "none")
ggplotly(gg)
For this visualization you will use plot_ly() function. There is only one more function in the plot: layout(). To create this plot, we will use economics data set from ggplot2.
Hint: Download Plotly cheatsheet
plot_ly(economics,
x = ~ date,
y = ~ unemploy,
type = 'scatter',
mode = 'lines'
) %>%
layout(title = "Unemployment peaked after the financial crisis",
xaxis = list(title = "Date"),
yaxis = list(title = "Unemployment in '000"))
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.
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.
mpgdf <- mpg %>%
filter(class != "2seater" & cyl != 5) %>%
group_by(class, cyl) %>%
summarize(avg_hwy = mean(hwy, na.rm = TRUE)) %>%
ungroup()
## `summarise()` regrouping output by 'class' (override with `.groups` argument)
hchart(mpgdf, "heatmap", hcaes(x = class, y = as.character(cyl), value = avg_hwy))
For this example, use a randomly selected subset of diamonds:
set.seed(2020)
d1 <- diamonds[sample(nrow(diamonds), 1000),]
Next use d1 to create the following plot. You have created this same plot in Homework 1 but with the full diamonds data set.
You can check out the themes here.
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.
hchart(d1, "scatter", hcaes(x = carat, y = price, group = clarity)) %>%
hc_add_theme(hc_theme_flat()) %>%
hc_xAxis(title = list(text="Weight of Diamonds in Carats")) %>%
hc_yAxis(title = list(text="Price of Diamonds")) %>%
hc_title(text = "Variation in Diamond Prices Increases with Carats")
Recreate the plot in Q2 using hchart(). I used hc_theme_chalk(). You can use any theme you want.
hchart(economics, "line", hcaes(x = date, y = unemploy), name = "Unemployment") %>%
hc_add_theme(hc_theme_chalk()) %>%
hc_xAxis(title = list(text="Date")) %>%
hc_yAxis(title = list(text="Unemployment in '000")) %>%
hc_title(text = "Unemployment peaked after the financial crisis")