# I loaded in the packages I would need and viewed the datasets that dslabs had
library("dslabs")
## Warning: package 'dslabs' was built under R version 4.0.4
library("dplyr")
## Warning: package 'dplyr' was built under R version 4.0.4
## 
## 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
data(package="dslabs")
list.files(system.file("script", package = "dslabs"))
##  [1] "make-admissions.R"                   
##  [2] "make-brca.R"                         
##  [3] "make-brexit_polls.R"                 
##  [4] "make-death_prob.R"                   
##  [5] "make-divorce_margarine.R"            
##  [6] "make-gapminder-rdas.R"               
##  [7] "make-greenhouse_gases.R"             
##  [8] "make-historic_co2.R"                 
##  [9] "make-mnist_27.R"                     
## [10] "make-movielens.R"                    
## [11] "make-murders-rda.R"                  
## [12] "make-na_example-rda.R"               
## [13] "make-nyc_regents_scores.R"           
## [14] "make-olive.R"                        
## [15] "make-outlier_example.R"              
## [16] "make-polls_2008.R"                   
## [17] "make-polls_us_election_2016.R"       
## [18] "make-reported_heights-rda.R"         
## [19] "make-research_funding_rates.R"       
## [20] "make-stars.R"                        
## [21] "make-temp_carbon.R"                  
## [22] "make-tissue-gene-expression.R"       
## [23] "make-trump_tweets.R"                 
## [24] "make-weekly_us_contagious_diseases.R"
## [25] "save-gapminder-example-csv.R"
# I chose to explore the stars dataset and used the str command to view the data 
data("stars")
str(stars)
## 'data.frame':    96 obs. of  4 variables:
##  $ star     : Factor w/ 95 levels "*40EridaniA",..: 87 85 48 38 33 92 49 79 77 47 ...
##  $ magnitude: num  4.8 1.4 -3.1 -0.4 4.3 0.5 -0.6 -7.2 2.6 -5.7 ...
##  $ temp     : int  5840 9620 7400 4590 5840 9900 5150 12140 6580 3200 ...
##  $ type     : chr  "G" "A" "F" "K" ...
# I did a quick reordering of variables using the dplyr command 'group_by' to organize the observations by the variable 'type' 
stars_dataset <- stars %>%
  group_by(type) %>%
  summarize(temp, magnitude)
## `summarise()` has grouped output by 'type'. You can override using the `.groups` argument.
head(stars_dataset)
## # A tibble: 6 x 3
## # Groups:   type [1]
##   type   temp magnitude
##   <chr> <int>     <dbl>
## 1 A      9620       1.4
## 2 A      9900       0.5
## 3 A      8060       2.2
## 4 A      9060       2  
## 5 A      9340      -7.2
## 6 A      9620       1.2
# I loaded in the packages I need to make a visualization through highcharter 
library(highcharter)
## Warning: package 'highcharter' was built under R version 4.0.4
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## 
## Attaching package: 'highcharter'
## The following object is masked _by_ '.GlobalEnv':
## 
##     stars
## The following object is masked from 'package:dslabs':
## 
##     stars
library(RColorBrewer)
cols <- brewer.pal(10, "Set1") # I set the color palette I want to use for my data and named it cols
## Warning in brewer.pal(10, "Set1"): n too large, allowed maximum for palette Set1 is 9
## Returning the palette you asked for with that many colors
highchart () %>%
  hc_add_series(data = stars_dataset, # Chose my reorganized dataset to use 
                type = "scatter", # Decided to make a scatterplot by setting the type to 'scatter'
                hcaes(x = temp, # Designated my x-axis, y-xis, and the variable I wanted to plot 
                      y = magnitude,
                      group = type)) %>%
  hc_colors(cols) %>% # Applied the color palette I chose earlier for the plotted variables
  hc_chart(style = list(fontFamily = "Helvetica")) %>% # I changed the generic font to one I liked
  hc_add_theme(hc_theme_darkunica()) %>% # I changed the generic theme to a dark one 
  hc_title(text = "Star Types and their General Attributes", # Added my title and changed the text color, and did the same with the subtitle
           margin = 20, align = "left",
           style = list(color = "#ffff66", useHTML = TRUE)) %>%
  hc_subtitle(text = "A look into how star types are categorized through temperature and magnitude",
              align = "left", style = list(color = "#ffffff", fontWeight = "bold")) %>%  
  hc_xAxis(title = list(text="Temperature of the Star (degrees Celsius)")) %>% # Labeled my axes
  hc_yAxis(title = list(text="Magnitutude of the Star (M)")) %>%
  hc_legend(align = "right", verticalAlign = "top", # Added a legend and placed it on the top right and displated it vertically 
            layout = "vertical") %>%
  hc_tooltip(enabled = TRUE) # Enabled tooltip so that I could hover over the plotted points and see exact numbers

For this weeks visualization, I wanted to explore the ‘Stars’ dataset in the dslabs package. This dataset is quite simple: it is not large, only containing 96 observations of 4 variables (the star’s name, its temperature, its magnitude, and the star type its classifed under the Morgan Keenan system). The Morgan Keenan (MK) system organizes stars by how hot they are, O being the hottest and M being the coolest. I wanted to see how what how a star’s temperature related to its magnitude, or brightness. Interestingly, the higher a star’s magnitude is, the dimmer it is. I decided that in order to observe correlation, the best way to go would be a scatterplot. Instead of using ggplot, I went with highcharter’s scatterplot because one) I find it more pleasing to look at it and two) it’s interactiveness would allow me to view the types individually on the graph through the function of the legend. I didn’t have to do much cleaning of the dataset other then using the dplyr ‘group_by’ and ‘summarize’ function. When coding the plot through highcharter, I decided to go with darkunica color theme and the Set2 ColorBrewer pallete because I wanted it to resemble the night sky (bright colors on a dark background). I put the temperature variable as my x-axis and magnitude variable as my y-axis and added a descriptive title and subtitle. Looking at the resulting scatterplot, its really cool to see how the types are categorized based on their temperature and how there is a downward trend as the temperature gets higher. So, it would seem that there is a correlation between how a higher temperature means a brighter star.

Sources:

https://cosmonova.org/different-types-stars-stellar-evolution/