I have a dataset that includes two columns: CaptureTime and Count. The CaptureTime column includes timestamps of when the count was recorded, and the Count column includes the actual count at that time.
CaptureTime Count
2023/01/21 03:40:33 257692746
2023/01/21 03:41:03 258058244
2023/01/21 03:41:33 258410404
2023/01/21 03:42:03 258737938
2023/01/21 03:42:33 259092123
2023/01/21 03:43:03 259465642
2023/01/21 03:43:33 259832477
2023/01/21 03:44:03 260196953
2023/01/21 03:44:33 260570014
2023/01/21 03:45:03 260942799
2023/01/21 03:45:33 261301729
2023/01/21 03:46:03 261662278
2023/01/21 03:46:33 262018775
2023/01/21 03:47:03 262382558
2023/01/21 03:47:33 262736133
2023/01/21 03:48:03 263088786
2023/01/21 03:48:33 263437736
I am trying to use ggplot2 to draw a line graph that shows how the count changes over time. I want the x-axis to represent the CaptureTime and the y-axis to represent the Count. However, since there are so many values on the x-axis, I don't want any text to be displayed on it.
I have been trying to use ggplot2 to create this graph, but I am having trouble getting the graph to look the way I want it to. Can anyone help me with this?
Update
This is what I have now. It doesn't work as expected though.
d <- read.table(file = 'D:\\data.txt', sep = '\t', header = TRUE)
d
View(d)
head(d, 3)
d$CaptureTime <- as.Date(d$CaptureTime)
# d$CaptureTime <- ymd(d$CaptureTime, format = "%Y/%m/%d %H:%M:%S")
# d$CaptureTime <- as.POSIXct(d$CaptureTime, format = "%Y-%m-%d %H:%M:%S")
library(scales)
library(lubridate)
d %>%
ggplot(aes(CaptureTime, Count))
geom_point()
geom_line()
scale_x_date(date_labels = "%H:%M")
theme_bw()
labs(
title = "Count",
x = "CaptureTime",
y = "Count"
)
CodePudding user response:
I think you need to convert CaptureDate from a character vector to date-time format:
library(ggplot2)
ggplot(df, aes(lubridate::ymd_hms(CaptureTime), Count))
geom_line()
geom_point()
scale_x_datetime("Capture Time")
scale_y_continuous("Count", labels = scales::comma)
theme_minimal(base_size = 16)
Created on 2023-01-26 with reprex v2.0.2
Question data in reproducible format
df <- structure(list(CaptureTime = c("2023/01/21 03:40:33",
"2023/01/21 03:41:03",
"2023/01/21 03:41:33", "2023/01/21 03:42:03", "2023/01/21 03:42:33",
"2023/01/21 03:43:03", "2023/01/21 03:43:33", "2023/01/21 03:44:03",
"2023/01/21 03:44:33", "2023/01/21 03:45:03", "2023/01/21 03:45:33",
"2023/01/21 03:46:03", "2023/01/21 03:46:33", "2023/01/21 03:47:03",
"2023/01/21 03:47:33", "2023/01/21 03:48:03", "2023/01/21 03:48:33"
), Count = c(257692746L, 258058244L, 258410404L, 258737938L,
259092123L, 259465642L, 259832477L, 260196953L, 260570014L, 260942799L,
261301729L, 261662278L, 262018775L, 262382558L, 262736133L, 263088786L,
263437736L)), class = "data.frame", row.names = c(NA, -17L))
CodePudding user response:
This should work, realizing that you need to use a class which supports date/time and not only date.
library(dplyr)
library(ggplot2)
d <- tibble(
CaptureTime = c("2023/01/21 03:40:33", "2023/01/21 03:41:03", "2023/01/21 03:41:33", "2023/01/21 03:42:03"),
Count = c(257692746, 258058244, 258410404, 258737938)
)
d <- d %>%
mutate(CaptureTime = as.POSIXct(CaptureTime, "%Y/%m/%d %H:%M:%S", tz = ""))
d %>%
ggplot(aes(CaptureTime, Count))
geom_point()
geom_line()
scale_x_datetime(date_labels = "%H:%M")
theme_bw()
labs(
title = "Count",
x = "CaptureTime",
y = "Count"
)