# load required packages
library(tidyverse)
setwd("C:/Users/bombshellnoir/Dropbox (Personal)/00000 Montgomery College/DATA 110")Week 8 DSLabs Datasets
Load required packages and datasets from course datasets link
#install.packages("dslabs")
# these are data science labs
library("dslabs")Warning: package 'dslabs' was built under R version 4.3.3
data(package="dslabs")
list.files(system.file("script", package = "dslabs")) [1] "make-admissions.R"
[2] "make-brca.R"
[3] "make-brexit_polls.R"
[4] "make-calificaciones.R"
[5] "make-death_prob.R"
[6] "make-divorce_margarine.R"
[7] "make-gapminder-rdas.R"
[8] "make-greenhouse_gases.R"
[9] "make-historic_co2.R"
[10] "make-mice_weights.R"
[11] "make-mnist_127.R"
[12] "make-mnist_27.R"
[13] "make-movielens.R"
[14] "make-murders-rda.R"
[15] "make-na_example-rda.R"
[16] "make-nyc_regents_scores.R"
[17] "make-olive.R"
[18] "make-outlier_example.R"
[19] "make-polls_2008.R"
[20] "make-polls_us_election_2016.R"
[21] "make-pr_death_counts.R"
[22] "make-reported_heights-rda.R"
[23] "make-research_funding_rates.R"
[24] "make-stars.R"
[25] "make-temp_carbon.R"
[26] "make-tissue-gene-expression.R"
[27] "make-trump_tweets.R"
[28] "make-weekly_us_contagious_diseases.R"
[29] "save-gapminder-example-csv.R"
data("stars")
library(tidyverse)
library(ggthemes)Warning: package 'ggthemes' was built under R version 4.3.3
library(ggrepel)Warning: package 'ggrepel' was built under R version 4.3.3
view(stars)Initial Perusal
types_unique <- unique(stars$type)
types_unique [1] "G" "A" "F" "K" "B" "M" "O" "DA" "DF" "DB"
types_num <- length(types_unique)
types_num[1] 10
Customization
Types DA, DF, and DB are all dwarf stars. For the purposes of this visualization, I will rename DA, DF, and DB to the same type, D.
dwarf <- c("DA", "DF", "DB")
star_viz <- stars
star_viz$type[star_viz$type %in% c("DA", "DF", "DB")] <- "D"Load additional packages
#install.packages("highcharter", "RColorBrewer")
library(scales)
Attaching package: 'scales'
The following object is masked from 'package:purrr':
discard
The following object is masked from 'package:readr':
col_factor
library(highcharter)Warning: package 'highcharter' was built under R version 4.3.3
Registered S3 method overwritten by 'quantmod':
method from
as.zoo.data.frame zoo
Highcharts (www.highcharts.com) is a Highsoft software product which is
not free for commercial and Governmental use
Attaching package: 'highcharter'
The following object is masked _by_ '.GlobalEnv':
stars
The following object is masked from 'package:dslabs':
stars
library(RColorBrewer)Custom Color Scheme
I tried creating a custom color scheme here. For further control over the legend, I factored the type of star so I could specify the order.
custom_colors <- c(
"O" = "dodgerblue3",
"B" = "steelblue",
"A" = "lightblue",
"F" = "lightyellow",
"G" = "yellow",
"K" = "darkgoldenrod",
"M" = "lightsalmon",
"D" = "#FFFFFF"
)
type_order <- c("O", "B", "A", "F", "G", "K", "M", "D")
star_viz$type <- factor(star_viz$type, levels = type_order)
star_viz$color <- custom_colors[star_viz$type]Attempt 1
star_viz %>%
hchart("scatter",
hcaes(x=magnitude,
y=temp,
name = star,
group = type,
color = type)) %>%
hc_colors(custom_colors) %>%
hc_chart(backgroundColor = "#000000",
style = list(color = "#FFFFFF", fontWeight = "bold")) %>%
hc_title(text = "A Cascade of Stars",
align = "right",
verticalAlign = "bottom",
style = list(fontSize="30px")) %>%
hc_caption(text = "Source: Stars dataset from DSLabs") %>%
hc_xAxis(title = list(text = "Magnitudes")) %>%
hc_yAxis(title = list(text = "Temperature (in Kelvins")) %>%
hc_tooltip(
headerFormat = "<b>{point.key}</b><br>",
pointFormat = "Type: {point.series.name}<br>Temperature: {point.y:,.0f}<br>Magnitude: {point.x}"
) %>%
hc_legend(verticalAlign = "top")Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.
Attempt 2
In the following code, I added a starry background, made the legend easier to read, and tried to brighten the chart text. If I could fix the text color in the entire chart, and change the color of the type B stars, this would be my favorite version.
star_viz %>%
hchart("scatter",
hcaes(x=magnitude,
y=temp,
name = star,
group = type,
color = type)) %>%
hc_colors(custom_colors) %>%
hc_chart(backgroundColor = "transparent",
divBackgroundImage = "http://www.wired.com/images_blogs/underwire/2013/02/xwing-bg.gif",
style = list(color = "#FFFFFF", fontWeight = "bold")) %>%
hc_title(text = "A Cascade of Stars",
align = "right",
verticalAlign = "bottom",
style = list(fontSize="30px", color = "#FFFFFF")) %>%
hc_subtitle(verticalAlign = "bottom",
align = "right",
text = "Stellar Classifications",
style = list(color = "#FFFFFF")) %>%
hc_caption(text = "Source: Stars dataset from DSLabs",
style = list(color = "#FFFFFF")) %>%
hc_xAxis(title = list(color = "#FFFFFF", text = "Magnitudes")) %>%
hc_yAxis(title = list(text = "Temperature (in Kelvins")) %>%
hc_tooltip(
headerFormat = "<b>{point.key}</b><br>",
pointFormat = "Type: {point.series.name}<br>Temperature: {point.y:,.0f}<br>Magnitude: {point.x}"
) %>%
hc_legend(verticalAlign = "top",
backgroundColor = "grey",
title = list(text = "Star Types"),
style = list(color = "white"))Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.
Attempt 3
An easy to read version.
star_viz %>%
hchart("scatter",
hcaes(x=magnitude,
y=temp,
name = star,
group = type,
color = type)) %>%
hc_colors(custom_colors) %>%
hc_title(text = "A Cascade of Stars",
align = "right",
verticalAlign = "bottom",
style = list(fontSize="30px")) %>%
hc_subtitle(verticalAlign = "bottom",
align = "right",
text = "Stellar Classifications") %>%
hc_caption(text = "Source: Stars dataset from DSLabs") %>%
hc_xAxis(title = list(color = "white", text = "Magnitudes")) %>%
hc_yAxis(title = list(text = "Temperature (in Kelvins")) %>%
hc_tooltip(
headerFormat = "<b>{point.key}</b><br>",
pointFormat = "Type: {point.series.name}<br>Temperature: {point.y:,.0f}<br>Magnitude: {point.x}"
) %>%
hc_legend(verticalAlign = "top",
title = list(text = "Star Types"))Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.
Writeup
I used the “stars” dataset from the “dslabs” package. After exploring the data, I decided to group by type of star and to consolidate the D* stars - the dwarf stars. I customized the legend to reflect the spectrum of star temperatures, from hottest (type O) to coolest (type D). I tried to customize the color scheme so that the star type’s color on the chart is similar to the star color in the night sky (hence the black background). The process requires further refinement.