UntiDSLabs Datasets & Highcharter Assignmenttled

Author

G Oliveira

Load Packages

# Load packages
library(dslabs)
library(dplyr)
library(ggplot2)
library(highcharter)
library(RColorBrewer)
#choosing the dataset
data(package = "dslabs")
?stars
Help on topic 'stars' was found in the following packages:

  Package               Library
  dslabs                /Library/Frameworks/R.framework/Versions/4.6/Resources/library
  graphics              /Library/Frameworks/R.framework/Versions/4.6/Resources/library
  highcharter           /Library/Frameworks/R.framework/Versions/4.6/Resources/library


Using the first match ...

Data: stars - Physical Properties of Stars, including luminosity, temperature, and spectral class.

Prepare the Data

# Load the stars data from dslabs
data(stars, package = "dslabs")
head(stars)
            star magnitude temp type
1            Sun       4.8 5840    G
2        SiriusA       1.4 9620    A
3        Canopus      -3.1 7400    F
4       Arcturus      -0.4 4590    K
5 AlphaCentauriA       4.3 5840    G
6           Vega       0.5 9900    A
#checking the type class
table(stars$type)

 A  B DA DB DF  F  G  K  M  O 
13 19  2  1  1  7  4 16 32  1 
# Remove missing values and make the spectral type a factor
stars1 <- stars |>
  filter(!is.na(temp), !is.na(magnitude)) |>
  mutate(type = as.factor(type))

Build the Graph

#defining variables
ggplot(stars1, aes(x = temp, y = magnitude, color = type)) +
  #setting size and alpha to give enough space
  geom_point(size = 3, alpha = 0.8) +
  #log scaling
  scale_x_log10("Surface temperature (log scale)") +
  #unit scale
  scale_y_continuous("Absolute magnitude (lower = brighter)") +
  #color for the third viable, title and legende
  scale_color_brewer(name = "Spectral type", palette = "Paired") +
  labs(title = "Temperature and Brightness of Stars by Spectral Type",
       caption = "Source: DS Labs") +
  #changing the default color
  theme_minimal(base_size = 14, base_family = "serif")

interactive scatterplot

hchart(stars1, type = "scatter",
       hcaes(x = temp, y = magnitude, group = type, name = star)) |>
  # ColorBrewer "Paired" colors as the static graph
  hc_colors(brewer.pal(12, "Paired")) |>
  # log scale on the x-axis, with axis title and units
  hc_xAxis(type = "logarithmic",
           title = list(text = "Surface temperature (log scale)")) |>
  # y-axis title
  hc_yAxis(title = list(text = "Magnitude (lower = brighter)")) |>
  # title, caption, and legend title
  hc_title(text = "Temperature and Brightness of Stars by Spectral Type") |>
  hc_caption(text = "Source: DS Labs") |>
  hc_legend(title = list(text = "Spectral type"))

Description

Both graphs uses the stars dataset from the dslabs package. The variables are surface temperature in log, absolute magnitude, a measure of true brightness, and spectral type. The first graph was built with ggplot2 by removing missing values, converting spectral type to a factor, and drawing a scatterplot with geom_point(). I used a log scale for temperature. I also made an interactive plot with highcharter from previous classes using the same variables and colors, where hovering over a point shows the name of the star.

The graph shows that temperature and brightness are related, but not in a single straight line. Hotter stars are also brighter. A separate group of very bright but cooler stars sits in the upper right, and a group of hot but dim stars sits in the lower left. This shows that brightness depends on more than temperature alone.