The objective of this is to display visualizations using plotly and highcharter.

Ex.1

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

data('mpg', package = "ggplot2")
wf <- ggplot(mpg, 
              aes(x = cty, y = hwy, color = class ))+
  geom_point(aes(size = class, frame = class))+
  #scale_x_continuous(labels = c(10, 15, 20, 25, 30, 35),
                #breaks = c(10, 15, 20, 25, 30, 35))+
  labs( x = 'cty',
        y = 'hwy')+
  theme(legend.position='none')
ggplotly(wf)
## Warning: Using size for a discrete variable is not advised.
## Warning in p$x$data[firstFrame] <- p$x$frames[[1]]$data: number of items to
## replace is not a multiple of replacement length

Ex.2

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

data(economics, package = "ggplot2")
unemployment <- economics %>%
  plot_ly(x = ~ date,
          y = ~ unemploy,
          title = 'Unemployment peaked after the financial crisis',
          type = 'scatter',
          mode = 'lines',
          hoverinfo = "enemploy") %>%
  layout(
    xaxis = list(
    zeroline = F,
    title = 'Date'),
    yaxis = list(
      title = 'Unemployment in \'000',
      zeroline = F))
unemployment

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.

Ex.3

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.

tf <- hchart(d1, 'point',hcaes(x=carat,y=price,group = clarity)) %>%
  hc_yAxis(title = list(text = "Price of Diamonds")) %>%
  hc_title(text = "Variation in Diamond Prices Increases With Carat")
wh <- hc_theme_flat()
tf%>%
  hc_add_theme(wh)

Ex.4

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

 q4 <- hchart(economics, 'line',hcaes(x=date,y=unemploy)) %>%
  hc_yAxis(title = list(text = "Unemployment in \'000")) %>%
  hc_title(text = "Unemployment peaked after the financial crisis")
emp5 <- hc_theme_chalk()
q4%>%
  hc_add_theme(emp5)