Mar*_*tin 5 plot r histogram baseline
我喜欢绘制简单的时间序列数据和覆盖降水数据。以下代码为常用数据绘制一条线,并为降水数据添加条形图(或直方图条)。
D # a simple (zoo) time series
P # a simple (zoo) time series of precipitation
plot(D, type="l")
lines(P, type="h", lwd=5)
Run Code Online (Sandbox Code Playgroud)
但是条形图基于 y=0 轴并向上上升。但是在水文学中通常是基于顶部轴和向下“流动”的降水条。D具有任意 y 范围,所以我更喜欢一个解决方案,它确实为P.
我在谷歌上搜索了很多,但没有找到如何在没有 ggplot 的情况下在 R 中做到这一点,也没有像水流图这样的额外包。
在 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)
Run Code Online (Sandbox Code Playgroud)