rnorouzian
rnorouzian

Reputation: 7517

How to change a histogram to a line-chart in R

I was wondering how I could change the histogram (see h = hist() below) to a line-chart as shown in the picture below?

Here is what I tried (with no success):

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)

To be clear, I am trying to plot:

table(hyp)

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

enter image description here

Upvotes: 0

Views: 110

Answers (1)

d.b
d.b

Reputation: 32558

Just run the table and then plot

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

OR following seems to work too

plot(table(hyp))

Upvotes: 2

Related Questions