如何将直方图更改为R中的折线图

时间:2017-06-21 18:40:51

标签: r plot histogram

我想知道如何将直方图(请参阅下面的h = hist())更改为折线图,如下图所示?

这是我尝试过的(没有成功):

set.seed(0)
hyp = rhyper(1e3, 12, 5, 7)
h = hist(hyp, plot = FALSE)
plot(hyp, xlim = range(hyp), ylim = range(h$counts), type = "h", lwd = 2)

要清楚,我试图绘制:

table(hyp)

# > hyp
# >  2   3   4   5   6   7  # X-axis
# >  2  66 253 404 240  35  # Y-axis

enter image description here

1 个答案:

答案 0 :(得分:2)

只需运行table,然后绘制

with(data.frame(table(hyp)), plot(x = as.numeric(as.character(hyp)), y = Freq, type = "h"))

或以下似乎也有效

plot(table(hyp))