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 uses plotly while Q2-Q5 use 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.
gg <-ggplot(mpg, aes(x = cty, y = hwy, color = class)) +geom_point(aes(frame = class)) +labs(x ='cty',y ='hwy') +theme(legend.position ='none')
Warning in geom_point(aes(frame = class)): Ignoring unknown aesthetics: frame
ggplotly(gg)
Warning in p$x$data[firstFrame] <- p$x$frames[[1]]$data: number of items to
replace is not a multiple of replacement length
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 heatmap is blue, which I changed using hc_colorAxis() function that I used in the Week 10 heatmap.
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.
mpg1 <-filter(mpg, cyl !=5) %>%filter(class !='2seater') %>%group_by(class, cyl) %>%summarize(avghwy =mean(hwy),.groups ="drop")q3 <-hchart(mpg1,"heatmap",hcaes(x = class,y =as.character(cyl),value = avghwy))q3 %>%hc_title(text ='<span style="font-family: helvetica, inter, sans-serif;"><strong><span style="color: #000000;">Highway Mileage Decreases across all the</span></span> <span>🚙 🚘 🚗 🛻 as the Number of Cylinders Increases</strong></span>',useHTML =TRUE, align ="center") %>%hc_colorAxis(stops =color_stops(colors =rev(c("#000004FF", "#56106EFF", "#BB3754FF", "#F98C0AFF", "#FCFFA4FF"))))
Q4 (3 points)
For this example, use a randomly selected subset of diamonds data set from ggplot2:
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.
diam1 <-hchart(d1,"scatter",hcaes(x = carat,y = price,group = clarity))diam1 %>%hc_title(text ="Variation in Prices for <span> 💠 </span> Increases with Carats") %>%hc_xAxis(title =list(text ="Weight of Diamonds in Carats")) %>%hc_yAxis(title =list(text ="Price of Diamonds")) %>%hc_add_theme(hc_theme_flat())
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.
q5 <-hchart(economics, "line",hcaes(x = date,y = unemploy))q5 %>%hc_tooltip(pointFormat ="Unemployment:{point.y}") %>%hc_title(text ="Unemployement peaked after the financial crisis") %>%hc_xAxis(title =list(text ="Date")) %>%hc_yAxis(title =list(text ="Unemployment in '000'")) %>%hc_add_theme(hc_theme_darkunica())
Bonus plot (Not graded)
This is the same plot as above except if you hover mouse pointer over the peak of unemployment, the tooltip will show more information. Once again, this is a simple trick and doesn’t require any advanced coding.
bq <- economics %>%plot_ly(x =~ date,y =~ unemploy,hoverinfo ='date') %>%add_trace(y =~ unemploy, mode ='lines') %>%layout(title ='Unemployment peaked after the financial crisis',yaxis =list(title ="Unemployment in '000"),xaxis =list(title ='Date',showlegend = F))bq
No trace type specified:
Based on info supplied, a 'scatter' trace seems appropriate.
Read more about this trace type -> https://plotly.com/r/reference/#scatter