【问题标题】:Precipitation plot, or mirrored histogram based on top axis降水图,或基于顶轴的镜像直方图
【发布时间】:2014-05-23 05:46:24
【问题描述】:

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

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 中执行此操作。

【问题讨论】:

    标签: r plot histogram baseline


    【解决方案1】:

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

    【讨论】:

    • 我需要这个来覆盖以前的图,对齐到它的顶部。将 y 轴放在绘图的右上方会很棒。
    • 如果您想让 y 轴位于右侧,请在 plot 中使用 axes=FALSE 并单独指定轴。所以对于右上角的 y 轴,您将使用 axis(4,ylim = rev(range(y)))mtext("y",side=4) 作为标签。
    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 2012-10-20
      • 2018-04-10
      • 1970-01-01
      • 2012-01-18
      • 2019-11-29
      • 2015-12-28
      • 1970-01-01
      • 1970-01-01
      • 2016-07-29
      相关资源
      最近更新 更多