I operate groundwater monitoring sensors for my job. They take a reading every 15 minutes. I pull the data once a month and add it to a running csv file I use to graph in RStudio.
I am having trouble with the labels along the X-axis taking up too much space. I am unsure how to make them smaller so that they do not overlap with the X-axis title, "Date/Time".
Here is the block of code that I am using. I am unsure of what code to add to edit the X-axis labels so that they do not overlap with the "Date/Time" title:
econ_15<- read.csv("econ_15_results.csv")
#check the class of my variables
class(econ_15$date.time)
head(econ_15)
#convert date/time format to one better for graphing
econ_15$date.time <- as.POSIXct(econ_15$date.time, format = "%m/%d/%Y %H:%M")
#plot econn NO3 data
plot(econ_15$date.time, econ_15$no3.n,
type = "o",
col = "blue",
xlab = "Date/Time",
ylab = "NO3-N concentration (mg/L)",
main = "Econ 15 NO3-N 09/02/2024 - 2/27/25",
cex = 0.25,
xaxt = "n",
grid = FALSE)
#Determine how many ticks there will be on the x-axis
time_ticks <- seq(from = min(econ_15$date.time),
to = max(econ_15$date.time),
by = "24 hours")
#convert the date/time variables to a readable format to graph
axis.POSIXct(1, at=time_ticks, format ="%m-%d-%Y %H:%M", las = 2,cex.axis = 0.7)
#Add grid lines
abline(h = pretty(range(econ_15$no3.n)), col = "lightgray", lty = 2) # Horizontal grid lines
abline(v = time_ticks, col = "lightgray", lty = 2) # Vertical grid lines
Here is a dput of a subset of my data for reproducibility:
nitrate_struc <-
structure(list(date.time = c("8/29/2024 10:00", "8/29/2024 10:15",
"8/29/2024 10:30", "8/29/2024 10:45", "8/29/2024 11:00", "8/29/2024 11:15",
"8/29/2024 11:30", "8/29/2024 11:45", "8/29/2024 12:00", "8/29/2024 12:15",
"8/29/2024 12:30", "8/29/2024 12:45", "8/29/2024 13:00", "8/29/2024 13:15",
"8/29/2024 13:30", "8/29/2024 13:45", "8/29/2024 14:00", "8/29/2024 14:15",
"8/29/2024 14:30", "8/29/2024 14:45", "8/29/2024 15:00", "8/29/2024 15:15",
"8/29/2024 15:30", "8/29/2024 15:45", "8/29/2024 16:00", "8/29/2024 16:15",
"8/29/2024 16:30", "8/29/2024 16:45", "8/29/2024 17:00", "8/29/2024 17:15",
"8/29/2024 17:30", "8/29/2024 17:45", "8/29/2024 18:00", "8/29/2024 18:15",
"8/29/2024 18:30", "8/29/2024 18:45", "8/29/2024 19:00", "8/29/2024 19:15",
"8/29/2024 19:30", "8/29/2024 19:45", "8/29/2024 20:00", "8/29/2024 20:15",
"8/29/2024 20:30", "8/29/2024 20:45", "8/29/2024 21:00", "8/29/2024 21:15",
"8/29/2024 21:30", "8/29/2024 21:45", "8/29/2024 22:00", "8/29/2024 22:15",
"8/29/2024 22:30", "8/29/2024 22:45", "8/29/2024 23:00", "8/29/2024 23:15",
"8/29/2024 23:30", "8/29/2024 23:45", "8/30/2024 0:00", "8/30/2024 0:15",
"8/30/2024 0:30", "8/30/2024 0:45", "8/30/2024 1:00", "8/30/2024 1:15",
"8/30/2024 1:30", "8/30/2024 1:45", "8/30/2024 2:00", "8/30/2024 2:15",
"8/30/2024 2:30", "8/30/2024 2:45", "8/30/2024 3:00", "8/30/2024 3:15",
"8/30/2024 3:30", "8/30/2024 3:45", "8/30/2024 4:00", "8/30/2024 4:15",
"8/30/2024 4:30", "8/30/2024 4:45", "8/30/2024 5:00", "8/30/2024 5:15",
"8/30/2024 5:30", "8/30/2024 5:45", "8/30/2024 6:00", "8/30/2024 6:15",
"8/30/2024 6:30", "8/30/2024 6:45", "8/30/2024 7:00", "8/30/2024 7:15",
"8/30/2024 7:30", "8/30/2024 7:45", "8/30/2024 8:00", "8/30/2024 8:15",
"8/30/2024 8:30", "8/30/2024 8:45", "8/30/2024 9:00", "8/30/2024 9:15",
"8/30/2024 9:30", "8/30/2024 9:45", "8/30/2024 10:00", "8/30/2024 10:15",
"8/30/2024 10:30", "8/30/2024 10:45", "8/30/2024 11:00", "8/30/2024 11:15",
"8/30/2024 11:30", "8/30/2024 11:45", "8/30/2024 12:00", "8/30/2024 12:15",
"8/30/2024 12:30", "8/30/2024 12:45", "8/30/2024 13:00", "8/30/2024 13:15",
"8/30/2024 13:30", "8/30/2024 13:45", "8/30/2024 14:00", "8/30/2024 14:15",
"8/30/2024 14:30", "8/30/2024 14:45", "8/30/2024 15:00", "8/30/2024 15:15",
"8/30/2024 15:30", "8/30/2024 15:45", "8/30/2024 16:00", "8/30/2024 16:15",
"8/30/2024 16:30", "8/30/2024 16:45", "8/30/2024 17:00", "8/30/2024 17:15",
"8/30/2024 17:30", "8/30/2024 17:45", "8/30/2024 18:00", "8/30/2024 18:15",
"8/30/2024 18:30", "8/30/2024 18:45", "8/30/2024 19:00", "8/30/2024 19:15",
"8/30/2024 19:30", "8/30/2024 19:45", "8/30/2024 20:00", "8/30/2024 20:15",
"8/30/2024 20:30", "8/30/2024 20:45", "8/30/2024 21:00", "8/30/2024 21:15",
"8/30/2024 21:30", "8/30/2024 21:45", "8/30/2024 22:00", "8/30/2024 22:15",
"8/30/2024 22:30", "8/30/2024 22:45", "8/30/2024 23:00", "8/30/2024 23:15",
"8/30/2024 23:30", "8/30/2024 23:45", "8/31/2024 0:00", "8/31/2024 0:15",
"8/31/2024 0:30", "8/31/2024 0:45", "8/31/2024 1:00", "8/31/2024 1:15",
"8/31/2024 1:30", "8/31/2024 1:45", "8/31/2024 2:00", "8/31/2024 2:15",
"8/31/2024 2:30", "8/31/2024 2:45", "8/31/2024 3:00", "8/31/2024 3:15",
"8/31/2024 3:30", "8/31/2024 3:45", "8/31/2024 4:00", "8/31/2024 4:15",
"8/31/2024 4:30", "8/31/2024 4:45", "8/31/2024 5:00", "8/31/2024 5:15",
"8/31/2024 5:30", "8/31/2024 5:45", "8/31/2024 6:00", "8/31/2024 6:15",
"8/31/2024 6:30", "8/31/2024 6:45", "8/31/2024 7:00", "8/31/2024 7:15",
"8/31/2024 7:30", "8/31/2024 7:45", "8/31/2024 8:00", "8/31/2024 8:15",
"8/31/2024 8:30", "8/31/2024 8:45", "8/31/2024 9:00", "8/31/2024 9:15",
"8/31/2024 9:30", "8/31/2024 9:45", "8/31/2024 10:00", "8/31/2024 10:15",
"8/31/2024 10:30", "8/31/2024 10:45", "8/31/2024 11:00", "8/31/2024 11:15",
"8/31/2024 11:30", "8/31/2024 11:45", "8/31/2024 12:00", "8/31/2024 12:15",
"8/31/2024 12:30", "8/31/2024 12:45", "8/31/2024 13:00", "8/31/2024 13:15",
"8/31/2024 13:30", "8/31/2024 13:45", "8/31/2024 14:00", "8/31/2024 14:15",
"8/31/2024 14:30", "8/31/2024 14:45", "8/31/2024 15:00", "8/31/2024 15:15",
"8/31/2024 15:30", "8/31/2024 15:45", "8/31/2024 16:00", "8/31/2024 16:15",
"8/31/2024 16:30", "8/31/2024 16:45", "8/31/2024 17:00", "8/31/2024 17:15",
"8/31/2024 17:30", "8/31/2024 17:45", "8/31/2024 18:00", "8/31/2024 18:15",
"8/31/2024 18:30", "8/31/2024 18:45", "8/31/2024 19:00", "8/31/2024 19:15",
"8/31/2024 19:30", "8/31/2024 19:45", "8/31/2024 20:00", "8/31/2024 20:15",
"8/31/2024 20:30", "8/31/2024 20:45", "8/31/2024 21:00", "8/31/2024 21:15",
"8/31/2024 21:30", "8/31/2024 21:45", "8/31/2024 22:00", "8/31/2024 22:15",
"8/31/2024 22:30", "8/31/2024 22:45", "8/31/2024 23:00", "8/31/2024 23:15",
"8/31/2024 23:30", "8/31/2024 23:45", "9/1/2024 0:00", "9/1/2024 0:15",
"9/1/2024 0:30", "9/1/2024 0:45", "9/1/2024 1:00", "9/1/2024 1:15",
"9/1/2024 1:30", "9/1/2024 1:45", "9/1/2024 2:00", "9/1/2024 2:15",
"9/1/2024 2:30", "9/1/2024 2:45", "9/1/2024 3:00", "9/1/2024 3:15",
"9/1/2024 3:30", "9/1/2024 3:45", "9/1/2024 4:00", "9/1/2024 4:15",
"9/1/2024 4:30", "9/1/2024 4:45", "9/1/2024 5:00", "9/1/2024 5:15",
"9/1/2024 5:30", "9/1/2024 5:45", "9/1/2024 6:00", "9/1/2024 6:15",
"9/1/2024 6:30", "9/1/2024 6:45", "9/1/2024 7:00", "9/1/2024 7:15",
"9/1/2024 7:30", "9/1/2024 7:45", "9/1/2024 8:00", "9/1/2024 8:15",
"9/1/2024 8:30", "9/1/2024 8:45", "9/1/2024 9:00", "9/1/2024 9:15",
"9/1/2024 9:30", "9/1/2024 9:45", "9/1/2024 10:00", "9/1/2024 10:15",
"9/1/2024 10:30", "9/1/2024 10:45", "9/1/2024 11:00", "9/1/2024 11:15",
"9/1/2024 11:30", "9/1/2024 11:45", "9/1/2024 12:00", "9/1/2024 12:15",
"9/1/2024 12:30", "9/1/2024 12:45", "9/1/2024 13:00", "9/1/2024 13:15",
"9/1/2024 13:30", "9/1/2024 13:45", "9/1/2024 14:00", "9/1/2024 14:15",
"9/1/2024 14:30", "9/1/2024 14:45", "9/1/2024 15:00", "9/1/2024 15:15",
"9/1/2024 15:30", "9/1/2024 15:45", "9/1/2024 16:00", "9/1/2024 16:15",
"9/1/2024 16:30", "9/1/2024 16:45", "9/1/2024 17:00", "9/1/2024 17:15",
"9/1/2024 17:30", "9/1/2024 17:45", "9/1/2024 18:00", "9/1/2024 18:15",
"9/1/2024 18:30", "9/1/2024 18:45", "9/1/2024 19:00", "9/1/2024 19:15",
"9/1/2024 19:30", "9/1/2024 19:45", "9/1/2024 20:00", "9/1/2024 20:15",
"9/1/2024 20:30", "9/1/2024 20:45", "9/1/2024 21:00", "9/1/2024 21:15",
"9/1/2024 21:30", "9/1/2024 21:45", "9/1/2024 22:00", "9/1/2024 22:15",
"9/1/2024 22:30", "9/1/2024 22:45", "9/1/2024 23:00", "9/1/2024 23:15",
"9/1/2024 23:30", "9/1/2024 23:45", "9/2/2024 0:00", "9/2/2024 0:15",
"9/2/2024 0:30", "9/2/2024 0:45", "9/2/2024 1:00", "9/2/2024 1:15",
"9/2/2024 1:30", "9/2/2024 1:45", "9/2/2024 2:00", "9/2/2024 2:15",
"9/2/2024 2:30", "9/2/2024 2:45", "9/2/2024 3:00", "9/2/2024 3:15",
"9/2/2024 3:30", "9/2/2024 3:45", "9/2/2024 4:00", "9/2/2024 4:15",
"9/2/2024 4:30", "9/2/2024 4:45", "9/2/2024 5:00", "9/2/2024 5:15",
"9/2/2024 5:30", "9/2/2024 5:45", "9/2/2024 6:00", "9/2/2024 6:15",
"9/2/2024 6:30", "9/2/2024 6:45", "9/2/2024 7:00", "9/2/2024 7:15",
"9/2/2024 7:30", "9/2/2024 7:45", "9/2/2024 8:00", "9/2/2024 8:15",
"9/2/2024 8:30", "9/2/2024 8:45", "9/2/2024 9:00", "9/2/2024 9:15",
"9/2/2024 9:30", "9/2/2024 9:45", "9/2/2024 10:00", "9/2/2024 10:15",
"9/2/2024 10:30", "9/2/2024 10:45", "9/2/2024 11:00", "9/2/2024 11:15",
"9/2/2024 11:30", "9/2/2024 11:45", "9/2/2024 12:00", "9/2/2024 12:15",
"9/2/2024 12:30", "9/2/2024 12:45", "9/2/2024 13:00", "9/2/2024 13:15",
"9/2/2024 13:30", "9/2/2024 13:45"), no3.n = c(5.39, 4.37, 4.1,
4.03, 3.96, 5.2, 3.61, 5.14, 4.64, 4.64, 4.31, 3.89, 4.82, 6.22,
5.43, 5.71, 4.08, 4.12, 3.81, 3.8, 5.77, 4.28, 5.26, 4.02, 4.2,
3.99, 4.73, 4.38, 3.51, 3.34, 3.77, 4.51, 3.6, 5.1, 4.12, 4.03,
3.67, 4.39, 3.4, 4.2, 4.45, 4.35, 4.37, 3.43, 4.45, 5.18, 4.44,
4.15, 4.07, 4.66, 3.97, 3.99, 5.08, 4.47, 4.53, 4.86, 3.23, 3.93,
4.49, 4.05, 3.06, 3.95, 3.77, 4.61, 4.66, 4.48, 3.72, 4.02, 3.48,
4.6, 3.85, 3.67, 4.27, 5.17, 4.36, 3.93, 3.54, 4.36, 4.84, 4.08,
3.85, 4.78, 3.99, 3.83, 4.42, 4.59, 4.48, 4.29, 4.34, 4.37, 4.28,
4.3, 4.16, 4.79, 4.43, 3.71, 4.51, 4.15, 3.86, 5.23, 4.16, 4.57,
3.95, 4.04, 4.32, 3.75, 3.79, 3.95, 3.69, 4.28, 4.38, 5.34, 4.44,
3.66, 4.42, 4.56, 4.67, 4.82, 3.9, 4.43, 3.74, 4.26, 3.94, 4.71,
4.08, 4.14, 4.84, 4.63, 5.03, 4.28, 4.64, 4.49, 4.17, 3.81, 4.81,
4.26, 4.49, 4.33, 4.97, 4.6, 4.52, 4.11, 5.13, 3.38, 4.15, 4.43,
4.48, 4.85, 5.31, 4.33, 4.69, 4.65, 4.95, 4.68, 4.27, 5.16, 4.35,
4.46, 4.6, 4.46, 4.85, 4.35, 4.82, 5.13, 5.02, 4.4, 5.32, 4.4,
4.52, 4.16, 4.53, 4.43, 4.3, 3.75, 4.53, 4.59, 4.39, 4.46, 4.67,
4.59, 3.7, 5.18, 4.83, 4.68, 4.92, 5.18, 4.89, 4.44, 4.56, 4.45,
4.12, 4.22, 4.74, 5.14, 4.61, 4.17, 4.58, 4.68, 4.53, 5.3, 4.73,
4.69, 5.12, 4.43, 4.32, 4.29, 5.23, 4.73, 5.05, 4.5, 4.39, 4.6,
4.49, 4.71, 5.15, 5.39, 5.87, 5.23, 4.83, 4.2, 5.74, 4.31, 5.16,
4.74, 4.84, 5.42, 4.34, 4.61, 4.54, 4.68, 4.78, 4.32, 4.83, 5.14,
4.59, 4.74, 4.29, 5.55, 5.06, 4.32, 4.53, 4, 3.68, 3.36, 3.43,
3.29, 4.31, 4.28, 5.19, 5.34, 4.95, 5.54, 5.41, 4.94, 4.81, 4.72,
5.26, 5.1, 5.12, 4.42, 4.79, 4.97, 4.99, 5.66, 4.53, 5.35, 5.18,
4.83, 4.86, 4.91, 5.51, 5.55, 5.09, 5.21, 4.95, 4.41, 5.73, 4.36,
5.21, 5.46, 5.37, 4.91, 5.58, 5.03, 5.05, 4.8, 5.29, 5.05, 5.37,
4.49, 4.69, 5.25, 5.33, 5.83, 4.7, 6.19, 5.08, 4.69, 5.9, 4.88,
5.32, 5.34, 6.39, 5.23, 4.92, 5.41, 5.66, 4.72, 5.32, 4.91, 4.73,
5.39, 5.27, 5.07, 5.94, 4.46, 4.42, 4.71, 5.49, 5.2, 5.06, 4.4,
4.38, 4.24, 4.82, 5.1, 5.61, 4.3, 4.78, 6.02, 4.34, 4.87, 4.27,
5.15, 4.91, 4.72, 5.21, 4.8, 5.23, 5.36, 5.7, 4.93, 5.42, 5,
5.62, 5.8, 4.66, 4.68, 4.72, 5.05, 5.14, 5.51, 5.56, 4.93, 6.2,
4.99, 5.05, 6.09, 5.25, 4.65, 5.49, 4.93, 5.2, 5.17, 5.22, 4.69,
5.42, 5.32, 5.19, 4.67, 5.13, 4.45, 4.47, 5.09, 4.8, 4.36, 4.82,
5.01, 5.3, 4.92, 4.64, 4.83, 5.01, 5.04, 5.22, 5.03, 5.35, 5.6,
5.05, 5.09, 5.3, 4.68, 5.09, 4.41, 4.52, 4.64, 4.67, 5.37, 4.38,
4.91)), row.names = c(NA, 400L), class = "data.frame")
**Note: Because I am on my work computer, and they are very strict with what I download onto the machine, I am unable to download and install packages in R/RStudio. I am working with the base functionality of R to do this work. It was difficult enough to get R approved and am unlikely to get additional package downloads approved as well. **
You may want to increase the lower mar
gin in par
ameters. To get the x-axis lab down, use an empty string in plot()
, then create it using mtext()
. I recommend doing the abline
s in panel.first
so they'll hide behind the graph.
op <- par(mar=c(10, 4, 4, 2) + 0.1) ## edit pars
plot(econ_15$date.time, econ_15$no3.n, type="o", col="blue", xlab="",
ylab="NO3-N concentration (mg/L)", main="Econ 15 NO3-N 09/02/2024 - 2/27/25",
cex=0.25, xaxt="n",
panel.first={
abline(h=pretty(range(econ_15$no3.n)), col="lightgray", lty=2)
abline(v=time_ticks, col="lightgray", lty=2)
})
mtext('Date/Time', 1, 8.5)
time_ticks <- seq(from=min(econ_15$date.time),
to=max(econ_15$date.time),
by="24 hours")
axis.POSIXct(1, at=time_ticks, format ="%m-%d-%Y %H:%M", las=2, cex.axis=1)
par(op) ## restore pars
You could also consider text()
for creating the x-tick labels, there's an option srt=45
to give them a 45° angle.