LookIntoEast
LookIntoEast

Reputation: 8798

Convert bar into points in hist() function

hist() will generate a histogram composed of lots of "bars"

How can I replace each bar with one point, so that many points together will look like a continuous curve?

Upvotes: 1

Views: 1529

Answers (2)

mnel
mnel

Reputation: 115390

some dummy data

x <- rnorm(50)

# create a histogram
.hist <- hist(x)

# look at the structure to see what is created when calling hist
str(.hist)

## List of 7
## $ breaks     : num [1:10] -2.5 -2 -1.5 -1 -0.5 0 0.5 1 1.5 2
## $ counts     : int [1:9] 1 2 5 6 8 10 10 5 3
## $ intensities: num [1:9] 0.04 0.08 0.2 0.24 0.32 0.4 0.4 0.2 0.12
## $ density    : num [1:9] 0.04 0.08 0.2 0.24 0.32 0.4 0.4 0.2 0.12
## $ mids       : num [1:9] -2.25 -1.75 -1.25 -0.75 -0.25 0.25 0.75 1.25 1.75
## $ xname      : chr "x"
## $ equidist   : logi TRUE
## - attr(*, "class")= chr "histogram"

# we could plot the mids (midpoints) against the counts
with(.hist, plot(mids, counts))

Or you could simply use density

plot(density(x))

enter image description here

Upvotes: 5

R. Schumacher
R. Schumacher

Reputation: 111

What you want is the density plot for a histogram. Here are two previous answers: The first shows how to overlay the density plot on the histogram. And the second on how to create the density plot along.

Axis-labeling in R histogram and density plots; multiple overlays of density plots

How to Plot Density from Frequency Table

Upvotes: 1

Related Questions