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 a new column in the "stars" dataset (luminosity) and categorize luminosity classes by stellar classificationstars <- 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 highcharterhc_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 legenditemStyle =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 titletext ="H-R Star Graph",style =list(color ="white") ) %>%hc_plotOptions( # Customize graphscatter =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 whitestates =list(hover =list(enabled =TRUE, lineWidth =1)) # Add hover effect ) ) %>%hc_tooltip( # Correctly placed tooltip optionpointFormat ="Temperature: {point.x} K<br>Magnitude: {point.y}"# Customize tooltip to display magnitude and temperature in kelvin. )# Add the spectral typesfor (t innames(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 highcharterhc_add_series( # Set up legenddata =list_parse(star_subset %>%transmute(x = temp, y = magnitude, name = star, color = color)), # Assign values to variablestype ="scatter", # Convert data into scatterplot formattingname = t, # Give the series a name (Spectral Type)color = spectral_colors[t], # Set the colors for the series according to spectral typemarker =list(radius =3) # Set the radius of the points on the scatterplot )}# Add luminosity classesluminosity_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 highcharterhc_add_series( # Add luminosity classes to the plotdata =list_parse(star_subset %>%transmute(x = temp, y = magnitude, name = star, color = color)), # Assign values to variablestype ="scatter", # Convert data into scatterplot formattingname = 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 plothc
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.