DS Labs Homework

Author

Z Griffin

Published

March 3, 2026

Load libraries and data

Load Highcharter first so it doesn’t mask the dslabs stars. Load DSlabs (data science labs) library and package along with RColorBrewer, dplyer.

Using the “stars” dataset: physical properties of stars.

library(highcharter)
library(RColorBrewer)
library(dslabs)
data(package = "dslabs")
library(dplyr)
data("stars")

Use library conflicted to force rstudio to recognize dslabs stars over highcharter stars

library(conflicted)
Warning: package 'conflicted' was built under R version 4.5.3
conflicts_prefer(dslabs::stars)
[conflicted] Will prefer dslabs::stars over any other package.

Cleaning

Find how many of each type of star there are:

unique(stars$type)
 [1] "G"  "A"  "F"  "K"  "B"  "M"  "O"  "DA" "DF" "DB"

Wikipedia told me that the D is actually a classifier of white dwarfs, which are not included in the main sequence, and are broken into subtypes, but also that DF doesn’t exist. Checking how many of those there are:

group_by(stars, type) |>
  count()|>
  arrange(desc(n))
# A tibble: 10 × 2
# Groups:   type [10]
   type      n
   <chr> <int>
 1 M        32
 2 B        19
 3 K        16
 4 A        13
 5 F         7
 6 G         4
 7 DA        2
 8 DB        1
 9 DF        1
10 O         1

The help shows that the types are the OBAFGKM system, which didn’t explain the double letter classes so looked them up, discovered the D was for white dwarfs, first thought I could just remove the D and group them with the rest of the classes, but then noticed that no, actually, the white dwarfs use a completely different letter system and and I want this to be accurate so I’m fixing it. Only Pryocon B and 40 Eridani B needed fixing.

Going manually by index.

stars1 <-stars |>
  mutate(type2 = type) |> 
  rename(surftemp = temp)  #the variable temp is *never* popping up in the next chunk, maybe changing it will help
  stars1[74, "type2"] <- "DQZ"  # Procyon B`
  stars1[86, "type2"] <- "DZ"  # Van Maanen's Star;
  #the two type DA SiriusB and 40 Eridani B, are correct and don't need to  be changed 

confirm that that worked, also get those types in OBAFGKM order via as factor

unique(stars1$type2) 
 [1] "G"   "A"   "F"   "K"   "B"   "M"   "O"   "DA"  "DQZ" "DZ" 
stars1$type2 <- factor(stars1$type2, levels= c("O","B","A","F","G","K","M","DA","DZ", "DQZ"))

Plot!

stars1$type2 <- factor(stars1$type2, levels= c("O","B","A","F","G","K","M","DA","DZ", "DQZ")) 
# apparently the factor levels did not hold over from the previous chunk  
cols <- brewer.pal(10, "Paired")

highchart() |>
  hc_add_series(stars1,
                type = "scatter",
                hcaes(x = surftemp,
                     y = magnitude,
                     group = type2)) |>
 hc_add_theme(hc_theme_darkunica()) |>
  hc_legend(title = list(text = "Stellar Classification", align = "center")) |>
  hc_colors(cols) |>
  hc_title(text = "Absolute Magnitute vs Surface Tempurature of Stars <br> by Stellar Classification") |>
  hc_xAxis(title = list(text="Surface Temperature (Kelvin)")) |>
  hc_yAxis(title = list(text="Absolute Magnitude")) |>
  hc_caption(
    text = "Source: dslabs", align = "right") |>
  hc_plotOptions(series = list(marker = list(radius = 4.8, symbol = "circle"))) |>
  hc_tooltip(pointFormat = "{point.star} <br> Class: {point.type2} <br> {point.surftemp} Kelvin <br> Absolute magnitude: {point.magnitude}") 

Additional Comments

I used the dslabs “stars” dataset of physical properties of 94 stars. As the highcharter library also includes a “stars” dataset, this proved an additional challenge, and forced me to learn what the messages I keep suppressing when loading libraries about masking functions actually mean, and how to solve them. I first tried to solve this issue by loading dslabs second, and when that proved insufficient, using library conflicted. My scatterplot shows the absolute magnitude (how bright a star would appear at a standard distance of 10 parsecs away) vs it’s surface temperature, with stars colored by their stellar classification. Two of the four white dwarfs had inaccurate classifications, which I fixed manually via their row indexes – information sources below– after mutating a new row for updated type. I included the star’s name on the tooltip along with the class, temperature, and magnitude. The Harvard system of stellar classification is derived from electromagnetic composition, and while it was not known at the time it was also a temperature sequence, O type being the hottest and M being the coolest was among the first results my google search turned up, so I was not surprised at the groupings on my graph. I am however intriged by how close to overlapping the lower temperature groups are, and how spread out class B is in comparison.

I am not a huge fan of the highcharter themes. It seems that apart from changing the background color, they mostly change the default colors, and since I specified my group colors, that is irrelevent. Also, the axes labeles are near illegible to me on nearly all the lighter themes, and I not going and coding a custom theme right now. I probably will have to later. Thankfully, the “paired” color palette pops on this dark background.

References

https://en.wikipedia.org/wiki/Stellar_classification

https://www.stellarcatalog.com/stars/procyon-b

https://www.stellarcatalog.com/stars/van-maanens-star

https://www.stellarcatalog.com/stars/sirius-b

https://www.stellarcatalog.com/stars/40-eridani-b