Im trying to change the background color of a graph. My x axis is the day time in format POSIXct and it have measures each 5 minutes. In the Y-axis I have numeric data of clorophyll (chla) measures and I have added also added solar radiation data (PARuw) using PAR(new = TRUE). Those 3 variables are in a data frame named Spring. This is my plot:
par(mfrow=c(1, 1), mar=c(2, 3, 1, 3))
plot(Spring$date_time, Spring$chla, xlab="", ylab="", type="n",
ylim=c(0, 6), las=1)
points(Spring$date_time, Spring$chla, pch=21, col="forestgreen",
bg="forestgreen", cex=.8)
lines(Spring$date_time, Spring$chla, col="forestgreen", lwd=5)
par(new=TRUE)
plot(Spring$date_time, Spring$PARuw, xlab="", ylab="", xaxt="n",
type="n", yaxt="n")
points(Spring$date_time, Spring$PARuw, pch=21, col="yellow2",
bg="yellow2", cex=.8)
lines(Spring$date_time, Spring$PARuw, col="yellow2", lwd=5)
axis(side=4, at=seq(0, 1200, by=200), labels=seq(0, 1200, by=200), las=1)
I add head and tail of the data frame:
date_time PARuw chla
1 2023-04-23 02:00:00 1.954 4.735
2 2023-04-23 02:05:00 1.971 4.769
3 2023-04-23 02:10:00 1.736 4.671
4 2023-04-23 02:15:00 1.586 4.616
5 2023-04-23 02:20:00 1.566 4.912
6 2023-04-23 02:25:00 1.686 4.615
date_time PARuw chla
283 2023-04-24 01:30:00 2.439 5.511
284 2023-04-24 01:35:00 2.250 5.402
285 2023-04-24 01:40:00 2.443 5.381
286 2023-04-24 01:45:00 2.330 5.386
287 2023-04-24 01:50:00 2.220 5.310
288 2023-04-24 01:55:00 2.472 5.313
I have tryed to differentiate day and night using PARuw > 20 (if this is true then day) and use this on polygon(function) but I dont know how to do it well.
have you considered rect()
? You'd need four points on the x axis, two of which you get from the range
of date_time
s that fall in PARuw > 20
, and the other from par()$usr
, the coordinates of the plotting region, after initiating the first plot.
> xday <- with(Spring, range(date_time[PARuw > 20]))
> op <- par(mfrow=c(1, 1), mar=c(2, 3, 1, 3))
> plot.new()
> ## solar
> plot.window(as.numeric(range(Spring$date_time)), range(Spring$PARuw))
> box()
> ## night bg
> pu <- par()$usr
> rect(pu[1], pu[3], xday[1], pu[4], border=NA, col=rgb(0, 0, 0, .3))
> rect(xday[2], pu[3], pu[2], pu[4], border=NA, col=rgb(0, 0, 0, .3))
> ## solar radiation
> with(Spring, lines(date_time, PARuw, col=rgb(1, .65, 0), lwd=2))
> with(Spring, points(date_time, PARuw, pch=3, col=rgb(1, .65, 0, .1)), cex=.8)
> axis(4, las=1, col=rgb(1, .65, 0), col.axis=rgb(1, .65, 0))
> ## chlorophyll
> par(new=TRUE)
> plot.window(as.numeric(range(Spring$date_time)), range(Spring$chla))
> with(Spring, lines(date_time, chla, col=rgb(.13, .54, .13), lwd=2))
> with(Spring, points(date_time, chla, pch=3, col='#218a211a', cex=.8))
> axis(2, las=1, col=rgb(.13, .54, .13), col.axis=rgb(.13, .54, .13))
> x1 <- seq.POSIXt(as.POSIXct('2023-04-23'), as.POSIXct('2023-04-24'), '6 hours')
> axis(1, at=x1, labels=strftime(x1, '%H:%M'))
> par(op)
I used e.g. rgb(.13, .54, .13, .1)
to give the colors some transparency with the alpha=
parameter. Or try e.g. col='#218a211a'
for '#RRGGBBαα'
. Also thought axes should have colors corresponding to the lines (yellow was a bit bright).
Data:
> dput(Spring)
structure(list(date_time = structure(c(1682208000, 1682208300,
1682208600, 1682208900, 1682209200, 1682209500, 1682209800, 1682210100,
1682210400, 1682210700, 1682211000, 1682211300, 1682211600, 1682211900,
1682212200, 1682212500, 1682212800, 1682213100, 1682213400, 1682213700,
1682214000, 1682214300, 1682214600, 1682214900, 1682215200, 1682215500,
1682215800, 1682216100, 1682216400, 1682216700, 1682217000, 1682217300,
1682217600, 1682217900, 1682218200, 1682218500, 1682218800, 1682219100,
1682219400, 1682219700, 1682220000, 1682220300, 1682220600, 1682220900,
1682221200, 1682221500, 1682221800, 1682222100, 1682222400, 1682222700,
1682223000, 1682223300, 1682223600, 1682223900, 1682224200, 1682224500,
1682224800, 1682225100, 1682225400, 1682225700, 1682226000, 1682226300,
1682226600, 1682226900, 1682227200, 1682227500, 1682227800, 1682228100,
1682228400, 1682228700, 1682229000, 1682229300, 1682229600, 1682229900,
1682230200, 1682230500, 1682230800, 1682231100, 1682231400, 1682231700,
1682232000, 1682232300, 1682232600, 1682232900, 1682233200, 1682233500,
1682233800, 1682234100, 1682234400, 1682234700, 1682235000, 1682235300,
1682235600, 1682235900, 1682236200, 1682236500, 1682236800, 1682237100,
1682237400, 1682237700, 1682238000, 1682238300, 1682238600, 1682238900,
1682239200, 1682239500, 1682239800, 1682240100, 1682240400, 1682240700,
1682241000, 1682241300, 1682241600, 1682241900, 1682242200, 1682242500,
1682242800, 1682243100, 1682243400, 1682243700, 1682244000, 1682244300,
1682244600, 1682244900, 1682245200, 1682245500, 1682245800, 1682246100,
1682246400, 1682246700, 1682247000, 1682247300, 1682247600, 1682247900,
1682248200, 1682248500, 1682248800, 1682249100, 1682249400, 1682249700,
1682250000, 1682250300, 1682250600, 1682250900, 1682251200, 1682251500,
1682251800, 1682252100, 1682252400, 1682252700, 1682253000, 1682253300,
1682253600, 1682253900, 1682254200, 1682254500, 1682254800, 1682255100,
1682255400, 1682255700, 1682256000, 1682256300, 1682256600, 1682256900,
1682257200, 1682257500, 1682257800, 1682258100, 1682258400, 1682258700,
1682259000, 1682259300, 1682259600, 1682259900, 1682260200, 1682260500,
1682260800, 1682261100, 1682261400, 1682261700, 1682262000, 1682262300,
1682262600, 1682262900, 1682263200, 1682263500, 1682263800, 1682264100,
1682264400, 1682264700, 1682265000, 1682265300, 1682265600, 1682265900,
1682266200, 1682266500, 1682266800, 1682267100, 1682267400, 1682267700,
1682268000, 1682268300, 1682268600, 1682268900, 1682269200, 1682269500,
1682269800, 1682270100, 1682270400, 1682270700, 1682271000, 1682271300,
1682271600, 1682271900, 1682272200, 1682272500, 1682272800, 1682273100,
1682273400, 1682273700, 1682274000, 1682274300, 1682274600, 1682274900,
1682275200, 1682275500, 1682275800, 1682276100, 1682276400, 1682276700,
1682277000, 1682277300, 1682277600, 1682277900, 1682278200, 1682278500,
1682278800, 1682279100, 1682279400, 1682279700, 1682280000, 1682280300,
1682280600, 1682280900, 1682281200, 1682281500, 1682281800, 1682282100,
1682282400, 1682282700, 1682283000, 1682283300, 1682283600, 1682283900,
1682284200, 1682284500, 1682284800, 1682285100, 1682285400, 1682285700,
1682286000, 1682286300, 1682286600, 1682286900, 1682287200, 1682287500,
1682287800, 1682288100, 1682288400, 1682288700, 1682289000, 1682289300,
1682289600, 1682289900, 1682290200, 1682290500, 1682290800, 1682291100,
1682291400, 1682291700, 1682292000, 1682292300, 1682292600, 1682292900,
1682293200, 1682293500, 1682293800, 1682294100), class = c("POSIXct",
"POSIXt"), tzone = ""), chla = c(3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2.9403,
2.8812, 2.8227, 2.7648, 2.7075, 2.6508, 2.5947, 2.5392, 2.4843,
2.43, 2.3763, 2.3232, 2.2707, 2.2188, 2.1675, 2.1168, 2.0667,
2.0172, 1.9683, 1.92, 1.8723, 1.8252, 1.7787, 1.7328, 1.6875,
1.6428, 1.5987, 1.5552, 1.5123, 1.47, 1.4283, 1.3872, 1.3467,
1.3068, 1.2675, 1.2288, 1.1907, 1.1532, 1.1163, 1.08, 1.0443,
1.0092, 0.9747, 0.9408, 0.9075, 0.8748, 0.8427, 0.8112, 0.7803,
0.75, 0.7203, 0.6912, 0.6627, 0.6348, 0.6075, 0.5808, 0.5547,
0.5292, 0.5043, 0.48, 0.4563, 0.4332, 0.4107, 0.3888, 0.3675,
0.3468, 0.3267, 0.3072, 0.2883, 0.27, 0.2523, 0.2352, 0.2187,
0.2028, 0.1875, 0.1728, 0.1587, 0.1452, 0.1323, 0.12, 0.1083,
0.0972, 0.0867, 0.0768, 0.0675, 0.0588, 0.0507, 0.0432, 0.0363,
0.03, 0.0243, 0.0192, 0.0147, 0.0108, 0.0075, 0.0048, 0.0027,
0.0012, 3e-04, 0, 3e-04, 0.0012, 0.0027, 0.0048, 0.0075, 0.0108,
0.0147, 0.0192, 0.0243, 0.03, 0.0363, 0.0432, 0.0507, 0.0588,
0.0675, 0.0768, 0.0867, 0.0972, 0.1083, 0.12, 0.1323, 0.1452,
0.1587, 0.1728, 0.1875, 0.2028, 0.2187, 0.2352, 0.2523, 0.27,
0.2883, 0.3072, 0.3267, 0.3468, 0.3675, 0.3888, 0.4107, 0.4332,
0.4563, 0.48, 0.5043, 0.5292, 0.5547, 0.5808, 0.6075, 0.6348,
0.6627, 0.6912, 0.7203, 0.75, 0.7803, 0.8112, 0.8427, 0.8748,
0.9075, 0.9408, 0.9747, 1.0092, 1.0443, 1.08, 1.1163, 1.1532,
1.1907, 1.2288, 1.2675, 1.3068, 1.3467, 1.3872, 1.4283, 1.47,
1.5123, 1.5552, 1.5987, 1.6428, 1.6875, 1.7328, 1.7787, 1.8252,
1.8723, 1.92, 1.9683, 2.0172, 2.0667, 2.1168, 2.1675, 2.2188,
2.2707, 2.3232, 2.3763, 2.43, 2.4843, 2.5392, 2.5947, 2.6508,
2.7075, 2.7648, 2.8227, 2.8812, 2.9403, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3), PARuw = c(0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 120, 286.8, 451.2, 613.200000000001,
772.8, 930.000000000001, 1084.8, 1237.2, 1387.2, 1534.8, 1680,
1822.8, 1963.2, 2101.2, 2236.8, 2370, 2500.8, 2629.2, 2755.2,
2878.8, 3000, 3118.8, 3235.2, 3349.2, 3460.8, 3570, 3676.8, 3781.2,
3883.2, 3982.8, 4080, 4174.8, 4267.2, 4357.2, 4444.8, 4530, 4612.8,
4693.2, 4771.2, 4846.8, 4920, 4990.8, 5059.2, 5125.2, 5188.8,
5250, 5308.8, 5365.2, 5419.2, 5470.8, 5520, 5566.8, 5611.2, 5653.2,
5692.8, 5730, 5764.8, 5797.2, 5827.2, 5854.8, 5880, 5902.8, 5923.2,
5941.2, 5956.8, 5970, 5980.8, 5989.2, 5995.2, 5998.8, 6000, 5998.8,
5995.2, 5989.2, 5980.8, 5970, 5956.8, 5941.2, 5923.2, 5902.8,
5880, 5854.8, 5827.2, 5797.2, 5764.8, 5730, 5692.8, 5653.2, 5611.2,
5566.8, 5520, 5470.8, 5419.2, 5365.2, 5308.8, 5250, 5188.8, 5125.2,
5059.2, 4990.8, 4920, 4846.8, 4771.2, 4693.2, 4612.8, 4530, 4444.8,
4357.2, 4267.2, 4174.8, 4080, 3982.8, 3883.2, 3781.2, 3676.8,
3570, 3460.8, 3349.2, 3235.2, 3118.8, 3000, 2878.8, 2755.2, 2629.2,
2500.8, 2370, 2236.8, 2101.2, 1963.2, 1822.8, 1680, 1534.8, 1387.2,
1237.2, 1084.8, 930.000000000001, 772.8, 613.200000000001, 451.2,
286.8, 120, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, -288L), class = "data.frame")