nqx_data <- read.csv("C:\\Users\\anami\\OneDrive\\Documents\\DEM\\Assignment6\\nqx.csv")
ndx_data <- read.csv("C:\\Users\\anami\\OneDrive\\Documents\\DEM\\Assignment6\\ndx.csv")
# Load necessary libraries
library(ggplot2)

# Ensure age_group is treated as a factor and ordered correctly
ndx_data$age_group <- factor(ndx_data$age_group, levels = ndx_data$age_group)

# Create the plot by age group for ndx data
ggplot(ndx_data, aes(x = age_group)) +
  geom_line(aes(y = hispanics, color = "Hispanics"), size = 1.5, group = 1) +  # Line for Hispanics
  geom_line(aes(y = whites, color = "NH Whites"), size = 1.5, group = 2) +     # Line for NH Whites
  geom_line(aes(y = blacks, color = "NH Blacks"), size = 1.5, group = 3) +     # Line for NH Blacks
  geom_point(aes(y = hispanics, color = "Hispanics"), size = 3) +   # Points for Hispanics
  geom_point(aes(y = whites, color = "NH Whites"), size = 3) +      # Points for NH Whites
  geom_point(aes(y = blacks, color = "NH Blacks"), size = 3) +      # Points for NH Blacks
  labs(title = "Number of Deaths \namong Hispanics, NH Whites, and NH Blacks, United States, 2010",
       x = "Age Group", y = "Number of Deaths") +
  scale_color_manual(values = c("Hispanics" = "coral", "NH Whites" = "seagreen", "NH Blacks" = "deepskyblue")) +  # Updated colors
  theme_minimal() +
  theme(
    axis.text.x = element_text(angle = 45, hjust = 1),  # Rotate x-axis labels for readability
    legend.position = "bottom",                        # Move legend to bottom
    legend.title = element_blank(),                    # Remove legend title
    plot.title = element_text(size = 16, hjust = 0.5)  # Bigger title and centered
  )
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

nlx_data <- read.csv("C:\\Users\\anami\\OneDrive\\Documents\\DEM\\Assignment6\\nlx.csv")
# Load necessary libraries
library(ggplot2)

# Ensure age_group is treated as a factor and ordered correctly
nlx_data$age_group <- factor(nlx_data$age_group, levels = nlx_data$age_group)

# Create the plot by age group for nlx (Number of Survivors) data
ggplot(nlx_data, aes(x = age_group)) +
  geom_line(aes(y = hispanics, color = "Hispanics"), size = 1.5, group = 1) +  # Line for Hispanics
  geom_line(aes(y = whites, color = "NH Whites"), size = 1.5, group = 2) +     # Line for NH Whites
  geom_line(aes(y = blacks, color = "NH Blacks"), size = 1.5, group = 3) +     # Line for NH Blacks
  geom_point(aes(y = hispanics, color = "Hispanics"), size = 3) +   # Points for Hispanics
  geom_point(aes(y = whites, color = "NH Whites"), size = 3) +      # Points for NH Whites
  geom_point(aes(y = blacks, color = "NH Blacks"), size = 3) +      # Points for NH Blacks
  labs(title = "Number of Survivors\nfor Hispanics, NH Whites, and NH Blacks, United States, 2010",
       x = "Age Group", y = "Number of Survivors") +
  scale_y_continuous(limits = c(20000, NA), labels = scales::comma) +  # Start y-axis at 20,000
  scale_color_manual(values = c("Hispanics" = "coral", "NH Whites" = "seagreen", "NH Blacks" = "deepskyblue")) +  # Updated colors
  theme_minimal() +
  theme(
    axis.text.x = element_text(angle = 45, hjust = 1),  # Rotate x-axis labels for readability
    legend.position = "bottom",                        # Move legend to bottom
    legend.title = element_blank(),                    # Remove legend title
    plot.title = element_text(size = 16, hjust = 0.5)  # Bigger title and centered
  )

ex

ex_data <- read.csv("C:\\Users\\anami\\OneDrive\\Documents\\DEM\\Assignment6\\ex.csv")
## Load necessary libraries
library(ggplot2)

# Ensure age_group is treated as a factor and ordered correctly
ex_data$age_group <- factor(ex_data$age_group, levels = ex_data$age_group)

# Create the plot by age group for ex (Life Expectancy) data
ggplot(ex_data, aes(x = age_group)) +
  geom_line(aes(y = hispanics, color = "Hispanics"), size = 1.5, group = 1) +  # Line for Hispanics
  geom_line(aes(y = whites, color = "NH Whites"), size = 1.5, group = 2) +     # Line for NH Whites
  geom_line(aes(y = blacks, color = "NH Blacks"), size = 1.5, group = 3) +     # Line for NH Blacks
  geom_point(aes(y = hispanics, color = "Hispanics"), size = 3) +   # Points for Hispanics
  geom_point(aes(y = whites, color = "NH Whites"), size = 3) +      # Points for NH Whites
  geom_point(aes(y = blacks, color = "NH Blacks"), size = 3) +      # Points for NH Blacks
  geom_hline(yintercept = 100, linetype = "dashed", color = "red", size = 1) +  # Add horizontal line at 100 years
  labs(title = "Life Expectancy\nfor Hispanics, NH Whites, and NH Blacks, United States,2010",
       x = "Age Group", y = "Life Expectancy (Years)") +
  scale_color_manual(values = c("Hispanics" = "coral", "NH Whites" = "seagreen", "NH Blacks" = "deepskyblue")) +  # Updated colors
  theme_minimal() +
  theme(
    axis.text.x = element_text(angle = 45, hjust = 1),  # Rotate x-axis labels for readability
    legend.position = "bottom",                        # Move legend to bottom
    legend.title = element_blank(),                    # Remove legend title
    plot.title = element_text(size = 16, hjust = 0.5)  # Bigger title and centered
  )

# Load necessary libraries
library(ggplot2)

# Ensure age_group is treated as a factor and ordered correctly
ex_data$age_group <- factor(ex_data$age_group, levels = ex_data$age_group)

# Create the plot by age group for ex (Life Expectancy) data
ggplot(ex_data, aes(x = age_group)) +
  geom_line(aes(y = hispanics, color = "Hispanics"), size = 1.5, group = 1) +  # Line for Hispanics
  geom_line(aes(y = whites, color = "NH Whites"), size = 1.5, group = 2) +     # Line for NH Whites
  geom_line(aes(y = blacks, color = "NH Blacks"), size = 1.5, group = 3) +     # Line for NH Blacks
  geom_point(aes(y = hispanics, color = "Hispanics"), size = 3) +   # Points for Hispanics
  geom_point(aes(y = whites, color = "NH Whites"), size = 3) +      # Points for NH Whites
  geom_point(aes(y = blacks, color = "NH Blacks"), size = 3) +      # Points for NH Blacks
  geom_hline(yintercept = 100, linetype = "dashed", color = "grey", size = 1) +  # Grey dashed line at 100 years
  geom_hline(yintercept = 0, linetype = "solid", color = "black", size = .25) +    # Solid black line at 0 years
  labs(title = "Life Expectancy \nfor Hispanics, NH Whites, and NH Blacks, United States, 2010",
       x = "Age Group", y = "Life Expectancy (Years)") +
  scale_color_manual(values = c("Hispanics" = "coral", "NH Whites" = "seagreen", "NH Blacks" = "deepskyblue")) +  # Updated colors
  theme_minimal() +
  theme(
    axis.text.x = element_text(angle = 45, hjust = 1),  # Rotate x-axis labels for readability
    legend.position = "bottom",                        # Move legend to bottom
    legend.title = element_blank(),                    # Remove legend title
    plot.title = element_text(size = 15, hjust = 0.5)  # Bigger title and centered
  )

dataLx <- read.csv("C:\\Users\\anami\\OneDrive\\Documents\\DEM\\Assignment6\\Lx.csv")
# Load necessary libraries
library(ggplot2)

# Ensure age_group is treated as a factor with correct levels in the proper order
age_group <- factor(c("<1", "1-4", "5-9", "10-14", "15-19", "20-24", "25-29", "30-34", 
                      "35-39", "40-44", "45-49", "50-54", "55-59", "60-64", "65-69", 
                      "70-74", "75-79", "80-84", "85+"), 
                    levels = c("<1", "1-4", "5-9", "10-14", "15-19", "20-24", 
                               "25-29", "30-34", "35-39", "40-44", "45-49", "50-54", 
                               "55-59", "60-64", "65-69", "70-74", "75-79", "80-84", "85+"))

# Create the data frame from the provided data
hispanics <- c(99509, 397617, 496649, 496366, 495451, 493495, 491103, 488641, 485900, 
               482350, 476929, 468507, 455727, 437343, 411927, 376460, 326655, 260472, 
               388677)

whites <- c(99481, 397474, 496414, 496059, 495066, 492759, 489567, 486072, 482058, 
            476828, 468998, 457260, 440660, 418212, 387658, 345641, 288752, 215995, 
            224787)

blacks <- c(98874, 394639, 492629, 492121, 490506, 486772, 481739, 476233, 470118, 
            462636, 451953, 435552, 410700, 376965, 334830, 283794, 223938, 158194, 
            167659)

# Combine data into a data frame
Lx_data <- data.frame(age_group, hispanics, whites, blacks)

# Create the plot
ggplot(Lx_data, aes(x = age_group)) +
  geom_line(aes(y = hispanics, color = "Hispanics"), size = 1.5, group = 1) +  # Plot for Hispanics
  geom_line(aes(y = whites, color = "Whites"), size = 1.5, group = 2) +       # Plot for Whites
  geom_line(aes(y = blacks, color = "Blacks"), size = 1.5, group = 3) +       # Plot for Blacks
  geom_point(aes(y = hispanics, color = "Hispanics"), size = 3) +  # Points for Hispanics
  geom_point(aes(y = whites, color = "Whites"), size = 3) +        # Points for Whites
  geom_point(aes(y = blacks, color = "Blacks"), size = 3) +        # Points for Blacks
  labs(title = "Person-Years Lived \nfor Hispanics, NH Whites, and NH Blacks, United States, 2010", 
       x = "Age Group", y = "Person-Years Lived") +           # Title in two lines
  scale_y_continuous(labels = scales::comma) +                     # Add comma format for large numbers
  scale_color_manual(values = c("Hispanics" = "orange", "Whites" = "seagreen", "Blacks" = "dodgerblue")) + 
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),  # Rotate x-axis labels
        legend.position = "bottom",                        # Move legend to the bottom
        legend.title = element_blank(),                    # Remove legend title
        plot.title = element_text(hjust = 0.5, size = 14)) # Center the title and adjust the size