I have a for loop that produces a set of graphs that are all the same but for different locations.
I want to be able to control the tick marks so that the the distance between each tick mark is the same for each plot. I don't want hard set limits as each graph is too different. I'd like the interval distance between each tick to be the same, so the graphs are more comparable. i.e. I'd like all the y axes to be relative to one another.
NOTE: I don't want them all the have the same axes, I just want them to be relative. The distance between two ticks is the same on each plot.
j = 1 # counter for plot title and index
for (datTime in chapel){
plotName <- names(chapel)[j]
j = j+1
datTime <- datTime %>%
filter((DateTime > as.POSIXct("2020-03-05 00:00:00")) & (DateTime < as.POSIXct("2023-03-04 00:00:00")))
plot <-
datTime %>%
ggplot(aes(x=DateTime)) +
geom_point(aes(y=mAOD), col='grey') +
geom_smooth(aes(y=mAOD), col='black', se=FALSE, span=0.1, method="loess") +
geom_hline(yintercept=datTime$groundLevel, col='dark green') +
theme_classic() +
labs(y='Water Depth (mAOD)', x=NULL) +
ggtitle(plotTitles[[plotName]][1]) +
scale_x_datetime(
breaks=seq(update(min(datTime$DateTime), month = 1, day = 1), max(datTime$DateTime),
by= "1 year"), date_labels="%Y") +
scale_y_continuous(labels=scaleFUN, limits = c(81.5, 84.8)) +
theme(text=element_text(size=20, family='Calibri Light')) +
theme(plot.margin = unit(c(1, 1, 1, 1), 'cm')) +
theme(axis.title.y=element_text(margin=margin(t=0, r=20, b=0, l=0))) +
theme(axis.title.x=element_text(margin=margin(t=20, r=0, b=0, l=0))) +
theme(axis.title.y.right=element_text(margin=margin(t=0, r=0, b=0, l=20)))
plotlist_controlSites[[plotName]] <- plot
}
The data frames each have a datetime column and a mAOD column (all values vary between 81 and 84). There are 10 plots in total.
This is the top 10 lines of the first dataframe in the list "chapel":
structure(list(DateTime = structure(c(1576022400, 1576108800,
1576195200, 1576281600, 1576368000, 1576454400), class = c("POSIXct",
"POSIXt"), tzone = "GMT"), mAOD = c(NA, 82.4402870833333, 82.5335010416667,
82.5776004166667, 82.5414891666667, 82.505000625)), row.names = c(447L,
488L, 529L, 570L, 611L, 652L), class = "data.frame")
I managed to solve this by finding the max range of the data on the y axis. Then Setting the ylims to the minimum y value and the min + the largest range in the data. Now all the y axis are relative to one another and plots can be compared visually.
I included this in the for loop above the plot
min <- min(datTime$mAOD, na.rm=T)
max <- (min + 1.8)
top <- max(datTime$mAOD, na.rm=T)
range <- (top - min)
print(range)
And added in this to the ggplot.
scale_y_continuous(labels=scaleFUN, breaks=seq(70, 90, by=0.5), limits = c(min, max))
Maybe a slight hack, but it did the job.