<- mpg |> filter(!class %in% c("subcompact", "minivan", "compact"))
MPG1
= ggplot(MPG1, aes(x=cty, y=hwy, color = class)) +
Q1 geom_point(aes(frame = class)) +
labs(x = "cty" , y = "hwy", color = "class") +
scale_x_continuous(breaks = seq(10, 35, 5), limits = c(10,35)) +
scale_y_continuous(breaks = seq(20, 40, 10), limits = c(10,40)) +
theme_minimal()
print(Q1)
Homework 3
DA 6233
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.
<- ggplotly(Q1)
Q1_interactive
Q1_interactive
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.
<- filter(mpg, cyl != 5) |>
mpg2 filter(class != '2seater') |>
group_by(class, cyl) |>
summarize(avghwy = round(mean(hwy), 2),
.groups = "drop")
<- hchart(mpg2,
Q2 "heatmap",
hcaes(x = as.character(class),
y = as.character(cyl),
value = avghwy)) |> hc_add_theme(hc_theme_538()) |>
hc_colorAxis(
stops = color_stops(colors = rev(c("#000004FF",
"#56106EFF",
"#BB3754FF",
"#F98C0AFF",
"#FCFFA4FF")))
)
Q2
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.
= Q2 |>
Q3 hc_tooltip(pointFormat = "Highway Mileage<br/>For class {point.class} with {point.cyl} cylinders: {point.avghwy} mpg") |>
hc_title(text = '<span style="font-family: helvetica, inter, sans-serif;"><strong><span style="color: #333333;">Highway Mileage Decreases across all the </span></strong></span> <span>🚗 🚙 🚐 🛻</span> as the Number of Cylinders Increases',
useHTML = TRUE, align = "center")
Q3
Q4 (3 points)
For this example, use a randomly selected subset of diamonds
data set from ggplot2
:
set.seed(2020)
= diamonds[sample(nrow(diamonds), 1000),] d1
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.
#|echo: true
#| fig-width: 9
#| fig-height: 6
set.seed(2020)
= diamonds[sample(nrow(diamonds), 1000),] |>
d1 hchart("scatter", hcaes(x = carat, y = price, group = clarity)) |>
hc_add_theme(hc_theme_flat()) |>
hc_yAxis(title = list(text = "Price of 💎")) |>
hc_xAxis(tickInterval = 0.5, title = list(text = "Weight of 💎 in Carats")) |>
hc_title(text = "Variation in Prices for 💎 Increases with Carats")
d1
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.
<- hchart(economics,
Econ_chart "line",
hcaes(x = date,
y = unemploy))
= Econ_chart|>
Q5 hc_tooltip(useHtML = TRUE, pointFormat = "Unemployment:<b>{point.y}<b>")|>
hc_title(text = "Unemployement Peaked after the Financial Crisis", align = "center") |>
hc_xAxis(title = list(text = "Date")) |>
hc_yAxis(title = list(text = "Unemployment in '000")) |>
hc_add_theme(hc_theme_economist())
Q5
#| fig-width: 9
#| fig-height: 6
#Econ_chart <- hchart(economics,
# "line",
# hcaes(x = date,
# y = unemploy))
#Q5 = Econ_chart|>
# hc_tooltip(useHtML = TRUE, pointFormat = "Unemployment:<b>{point.y}<b>")|>
# hc_title(text = "Unemployement Peaked after the Financial Crisis", align = "center") |> |>
# hc_add_theme(hc_theme_economist()) |>
#hc_xAxis(title = list(text = "Date")) |>
#hc_yAxis(title = list(text = "Unemployment in '000")) |>
#hc_tooltip(
# formatter = JS("function() {
# if (date == 2009-11-1) {
# return {point.x} + "'Unemployment:<b>{point.y}<b>'" +
# "'Note: Mortgage Crisis'"}
#else {
# return {point.x} + Unemployment:<b>{point.y}<b>"
# "))
#Q5
```
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.