How to convert a bar histogram into a line histogram in R?

937    Asked by PavithraShah in Data Science , Asked on Aug 1, 2020
Answered by Pavithra Shah

The histograms are meant to be bars but representing them as a continuous line makes more visual sense. Using default R graphics (i.e. without installing ggplot) we can do the following, which might also make what the density function does a bit clearer:

# Generate some data

data=rnorm(1000)

# Get the density estimate

dens=density(data)

# Plot y-values scaled by number of observations against x values

plot(dens$x,length(data)*dens$y,type="l",xlab="Value",ylab="Count estimate")



Your Answer

Interviews

Parent Categories