DS Labs HW

Author

Hana Rose

Use the Package DSlabs (Data Science Labs) and Load Libraries

library(highcharter)
Warning: package 'highcharter' was built under R version 4.4.3
Registered S3 method overwritten by 'quantmod':
  method            from
  as.zoo.data.frame zoo 
library(dplyr)
Warning: package 'dplyr' was built under R version 4.4.3

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(dslabs)
Warning: package 'dslabs' was built under R version 4.4.3

Attaching package: 'dslabs'
The following object is masked from 'package:highcharter':

    stars
library(purrr)

Select a Dataset from DSlabs

I’m choosing the “stars” dataset.

Load Selected Dataset (Stars)

data("stars", package = "dslabs")

View Dataset

View(stars)

Select Variables

My variables will be:

x = temperature (temp) y = magnitude (magnitude) color = star type (type)

Coordinate Colors

I want the colors of the stars on the graph to match the colors of their spectral types. To do this, I’m creating a custom color scale by assigning the matching color value to it’s spectral type.

# Create custom color scale
spectral_colors <- c(
  "O" = "blue", 
  "B" = "lightblue", 
  "A" = "white", 
  "F" = "lightyellow", 
  "G" = "yellow", 
  "K" = "orange", 
  "M" = "red", 
  "DA" = "white", 
  "DB" = "lightblue", 
  "DF" = "white"
)
# Assign color values to spectral types
stars <- stars %>%
  mutate(color = spectral_colors[type])

Set Up Luminosity Classes

I’m adding luminosity classes to the legend to make the interactive graph a little more fun.

# Label luminosity classes
luminosity_classes <- c(
  "Supergiant" = "Supergiant",
  "Bright Giant" = "Bright Giant",
  "Giant" = "Giant",
  "Subgiant" = "Subgiant",
  "Dwarf" = "Dwarf",
  "White Dwarf" = "White Dwarf"
)
# Create a new column in the "stars" dataset (luminosity) and categorize luminosity classes by stellar classification
stars <- stars %>%
  mutate(luminosity = case_when(
    type %in% c("O", "B") ~ "Supergiant", 
    type %in% c("A") ~ "Bright Giant", 
    type %in% c("F", "G") ~ "Giant", 
    type %in% c("K") ~ "Subgiant", 
    type %in% c("M") ~ "Dwarf", 
    type %in% c("DA", "DB", "DF") ~ "White Dwarf",
    TRUE ~ "Unknown"
  ))

Create Plot

For my graph, I’m doing a scatterplot showing the relationships between stars and their magnitudes, temperatures, stellar classifications, and star types. My inspiration comes from the Hertzsprung-Russell diagram, a graph astronomers use to understand the life cycles and properties of stars by observing their temperatures and luminosity.

hc <- highchart() %>% # Set up highcharter
  hc_chart(type = "scatter", backgroundColor = "#000000") %>%  # Make the graph type a scatterplot and customize the background. I'm choosing black because it looks like space and shows the colors of the dots (or stars) well. 
  hc_xAxis(title = list(text = "Temperature (Kelvin)"), reversed = TRUE) %>% # Add an x-axis, give it a title, and reverse the x-axis so it fits H-R diagram formatting (higher temperatures are on the left and lower temperatures are on the right.)
  hc_yAxis(title = list(text = "Magnitude")) %>% # Add a y-axis and give it a title.
  hc_legend( # Create a legend
    itemStyle = list(color = "white"), # Customize legend text color. I'm making mine white for visibility. 
    title = list(text = "Star Type", style = list(color = "white")) # Give the legend a title. 
  ) %>% 
  hc_title( # Give the chart a title
    text = "H-R Star Graph",
    style = list(color = "white")
  ) %>%
  hc_plotOptions( # Customize graph
    scatter = list( # Customize scatterplot 
      marker = list(symbol = "circle"), # Make the dots on the scatterplot circles 
      dataLabels = list(enabled = TRUE, format = "{point.name}", color = "white"),  # Label the stars by name and make the text white
      states = list(hover = list(enabled = TRUE, lineWidth = 1)) # Add hover effect
    )
  ) %>%
  hc_tooltip(  # Correctly placed tooltip option
    pointFormat = "Temperature: {point.x} K<br>Magnitude: {point.y}"  # Customize tooltip to display magnitude and temperature in kelvin.
  )

# Add the spectral types
for (t in names(spectral_colors)) { # Loop through the spectral types in spectral_colors
  star_subset <- stars %>% filter(type == t) # Filter for spectral types in stars dataset
  
  hc <- hc %>% # Update highcharter
    hc_add_series( # Set up legend
      data = list_parse(star_subset %>% 
                          transmute(x = temp, y = magnitude, name = star, color = color)), # Assign values to variables
      type = "scatter", # Convert data into scatterplot formatting
      name = t, # Give the series a name (Spectral Type)
      color = spectral_colors[t],  # Set the colors for the series according to spectral type
      marker = list(radius = 3) # Set the radius of the points on the scatterplot
    )
}

# Add luminosity classes
luminosity_classes <- c("Supergiant", "Bright Giant", "Giant", "Subgiant", "Dwarf", "White Dwarf")

for (lum in luminosity_classes) { # Loop through the luminosity classes
  star_subset <- stars %>% filter(luminosity == lum)  # Filter for luminosity classes
  
  hc <- hc %>% # Update highcharter
    hc_add_series( # Add luminosity classes to the plot
      data = list_parse(star_subset %>% 
                          transmute(x = temp, y = magnitude, name = star, color = color)), # Assign values to variables
      type = "scatter", # Convert data into scatterplot formatting
      name = lum, # Give the series a name (Luminosity Class)
      color = "white", # Set the color to white for the luminosity series (no color change here)
      marker = list(radius = 3) # Set the radius of the points on the scatterplot
    )
}

# Print plot
hc
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.

☆ Final graph! ☆