【发布时间】:2018-07-20 20:25:36
【问题描述】:
我正在尝试将数据点的图表作为日期经过的时间的函数放在一起,但问题是我的日期字符串大小的数据点太多,如下图所示。
我希望 X 轴只显示 %Y-%m-%d 而不是完整的日期和时间,但我似乎无法获得 scale_x_date、scale_x_datetime、@ 987654327@,或xmin 和xmax 工作。
我遇到的错误:
Error: Invalid input: time_trans works with objects of class POSIXct only
Error: Invalid input: date_trans works with objects of class Date only
到目前为止我的代码(已注释掉失败):
library(ggplot2)
library(scales)
mydata <- read.csv("/Users/user/R/restore_graphs/CSV/store.csv.tmp")
restore.df = data.frame(
Time = mydata$start,
Duration = mydata$time,
Labels = gsub(" [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}","",mydata$start)
)
p <- ggplot(restore.df, aes(x=Time,y=Duration)) + geom_point(colour="red")
#p <- ggplot(restore.df, aes(x=Time,y=Duration)) + geom_point(colour="red") + scale_x_datetime(date_labels = "%Y-%m-%d %H")
#p + scale_x_date(date_labels = "%y-%m-%d", limits = as.Date('2018-06-14', "%y-%m-%d"), as.Date('2018-06-20', "%Y-%m-%d"))
#, xlim(as.Date('2018-06-14', "%Y-%m-%d"), as.Date('2018-06-20', "%Y-%m-%d")))) + geom_point(colour="red")# + xlim(as.Date('2018-06-14', "%Y-%m-%d"), as.Date('2018-06-20', "%Y-%m-%d"))
#aes(xmin = as.Date("2018-06-14", "%Y-%m-%d"), xmax = as.Date("2018-06-20", "%y-%m-%d"))) #
dput(restore.df$Time)
print(p)
当我运行 ggplot 更改为的行时:
p <- ggplot(restore.df, aes(x=Time,y=Duration,xmin = as.Date("2018-06-14", "%Y-%m-%d"), xmax = as.Date("2018-06-20", "%y-%m-%d"))) + geom_point(colour="red")
它将图表更改为将每个点推到屏幕左侧。
样本数据:
uuid,db,table,start,stop,time,size
941439639,test,,"2018-06-14 17:35:07","2018-06-14 17:35:07",62.9666666666667,141329782065
890252165,test,,"2018-06-14 23:35:38","2018-06-14 23:35:38",61.7166666666667,141380294237
943883747,test,,"2018-06-15 05:38:39","2018-06-15 05:38:39",77.7666666666667,141469254934
827384296,test,,"2018-06-15 11:35:11","2018-06-15 11:35:11",63.4166666666667,141276941916
454468935,test,,"2018-06-15 17:35:23","2018-06-15 17:35:23",64.4333333333333,141380122325
705894402,test,,"2018-06-15 23:35:29","2018-06-15 23:35:29",63.9,141715941073
396694772,test,,"2018-06-16 05:39:59","2018-06-16 05:39:59",75.0666666666667,141789270192
【问题讨论】:
-
您没有将日期时间转换为日期时间。默认情况下,这些被读取为因素。要转换,您可以执行
Time = as.POSIXct(mydata$start)之类的操作。 -
完美解决了,谢谢。