R绘图功能可删除x轴上的小时数
我想在R中的简单绘图中显示以下数据帧:R绘图功能可删除x轴上的小时数,r,datetime,plot,axis,R,Datetime,Plot,Axis,我想在R中的简单绘图中显示以下数据帧: > df value dateTime 1 -0.8 2018-04-02 06:00:00 2 -0.7 2018-04-02 06:01:00 3 -0.6 2018-04-02 06:02:00 4 -0.7 2018-04-02 06:03:00 5 -0.5 2018-04-02 06:04:00 6 -0.9 2018-04-02 06:05:00 > plot(df$dateTime, df$value
> df
value dateTime
1 -0.8 2018-04-02 06:00:00
2 -0.7 2018-04-02 06:01:00
3 -0.6 2018-04-02 06:02:00
4 -0.7 2018-04-02 06:03:00
5 -0.5 2018-04-02 06:04:00
6 -0.9 2018-04-02 06:05:00
> plot(df$dateTime, df$value, type = "o", xlab = "Time", ylab = "Value", cex.axis = 1.5, cex.lab = 1.5, cex.main = 1.5, ylim=c(-1,1))
结果是:
如您所见,该图仅显示x轴上的分和秒。但是,我希望x轴上有小时和分钟。最好单独添加x轴。通过设置
xaxt=“n”
在plot()中禁用它。然后我们可以使用axis.POSIXct()
再次添加它,它有一个参数format
,可以指定日期和时间的表示方式。有关如何指定此格式,请参阅?strtime
df <- structure(list(value=c(-0.8, -0.7, -0.6, -0.7, -0.5, -0.9),
dateTime=structure(c(1522641600, 1522641660, 1522641720,
1522641780, 1522641840, 1522641900), class=c("POSIXct",
"POSIXt"), tzone="")), class="data.frame", row.names=c(NA,
-6L))
plot(df[,2:1], ylim=c(-1, 1), xaxt="n", type="o")
axis.POSIXct(1, df$dateTime, format="%H:%M")
df对不起,我之前的回答令人困惑。我认为ggplot2
可以:
library(tidyverse)
library(lubridate)
library(scales)
df<-tibble(
value = c(-0.8, -0.7, -0.6, -0.7, -0.5, -0.9),
dateTime = ymd_hms(
"2018-04-02 06:00:00", "2018-04-02 06:01:00",
"2018-04-02 06:02:00", "2018-04-02 06:03:00",
"2018-04-02 06:04:00", "2018-04-02 06:05:00") )
ggplot(data = df, aes(x = dateTime, y = value)) +
geom_line() + scale_x_datetime(breaks = date_breaks("1 min"),
labels = date_format("%H:%M"))
库(tidyverse)
图书馆(lubridate)
图书馆(比例尺)
您是否添加了库(刻度)
?