将直方图绘制为线条

时间:2013-05-21 20:52:14

标签: r plot histogram

我想在与线条相同的图上绘制几个时间数据的直方图(1周长)。

我有时间数据:

> dput(head(ts))
structure(c(1364421605.227, 1364375025.034, 1364376298.393, 1364375002.928, 
1364393158.084, 1364423268.856), class = c("POSIXct", "POSIXt"))

我想绘制直方图。 hist(ts,breaks=7*24)工作正常,但它使用的h绘图类型会遮盖我要添加的其他直方图:我知道我可以使用透明色 - rainbowalpha=0.5,但我真的很想看到这些线条。

我试过

> hist(ts, breaks = 7*24, type="l")
Warning messages:
1: In title(main = main, sub = sub, xlab = xlab, ylab = ylab, ...) :
  graphical parameter "type" is obsolete
Calls: hist -> hist.POSIXt -> myplot -> plot -> plot.histogram -> title
2: In axis(2, ...) : graphical parameter "type" is obsolete
Calls: hist -> hist.POSIXt -> myplot -> axis
3: In axis(side, at = z, labels = labels, ...) :
  graphical parameter "type" is obsolete
Calls: hist -> hist.POSIXt -> myplot -> axis.POSIXct -> axis

为什么histtype传递给title

我可以创建直方图对象并单独绘制它:

> h <- hist(ts, breaks=7*24, plot=FALSE)
> plot(x=h$mids, y=h$density, type="l")

但是x轴标有数字(e.d。,“1364421605”)而非日期(例如“3月25日”)。

我想我应该使用axis,但我宁愿不自己处理 - 毕竟,hist只构建我想要的轴!

谢谢!

1 个答案:

答案 0 :(得分:5)

以下内容看起来很奇怪,因为您只在dput中为我们提供了六个数据点。您可以绘制没有x轴的线图,然后像这样添加它:

h <- hist(ts, breaks=7*24, plot=FALSE)
plot(x=h$mids, y=h$density, type="l", xaxt="n")
axis(1,at=ts,labels=format(ts,"%b-%d"))

enter image description here

相关问题