Assignment 6

Author

Emma Wang

library(dslabs)
library(ggplot2)
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
library(stringr)
head(reported_heights)
           time_stamp    sex height
1 2014-09-02 13:40:36   Male     75
2 2014-09-02 13:46:59   Male     70
3 2014-09-02 13:59:20   Male     68
4 2014-09-02 14:51:53   Male     74
5 2014-09-02 15:16:15   Male     61
6 2014-09-02 15:16:16 Female     65
clean_height = reported_heights |> 
  filter(!str_detect(height, "'")) |> 
  filter(!str_detect(height, "inches")) |> 
  filter(!str_detect(height, "cm")) 
clean_height = clean_height |> 
  mutate(height = as.numeric(height)) |> 
  filter(height > 50 & height <85)
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `height = as.numeric(height)`.
Caused by warning:
! NAs introduced by coercion
height_plot = ggplot(clean_height, aes(x = time_stamp, y = height, color = sex, text = sex)) +
  geom_point(size = 2.4, alpha = 0.5) +
  labs (
    title = "Comparison of Self-Reported Height between Males and Females", 
    x = "Year Reported (2014 - 2017)", 
    y = "Height (in inches)", 
    caption = "Dataset sourced from DS Labs"
  ) +
  theme_minimal(base_size = 10, base_family = "mono") +
  scale_color_manual(values = c("pink", "darkblue"))
height_plot

inter = ggplotly(height_plot)
inter

I utilized the reported_height dataset included in the “dslabs” package. The dataset included the following variables:

Time_stamp: the exact date and time in which the height was self-reported.

Sex: gender of the reported (either male or female)

Height: the actual self-reported height.

The height entries varied, as they were self-reported. Some reported using feet and inches, some only in inches, some fully spelled out. Since the data varied, I needed to clean it first utilizing the filter and str_detect functions. I then mutated it into numerical data and filtered it once again to only include heights above 50 and below 85 inches. I created my graph using the time_stamps as my x axis, and height as my y-axis. I utilized geom_point() and colored the points by sex. I then made the graph interactive employing ggplotly to allow the time stamp, sex, and height of each point to appear when hovering over the point.