sch*_*sie 5 time r ggplot2 chron
我有很多次,想要在一个条形图中绘制每次的频率
library(ggplot2)
library(chron)
test <- data.frame(times = c(rep("7:00:00",4), rep("8:00:00",3),
rep("12:00:00",1), rep("13:00:00",5)))
test$times <- times(test$times)
test
times
1 07:00:00
2 07:00:00
3 07:00:00
4 07:00:00
5 08:00:00
6 08:00:00
7 08:00:00
8 12:00:00
9 13:00:00
10 13:00:00
11 13:00:00
12 13:00:00
13 13:00:00
Run Code Online (Sandbox Code Playgroud)
binwidth选择值代表分钟
p <- ggplot(test, aes(x = times)) + geom_bar(binwidth=1/24/60)
p + scale_x_chron(format="%H:%M")
Run Code Online (Sandbox Code Playgroud)
如您所见,x轴上的刻度加一小时:
我觉得这与时区有关,但我真的不能这样做:
Sys.timezone()
[1] "CET"
Run Code Online (Sandbox Code Playgroud)
编辑:感谢@shadow的评论
更新:
如果我先跑,Sys.setenv(TZ='GMT')它会很完美.问题在于times()功能.我自动设置时区GMT,如果我正在绘制x轴,ggplot会注意到我的系统时区是CET并且在绘图上增加了一个小时.现在如果我将我的系统时区设置为GMT,ggplot不会增加一小时.
问题是times(...)假设时区是GMT,然后ggplot补偿您的实际时区.这是公平的:除非你指定时区,否则时间毫无意义.更大的问题是,似乎无法分辨times(...)实际时区是什么(如果其他人知道如何做到这一点,我很想知道).
解决方法是使用POSIXct并确定您的时区(我的是EST).
test <- data.frame(times = c(rep("7:00:00",4), rep("8:00:00",3),
rep("12:00:00",1), rep("13:00:00",5)))
test$times <- as.POSIXct(test$times,format="%H:%M:%S",tz="EST")
p <- ggplot(test, aes(x = times)) + geom_bar(binwidth=60,width=.01)
Run Code Online (Sandbox Code Playgroud)

binwidth=60 是60秒.
| 归档时间: |
|
| 查看次数: |
2019 次 |
| 最近记录: |