Alexander David
Alexander David

Reputation: 799

Histogram from frequency data

My data is already summarized in frequency form due to the substantial number of observations (N=10M).

For example, is it in the following form (df):

base <- data.frame(x=round(rnorm(1000,mean=100,sd=10),1))
df <- base %>% group_by(x) %>% summarize(n=n())

How can I use this data to create a histogram in ggplot2? I know I've done this before without manually creating the bins, but I cannot remember how for the life me. I want to avoid re-transforming the data, ex:

ggplot() + geom_histogram(aes(rep(df$x, df$n)))

Thank you in advance.

edited to provide non-integer example which precludes use of geom_col/geom_bar

Upvotes: 1

Views: 3036

Answers (1)

detroyejr
detroyejr

Reputation: 1154

You can also use geom_bar.

ggplot(df, aes(x, n)) + geom_bar(stat = 'identity')

Upvotes: 1

Related Questions