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

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.

p1 <- mpg |> ggplot(aes(x = cty, y = hwy)) +
  geom_point(aes(color = class, frame = class)) +
  theme(legend.position='none')

ggplotly(p1)

For the next four questions, you will use highcharter.

Q2 (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 essential for creating this plot.

I am using hc_theme_538(). Furthermore, the default color in the heamap is blue, which I changed using hc_colorAxis() function that I used in the Week 10 heatmap.

q2 <- mpg |>
  mutate(cyl = as.character(cyl)) |> 
  filter(cyl != '5' & class != '2seater') |> 
  select(hwy, class, cyl) |> 
  dplyr::group_by(class, cyl) |> 
  dplyr::summarise(hwy = round(mean(hwy),2))
## `summarise()` has grouped output by 'class'. You can override using the
## `.groups` argument.
p2 <- hchart(
    q2,
    'heatmap',
    hcaes(
      x = class,
      y = cyl,
      value = hwy)) |> 
  hc_colorAxis(
    stops = color_stops(colors = rev(c("#000004FF", 
                                   "#56106EFF", 
                                   "#BB3754FF", 
                                   "#F98C0AFF", 
                                   "#FCFFA4FF")))
    ) |> 
  hc_add_theme(hc_theme_538())
  
p2

Q3 (3 points)

In the above plot, the tooltip shows confusing information. Below, I modified the tooltip to present more information. The code is not at all complicated and relies on the tooltip code we used in Week 10.

Next, I removed the X axis title and modified Y axis title.

Finally, I added a title to the plot. Note how I used four different emojies related to cars. It doesn’t matter which car emojis you use as long as they are related to automobiles.

p3 <- p2 |> 
  hc_tooltip(
    headerFormat = "<span style=color:{point.color}>●</span> Highway Mileage<br>",
    pointFormat = "For class {point.class} with {point.cyl} cylinders: {point.value} mpg"
    ) |>
   hc_yAxis(
    title = list(text = "Number of Cylinders")
   ) |> 
  hc_xAxis(
    title = list(text = "")
   ) |> 
  hc_title(
    text = '<span style="font-family: helvetica, arial, sans-serif;"><strong>Highway Mileage Decreases across all the</span></strong></span> <span>&#128657 &#128661 &#128663 &#128668;</span> as the Number of Cylinders Increases',
           useHTML = TRUE, align = "left")
  


p3

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. Wherever the word diamond appeared in the plot, I replaced it with the diamond emoji.

Point colors in this graph are mapped to clarity. Check out all the variables in this data set by typing ?diamonds in the console.

p4 <- hchart(
  d1,
  "scatter",
  hcaes(
    x = carat,
    y = price,
    group = clarity
  )
) |> 
  hc_xAxis(
    title = list(text = "Weight of &#128142 in Carats",
                 useHTML = TRUE
                 )
    )|>
  hc_yAxis(
    title = list(text = "Price of &#128142",
                 useHTML = TRUE
                 )
    ) |>
  hc_title(
    text = "Variation of Prices for &#128142 Increases with Carats",
    useHTML = T
  ) |> 
  hc_add_theme(hc_theme_flat())
  

p4

Q5 (3 points)

Recreate the plot in Q2 using hchart(). I used hc_theme_economist(). You can use any theme you want. You can check out the themes here. I used economics dataset from ggplot2. Learn more about the variables in the dataset by typing ?economics in the console.

p5 <- mutate(economics, unemployThous = unemploy/1000) |> 
  hchart(
  type = "line",
  hcaes(
    x = date,
    y = unemployThous
  )
) |> 
  hc_yAxis(
    title = list(text = "Unemployment in '000"),
    labels = list(
      format = '{value}k'
      )
  ) |> 
  hc_xAxis(
    title = list(text = "Date")
    ) |>
  hc_title(
    text = "Unemployment Peaked after the Financial Crisis"
  ) |> 
  hc_tooltip(
    headerFormat = '{point.key}<br>',
    pointFormat = 'Unemployment: <b>{point.unemploy:.,2f}</b>'
  ) |> 
  hc_add_theme(hc_theme_economist())


p5