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

17,741

Solution 1

Using default R graphics (i.e. without installing ggplot) you 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")

Solution 2

This is an old question, but I thought it might be helpful to post a solution that specifically addresses your question.

In ggplot2, you can plot a histogram and display the count with bars using:

ggplot(data) +  
geom_histogram()

You can also plot a histogram and display the count with lines using a frequency polygon:

ggplot(data) + 
geom_freqpoly()

For more info -- ggplot2 reference

Share:
17,741
asangoi
Author by

asangoi

Updated on June 17, 2022

Comments

  • asangoi
    asangoi about 2 years

    I've seen many examples of a density plot but the density plot's y-axis is the probability. What I am looking for a is a line plot (like a density plot) but the y-axis should contain counts (like a histogram).

    I can do this in excel where I manually make the bins and the frequencies and make a bar histogram and then I can change the chart type to a line - but can't find anything similar in R.

    I've checked out both base and ggplot2; yet can't seem to find an answer. I understand that histograms are meant to be bars but I think representing them as a continuous line makes more visual sense.

    • Richie Cotton
      Richie Cotton almost 10 years
      I'm not sure you have your terminology quite right. To me, a line histogram would be something like plot(..., type = "h"). That is, a histogram with vertical lines rather than bars. Your question suggests that you want a density plot with count on the y-axis.
    • asangoi
      asangoi almost 10 years
      Yes you're right. Density plot with count on the y-axis
  • asangoi
    asangoi almost 10 years
    Thanks. It did help me figure out the what the density function does.
  • asangoi
    asangoi almost 10 years
    Perfect. Seems I missed looking at the ?stat_density page.
  • Dale
    Dale almost 10 years
    @asangoi - I imagine you've gone off to sum() the density$y values for your various bins. An easier approach might be hist_list <- hist(data); plot(hist_list$mids, hist_list$counts, type = "b") Also, it you plot(hist_list$breaks, c(hist_list$counts, 0), type = "s") you (kinda) get the outline of the histogram. And hist(data, breaks = ...) lets you speicfy your own bins if you want. See ?hist to see how this works.
  • quesadagranja
    quesadagranja about 3 years
    Wouldn't it be better to use <- instead of =?
  • CnrL
    CnrL about 3 years
    People do that a lot, I think to avoid the ambiguity between assignation ("=") and comparison ("=="), but I think it's horrible and just moves the problem elsewhere. Let's say a = 1. Let's say we want to ask ourselves the question "Is a less than -1?": a < -1 Looks far too much like a <- 1 to me. Also, why use two symbols: "<" and "-" in place of one: "=". Assignment is an extremely common pattern in all programming.