r - 使用 ggplot2 绘制 POSIXct 时间戳系列

标签 r ggplot2 posixct

我想绘制负载测试的结果。每行包含一个以秒为单位的时间戳和请求的响应时间。例如:

timestamp <- c(1441229131, 1441229132, 1441229133, 1441229134, 1441229135)
response.time <- c(22, 48, 48, 59, 52)
lt1 <- data.frame(timestamp, response.time)
lt1$datetime <- as.POSIXct(lt1$timestamp, origin="1970-01-01", tz="GMT")

但是,当我尝试绘制此图时,x 轴显示的是奇怪的数字,而不是格式正确的日期。
g1 <- ggplot(lt1, aes(datetime, response.time))
g1 <- g1 + geom_point()
g1 <- g1 + theme(axis.text.x=element_text(angle=90, hjust=1))
g1

enter image description here

最佳答案

您需要使用库 scales 在 x 轴上指定您想要的内容和函数 scale_x_datetime :

library(scales)
ggplot(lt1, aes(datetime, response.time)) +
       geom_point() +
       theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
       scale_x_datetime(labels = date_format("%H:%M:%S"))

enter image description here

欲了解更多信息,请参阅 ?scale_x_datetime , ?date_format?strptime

关于r - 使用 ggplot2 绘制 POSIXct 时间戳系列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32364018/

相关文章:

R:如何使用 ggplot2 的 stat_function 绘制gumbel 分布

r - 如何通过 POSIXct 将日期作为年/周转换为一周的第一天?

r - 如何创建POSIXct矩阵

r - 在 R 中将字符转换为 POSIXct 会丢失时区

r - 以整洁的方式匿名化数据框的选定列

r - 使用 tidyverse 和 purrr 进行双重嵌套

读取托管在 Google Drive 上的 csv 文件

r - 使用 awk 或 R 编辑文件,如果重复则更改值

r - ggplot2:如何以特定角度旋转图表?

r - 小于等于 ggplot2 轴标签