I like to plot simple time series data and overplot precipitation data. The following code plots a line for the usual data and adds barplots (or histogram bars) for the precipitation data.
D # a simple (zoo) time series
P # a simple (zoo) time series of precipitation
plot(D, type="l")
lines(P, type="h", lwd=5)
But the bars are based on the y=0 axis and rise topwards. But usual in hydrology are precipitation bars that are based on the very top axis and "flow" downwards. D
has arbitrary y-ranges, so I would prefer a solution that does fix a value for the baseline of P
.
I googled a lot but did not manage to find how to do this in R without ggplot and without extra packages like hydrograph.
Not sure whether I understood it correctly so this is my interpretation.
Don't know whether this works with zoo
objects as well.
# Create some mock data
x<-runif(20,0,100)
y<-runif(20,0,100)
# This is the normal topwards plot
plot(x,y,type="h")
# And this is the downwards plot
plot(x, y, ylim = rev(range(y)),type="h")
With the help of BlankUsername I found the following solution for zoo
time series. I have not been aware of something like par(new=T)
or the axis()
command before:
# plot the usual data
plot(D)
# add half day to indicate that P is a sum of the whole day
index(P) <- index(P) + 0.5
# define an overlay plot without border
par(bty="n", new=T)
plot(P, type="h", ylim=rev(range(P)), # downward bars by BlankUsername
yaxt="n", xaxt="n", ann=F, # do not plot x and y axis
xlim=c(start(D),end(D)), # without xlim the two overlayed plots will not fit
lwd=10, col=rgb(0,0,0,0.1) ) # suggested cosmetics
# add right axis (4) to describe P
axis(4, pretty(range(P)), col.axis="grey", col="grey", las=1, cex.axis=0.7 )
# reset border and overlay
par(bty="o", new=F)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With