I apologize if this is a messy question, but I don't know any better way to format it. I'm trying to get it so on the x-axis of my graph, all of the dates I have in my dataset are shown. Right now, it's just about half of them - I'll attach a picture of my graph. I would also like if there were more tick marks between each date. I used the lubridate() function to combine my date/time columns, so that's not an issue. I was wondering if there was a way to manipulate the axis tick marks even though these aren't typical numerical values. I'll attach my code below.
PS4T1$newdate <- with(PS4T1, as.POSIXct(paste(date, time), format="%m-%d-%Y %H:%M"))
plot(average ~ newdate, data=PS4T1, type="b", col="blue")
CodePudding user response:
First, format date as.POSIXct
, this is important for which plot
method is called, apparently you already have done that.
dat <- transform(dat, date=as.POSIXct(date))
Then, subset on the substr
ings where hours are e.g. '00'
. Next plot without x-axis and build custom axis using axis
and mtext
.
st <- substr(dat$date, 12, 13) == '00'
plot(dat, type='b', col='blue', xaxt='n')
axis(1, dat$date[st], labels=F)
mtext(strftime(dat$date[st], '%b %d'), 1, 1, at=dat$date[st])
Data:
set.seed(42)
dat <- data.frame(
date=as.character(seq.POSIXt(as.POSIXct('2021-06-22'), as.POSIXct('2021-06-29'), 'hour')),
v=runif(169)
)