I've been trying to plot a graph, but for some reason I keep having variables removed. I have a dataframe with 350 observations and 11 variables, but when I try plot my graph, 140 of the observations are removed.
I started off by modifying the dataframe in order to plot against time over two consecutive days:
library(hms)
library(dplyr)
library(ggplot2)
library(tidyr)
library(tidyverse)
#Generate sample data
df <- data.frame(hour = hms(sample(0:59, replace = TRUE, 350),
sample(0:59, replace = TRUE, 350),
sample(c(0:5, 20:23), replace = TRUE, 350)),
count = floor(runif(350, min=0, max=20)))
df <- df %>%
mutate(ai = count/10) %>%
mutate(graphing.date = if_else(
hour > parse_hms("12:00:00"), as.Date("2022-02-05"),
as.Date("2022-02-06")),
graphing.datetime = as.POSIXct(paste(graphing.date, hour)))
And then I use the variables graphing.datetime and ai as my x and y variables respectively:
p <- ggplot(df, aes(x = graphing.datetime, y = ai)) +
geom_point() +
scale_x_datetime("Time",
limits = c(as.POSIXct("2022-02-05 20:00:00"),
as.POSIXct("2022-02-06 06:00:00")),
date_breaks = "1 hours",
date_labels = "%H:%M")
p
When I do this, I get the following message:
Warning message: Removed 140 rows containing missing values (geom_point).
What can I do to fix this? is there anything wrong with my code that I need to fix?