library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.0      ✔ purrr   0.3.5 
## ✔ tibble  3.1.8      ✔ dplyr   1.0.10
## ✔ tidyr   1.2.1      ✔ stringr 1.4.1 
## ✔ readr   2.1.3      ✔ forcats 0.5.2 
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout
stars <- read.csv("near_stars.csv")
View(stars)
stars <- stars %>% rename(Color = bv_color, Radius = R) 

Part 1

colorvec <- c("red4", "red", "yellow", "white", "lightcyan",
              "cyan", "blue", "navy")
gg<-ggplot(stars, aes(x=Color, y=L)) + geom_point(aes(size=Radius, col=temp)) +
  scale_y_continuous(trans='log10') + 
  ggtitle("Star Color in Comparison to Brightness") + 
  labs(x="Color of Star", y="Brightness (L/L0) (Log Scale)", 
       subtitle = "Hertzsprung-Russell Diagram",
       color = "Temperature (K)",
       size = "Radius (R/R0)") +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_rect(fill = 'black')) +
  scale_color_gradientn(colours = colorvec) +
  scale_size(range = c(0,7)) 
gg
## Warning: Removed 10 rows containing missing values (`geom_point()`).

# Part 2

ggplotly(gg) 

Part 3

starNew <- stars %>% mutate(Distance = case_when(
  (parsec <= 6) ~ "0-6 Parsec",
  (parsec > 6 & parsec <= 12) ~ "06-12 Parsec",
  (parsec > 12 & parsec <= 18) ~ "12-18 Parsec",
  (parsec > 18 & parsec <= 24) ~ "18-24 Parsec",
  (parsec > 24 & parsec <= 30) ~ "24-30 Parsec"
))
#https://community.rstudio.com/t/use-mutate-to-add-large-number-of-levels-to-a-factor-variable/47715
starNew <- starNew %>% rename(Temperature = temp)
palette <- colorRampPalette(c("red4", "red", "yellow", "white", "lightcyan",
              "cyan", "blue", "navy"))
axes <- list(
  zeroline = FALSE,
  showline = FALSE,
  showgrid = FALSE
)

p <- plot_ly(data = starNew, x = ~Color, y = ~L, color = ~Temperature, 
             size = ~Radius, 
        colors = palette(50),
        hoverinfo = 'text',
        frame = ~Distance,                            #https://plotly.com/r/animations/ 
        text = ~paste('</br> Name: ', name,         #https://plotly.com/r/text-and-annotations/
                      '</br> Color: ', Color,
                      '</br> Brightness: ', L,
                      '</br> Radius: ', Radius 
      )
      ) %>% 
  layout(yaxis = list(type = "log"), paper_bgcolor="black",
       plot_bgcolor='black', xaxis = axes, yaxis = axes)    #https://plotly.com/r/axes/
f <- list(                                 #https://plotly.com/r/figure-labels/
  family = "Courier New, monospace",
  size = 16,
  color = "grey"
)
x <- list(
  title = "Color",
  titlefont = f
)
y <- list(
  title = "Brightness (L/L0)(Log Scale)",
  titlefont = f
)
p <- p %>% layout(xaxis = x, yaxis = y, 
                  title = "Star Color in Comparison to Brightness 
Hertzsprung-Russell Diagram")
p
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
## Warning: Ignoring 10 observations
## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

The plotly graph is a Hertzsrpung-Russel Diagram that compares star colors to brightness. In addition to these two axes variables, the interactive graph indicates the temperature, radius and parsec of the stars. The parsec variable is the distance between the star and Earth, which can be manipulated by the user. Rather than having individual parsecs for each star in the animation, the stars are clumped into five groups based on parsec ranges, so the user can easily compare stars within a parsec group. Because it is an animation, the user can choose to either manually move the slider to a different parsec range or have the slider play in a continuous fashion. Temperature was indicated through the use of color. A heat gradient scale was used, so the viewer would be able to decipher the temperature of the stars with ease. For example, the hottest stars are blue and the coldest are red, respectively. This color scale was chosen strategically to allude to a typical heat scale, and it is a gradient to indicate that it is a change in value, rather than separate categories. Lastly, shape was utilized to indicate the radius of the star. Although there is no legend for the radii, the users can hover over the individual stars to see the exact radius. Also, the hover tool allows the viewer to see the name of the star, and the exact color and brightness value.