r-如何在带有时间戳的ggplot时间序列中添加更多刻度



我使用ggplot将时间序列可视化为折线图。但是,我希望在x轴上有更多的刻度。

时间戳的格式如下:2014-01-10 00:30:00

p3 <- ggplot(sub2, aes(x=Timestamp, y=Value)) +
geom_line( color="#2a7a3f") + 
xlab("Time")+
ylab("Chloride")

# Title
p3 + labs(title = "Chloride Concentration over Time") + theme_minimal() + theme(plot.title = element_text(hjust=0.5, size=11, face='bold')) 
``

您可以使用scales中的pretty_breaks来轻松更改中断次数(尽管它不是您指定的数字(

这样使用:

library(ggplot2)
df <- iris
df$rand_date <- sample(seq(as.POSIXct('2017/05/01'), as.POSIXct('2017/05/02'), by="1 mins"), 150)

ggplot(data = df, aes(x = rand_date, y = Sepal.Length)) +
geom_line() +
scale_x_datetime(breaks = scales::pretty_breaks(n=5),date_labels = "%Y-%m-%d %H:%M:%S")

n-参数控制将在绘图上获得的中断次数

最新更新