Problem 1

library(ggplot2)
library(plotly)
 
# Sample data
set.seed(123)
age <- rnorm(100, mean = 40, sd = 10)
income <- rnorm(100, mean = 50000, sd = 10000)
education_levels <- sample(c("High School", "Bachelor's Degree", "Master's Degree", "PhD"), 100, replace = TRUE)
employment_status <- sample(c("Employed", "Unemployed"), 100, replace = TRUE)
 
# Create data frame
data <- data.frame(age = age, income = income, 
                   education = education_levels, employment = employment_status)
 
# Create ggplot
plot <- ggplot(data, aes(x = age, y = income, color = education, shape = employment, text = paste('Age: ', age, '<br>',
'Income: ', income, '<br>',
'Education: ', education, '<br>',
'Employment: ', employment))) +
        geom_point(alpha = 0.5, size = 2) +
        labs(title = 'Relationship Between Age, Income, Education, and Employment Status',
             x = 'Age', y = 'Income') +
        scale_shape_manual(values = c(15, 16)) +  # Different shapes for Employed and Unemployed
        theme_minimal()
 
# Convert ggplot to plotly
plotly_plot <- ggplotly(plot)
 
# Show plot
plotly_plot

Discussion: The given code creates a scatter plot that illustrates the link between age and income. For employment status and education level, additional information is represented by shape and color, respectively. With their age on the x-axis and their income on the y-axis, each point in the dataset represents a unique person. The shape indicates which people are employed and which are unemployed, while the color of the points indicates the person’s levels of education.

Any data point that the cursor is over will produce a tooltip with comprehensive details about that specific person, such as their age, income, degree of education, and employment status. By giving users detailed information about individual data points, this interactive feature improves the plot’s interpretability and helps users gain a deeper knowledge of the relationships between the variables.

Encoding numerous variables simultaneously may be a challenge when making this plot. Ensuring that the storyline remains clear and understandable while including color, shape, and tooltip information necessitates careful aesthetic and design decisions. In this scenario, employing a simple theme and altering the transparency of points help to retain visual clarity without overwhelming the observer with unnecessary detail.

Furthermore, the color palette for education degrees and shapes for work status should be intuitive and understandable to viewers. Testing different combinations and ensuring that colors and forms are easily distinct will help reduce potential confusion.

Problem 2

# Load necessary libraries
library(plotly)
library(dslabs)
library(dplyr)

# Load sample dataset from dslabs package
data("murders")

# Create a unique column name
colnames(murders)[1] <- "state_code"

# Sample data for animation
set.seed(123)
murders_sampled <- murders %>%
  sample_n(51)

# Create animated plotly graph
p <- plot_ly(data = murders_sampled, 
             x = ~population, 
             y = ~total, 
             color = ~region, 
             text = ~state_code, 
             frame = ~region, 
             type = "scatter", 
             mode = "markers") %>%
  layout(title = "Murders by Population in States",
         xaxis = list(title = "Population", showticklabels = TRUE),  # Added showticklabels = TRUE
         yaxis = list(title = "Total Murders"),
         showlegend = TRUE)

# Customize frame label
p <- p %>%
  animation_opts(frame = 3000, redraw = TRUE) %>%
  layout(annotations = list(
    list(
      x = 0.95,  # Adjust the x-coordinate position of the label
      y = -0.1,  # Adjust the y-coordinate position of the label
      text = "Region",  # Text for the frame label
      showarrow = FALSE,
      xref = "paper",
      yref = "paper",
      font = list(size = 14, color = "black")  # Adjust font size and color
    )
  ))

# Show plot
p
## Warning in p$x$data[firstFrame] <- p$x$frames[[1]]$data: number of items to
## replace is not a multiple of replacement length

Discussion: The code generates an animated scatter plot that depicts the association between population and total murders in various states across the United States. Each point on the plot represents a state, with the x-axis designating population and the y-axis representing total murders. The points are color-coded according to the region to which each state belongs, making it easier to distinguish between different regions.

Several trends emerge during the animation. First, there appears to be a general positive link between population size and total number of murders, implying that states with larger populations have higher murder rates. However, the data show that this relationship is not exactly linear, as states with smaller populations had greater murder rates than states with higher populations. Furthermore, certain regions appear to have higher concentrations of murders than others, indicating probable geographical variances in crime rates.

One potential concern faced while building the plot was ensuring that the frame label denoting the region did not impede the plot’s perspective or interfere with data interpretation. I was able to increase the visibility of the frame label without overshadowing the plot by altering its location, text size, and color. Another consideration was the removal of extraneous labels, such as the “Region” label on the x-axis, in order to streamline the plot and improve clarity. Overall, these changes helped to produce a visually pleasing and informative animated scatter plot that successfully displays data trends and patterns.