我希望下图中的 x 轴从 06:00 开始,到 22:00 结束,每 4 小时休息一次。但是,我无法弄清楚以下内容。
a) 如何使x轴从06:00开始,06:00之前没有任何空白。
b) 如何使x轴在22:00结束,22:00之后没有任何空白。现在甚至没有显示 22:00
c) 如何每 4 小时休息一次。
d) 如何为 y 轴分配标签(目前只是 X4,列名称)。
我尝试了几件事,但没有成功。一些示例数据:
range <- seq(as.POSIXct("2015/4/18 06:00"),as.POSIXct("2015/4/18 22:00"),"mins")
df <- data.frame(matrix(nrow=length(range),ncol=4))
df[,1] <- c(1:length(range))
df[,2] <- 2*c(1:length(range))
df[,3] <- 3*c(1:length(range))
df[,4] <- range
Run Code Online (Sandbox Code Playgroud)
重塑:
library(reshape2)
df2 <- melt(df,id="X4")
Run Code Online (Sandbox Code Playgroud)
图形:
library(ggplot2)
ggplot(data=df2,aes(x=X4,y=value,color=variable)) + geom_line()+
scale_y_continuous(expand=c(0,0)) +
coord_cartesian(xlim=c(as.POSIXct("2015/4/18 06:00:00"),as.POSIXct("2015/4/18 22:00:00")))
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
这是一些应该对您有帮助的代码。使用 可以轻松完成此操作scale_x_datetime
。
## desired start and end points
st <- as.POSIXct("2015/4/18 06:00:00")
nd <- as.POSIXct("2015/4/18 22:00:00")
## display data for given time range
ggplot(data = df2, aes(x = X4, y = value, color = variable)) +
geom_line() +
scale_y_continuous("Some name", expand = c(0, 0)) +
scale_x_datetime("Some name", expand = c(0, 0), limits = c(st, nd),
breaks = seq(st, nd, "4 hours"),
labels = strftime(seq(st, nd, "4 hours"), "%H:%S"))
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1550 次 |
最近记录: |