Week 8 DSLabs Datasets

Author

Annet Isa

Initial Perusal

types_unique <- unique(stars_data$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_data

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 

Attaching package: 'highcharter'
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")) %>%
  #chatGPT helped with the following
  hc_add_series(name = "Our Sun",
                type = "scatter",
                data = list(list(x = 4.8, y = 5840)),
                marker = list(symbol = "star",
                              fillColor = "yellow",
                              lineWidth = 2,
                              lineColor = "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")) %>%
  #chatGPT helped with the following
  hc_add_series(name = "Our Sun",
                type = "scatter",
                data = list(list(x = 4.8, y = 5840)),
                marker = list(symbol = "star",
                              fillColor = "yellow",
                              lineWidth = 2,
                              lineColor = "black"))
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.