Precipitation graph or mirror histogram based on the upper axis

I like to build simple time series data and superficial rainfall data. The following code displays a line for ordinary data and adds bar marks (or histograms) for 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 up. But common in hydrology are precipitation bands that are based on the uppermost axis and “flow” down. Dhas arbitrary y ranges, so I would prefer a solution that captures the value for the baseline P.

I googled a lot, but could not find how to do it in R without ggplot and without additional packages such as a hydrograph.

+4
source share
2

BlankUsername zoo. - par(new=T) axis():

# 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)
+2

, , . , zoo.

# 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")

enter image description here

# And this is the downwards plot
plot(x, y, ylim = rev(range(y)),type="h") 

enter image description here

+3

Source: https://habr.com/ru/post/1535809/


All Articles