gpt4 book ai didi

r - 无法使用 scales 包格式化 xaxis

转载 作者:行者123 更新时间:2023-12-04 11:18:17 24 4
gpt4 key购买 nike

我有这些数据并尝试使用 ggplot2 和比例来格式化 x 轴,具有日期和时间。

dput(head(pp))
structure(list(DateTime = structure(c(1362405600, 1362409200,
1362412800, 1362416400, 1363006800, 1363010400), class = c("POSIXct",
"POSIXt"), tzone = ""), Day = structure(c(1L, 1L, 1L, 1L, 1L,
1L), .Label = c("Fri", "Mon", "Sat", "Sun", "Thu", "Tue", "Wed"
), class = "factor"), Total_Logins = c(818832L, 801771L, 787508L,
731672L, 748872L, 727140L), Unique_Logins = c(732152L, 713380L,
701348L, 647321L, 672848L, 649453L), Date = structure(c(15768,
15768, 15768, 15768, 15775, 15775), class = "Date")), .Names = c("DateTime",
"Day", "Total_Logins", "Unique_Logins", "Date"), row.names = c(1498L,
1499L, 1500L, 1501L, 1666L, 1667L), class = "data.frame")

library(ggplot2)
library(scales)

ggplot(subset(pp, Day=="Fri"), aes(DateTime, Total_Logins, group=1)) +
geom_line() +
geom_smooth(method="loess", se=T, size=1) +
scale_x_date(breaks = "1 day", labels=date_format("%b-%d-%Y %H"))

我收到这个错误:

Error: Invalid input: date_trans works with objects of class Date only

我的 DateTime 列已经是日期格式了。任何想法,我在这里做错了什么?

enter image description here

最佳答案

由于 DateTime 列同时包含日期和时间(类 POSIXct)并且您还想显示 x 轴标签的小时,您应该使用 scale_x_datetime() 而不是 scale_x_date().

+scale_x_datetime(breaks = "1 day", labels=date_format("%b-%d-%Y %H"))

要更改显示 x 轴刻度的休息时间,您可以将 breaks="1 day" 更改为例如 breaks="6 hours"。另一种可能性是仅在实际值可用时显示中断。如果轴文本项目太多,那么您可以更改文本的方向。

+scale_x_datetime(breaks = unique(pp$DateTime), labels=date_format("%b-%d-%Y %H"))+
theme(axis.text.x=element_text(angle=90,vjust=0.5))

关于r - 无法使用 scales 包格式化 xaxis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16041090/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com