【发布时间】:2014-04-22 20:15:20
【问题描述】:
我正在绘制收到的消息数量与收到消息的时间。 例如,我的数据如下所示(我从 csv 中读取):
timestamp count
1398094330 286
1398094340 6279
1398094350 447
1398094360 946
1398094370 719
1398094380 171
1398094390 4
1398094400 42
1398094420 2
通常,数据会在一天中跨越多个小时,但有时会像上面的示例一样被限制在一个小时间窗口内。
因为数据通常跨越一天,我已将 scale_x_datetime 的休息时间设置为 "10 mins",这适用于我 99% 的用例。但是,在这种情况下它失败了。
我将失败定义为 ggplot 没有显示任何 x 轴刻度,因为我的所有数据都发生在这一天大约 1 分钟的窗口中,而我的休息时间为 10 分钟。
这显然没有用,因为您在查看生成的 ggplot 图表时不知道消息何时出现。
我的其他一切工作正常,但不知道如何在此示例中显示任何类型的 x 轴刻度线。
我研究过漂亮,但这似乎不适用于datetime。减少中断只会使所有其他图表看起来凌乱且难以辨认。
代码sn-p:
tmp = read.csv(args[1])
tmp$time = as.POSIXct(tmp$timestamp, "1970-1-1", tz="America/New_York")
p = ggplot()
p = p + geom_point(tmp, mapping=aes(x=time, y=count), size=1.5)
p = p + scale_y_log10()
p = p + scale_x_datetime(breaks=date_breaks("10 mins"), labels=date_format("%H:%M:%S"))
p = p + theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust=0.5))
p = p + labs(title=paste("Message Count, 10 Second Intervals \n", name))
输入:
structure(list(timestamp = c(1398094330L, 1398094340L, 1398094350L, 1398094360L, 1398094370L, 1398094380L, 1398094390L, 1398094400L, 1398094420L), count = c(286L, 6279L, 447L, 946L, 719L, 171L, 4L, 42L, 2L), time = structure(c(1398094330, 1398094340, 1398094350, 1398094360, 1398094370, 1398094380, 1398094390, 1398094400, 1398094420), class = c("POSIXct", "POSIXt"), tzone = "America/New_York")), .Names = c("timestamp", "count", "time"), row.names = c(NA, -9L), class = "data.frame")
【问题讨论】:
-
注意:我使用的是 R 版本 3.0.2
-
您的数据的
dput和您尝试过的一些“工作”代码会有所帮助。 -
将代码编辑到我的帖子中。不确定“dput”是什么/做什么