For this homework assignment, I will need tidyverse to clean up the dataset, RColorBrewer to select a color scheme for my plot, highcharter to create my plot, and dslabs to obtain the dataset.
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.2 ✓ purrr 0.3.4
## ✓ tibble 3.0.4 ✓ dplyr 1.0.2
## ✓ tidyr 1.1.2 ✓ stringr 1.4.0
## ✓ readr 1.4.0 ✓ forcats 0.5.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(RColorBrewer)
library(highcharter)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(dslabs)
##
## Attaching package: 'dslabs'
## The following object is masked from 'package:highcharter':
##
## stars
I selected the stars dataset to visualize. I wanted to order the star types as factors based on their classification in order of increasing temperature. I also removed rows with missing data and used a summary to make sure there was a complete dataset. I saved this dataset as a CSV file to make sharing with others easier.
data("stars")
stars$type <- factor(stars$type, levels = c("M", "K", "G", "F", "A", "B", "O"))
stars <- drop_na(stars)
summary(stars)
## star magnitude temp type
## Altair : 2 Min. :-8.000 Min. : 2500 M:32
## *40EridaniA : 1 1st Qu.:-2.150 1st Qu.: 3038 K:16
## *40EridaniC : 1 Median : 2.100 Median : 4900 G: 4
## *61CygniA : 1 Mean : 3.904 Mean : 8617 F: 7
## *61CygniB : 1 3rd Qu.:11.050 3rd Qu.: 9900 A:13
## *70OphiuchiA: 1 Max. :17.000 Max. :33600 B:19
## (Other) :85 O: 1
write_csv(stars, "stars_dslabs.csv", na="")
I chose to create a scatterplot with highcharter. I chose the Red-Yellow-Blue color scheme to mimic the color of stars as they increase in temperature. I decided to plot the temperature vs the brightness of the stars and use a tooltip to show the name of each star. The colors also group the stars by their classification.
# set color palette
cols <- brewer.pal(7, "RdYlBu")
my_scatterplot <- highchart() %>%
hc_add_series(data = stars, type = "scatter", hcaes(x = temp, y = magnitude, group = type)) %>%
hc_add_theme(hc_theme_darkunica()) %>%
hc_colors(cols) %>%
hc_xAxis(title = list(text="Temperature (K)")) %>%
hc_yAxis(title = list(text="Brightness (absolute magnitude)")) %>%
hc_title(text = "Brightness of a Star as Temperature increases") %>%
hc_legend(align = "right", title = "Type") %>%
hc_tooltip(shared = TRUE, pointFormat = "{point.star}", crosshairs = TRUE) %>%
hc_chart(style = list(fontWeight = "bold"))
my_scatterplot
```