library(tidyverse)
library(scales)
library(plotly)
library(NHANES)
library(babynames)
library(viridis)651_HW_1
Excercise 1)
data(mpg)
non_interactive_plot1 <- ggplot(mpg, aes(x = cty, y = hwy, color = drv, shape = drv)) +
geom_point(size = 3) +
labs(title = "City vs. Highway MPG by Drive Train Type",
x = "City MPG",
y = "Highway MPG")
non_interactive_plot1Exercise 2)
height_summary <- NHANES %>%
group_by(AgeDecade) %>%
summarize(mean_height = mean(Height, na.rm = TRUE))
non_interactive_plot2 <- ggplot(height_summary, aes(x = AgeDecade, y = mean_height)) +
geom_bar(stat = "identity", fill = "skyblue") +
labs(title = "Average Height by Age Decade",
x = "Age Decade",
y = "Mean Height (cm)") +
geom_text(aes(label=round(mean_height,1)))+
theme_minimal()
non_interactive_plot2Exercise 3)
frank_data <- babynames %>%
filter(name == "Frank", sex == "M")
frank_plot <- ggplot(frank_data, aes(x = year, y = n)) +
geom_line(color = "blue") +
labs(title = "Frequency of Babies Born Named Frank",
x = "Year",
y = "Frequency of Babies") +
theme_minimal()
interactive_plot1 <- ggplotly(frank_plot)
interactive_plot1Exercise 4)
nhanes_data <- NHANES %>%
filter(!is.na(Height), !is.na(Weight), !is.na(AgeDecade))
interactive_plot2 <- nhanes_data %>%
plot_ly(x = ~Weight, y = ~Height, color = ~AgeDecade, colors = viridis(8), type = "scatter", mode = "markers",
marker = list(size = 10, opacity = 0.6)) %>%
layout(title = "Height vs. Weight Colored by Age Groups",
xaxis = list(title = "Weight in Kilograms"),
yaxis = list(title = "Height in Centimeters"),
colorway = viridis(8),
legend = list(title = list(text = 'Age Decade')))
interactive_plot2From the graph there appears to be a positive correlation in the upward trend between height and weight. This makes sense because taller individuals often times weigh more. The plot suggests that most individuals reach a stable height and weight range in early adulthood and maintain this range across different age groupings.