5

我喜欢绘制简单的时间序列数据和叠加降水数据。以下代码为常用数据绘制一条线,并为降水数据添加条形图(或直方图条)。

D # a simple (zoo) time series
P # a simple (zoo) time series of precipitation
plot(D, type="l")
lines(P, type="h", lwd=5)

但条形图基于 y=0 轴并向上上升。但在水文学中通常是基于最上轴并向下“流动”的降水条。D有任意的 y 范围,所以我更喜欢一个解决方案,它确实为P.

我用谷歌搜索了很多,但没有找到如何在没有 ggplot 和没有像水文这样的额外包的情况下在 R 中做到这一点。

4

2 回答 2

3

不确定我是否理解正确,所以这是我的解释。不知道这是否也适用于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")

在此处输入图像描述

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

在此处输入图像描述

于 2014-04-09T20:44:07.747 回答
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)
于 2014-04-09T22:47:18.690 回答