Home > OS >  How to add more ticks to a ggplot timeseries with timestamps
How to add more ticks to a ggplot timeseries with timestamps

Time:10-13

I visualized a timeseries as line graph by using ggplot. However, I would like to have more ticks on the x-axis.

The timestamp is the following format: 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')) 

``

CodePudding user response:

you can use the pretty_breaks from scales to easily alter the number of breaks (although it is not exactly the number you specify)

use it like this:

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")

the n-parameter controls the number of breaks you'll get on the plot

  • Related