Reza
Reza

Reputation: 398

Drawing a histogram with ggplot2 when you only have the frequencies

My actual data set is quite large and it takes R some time to process it. So I wrote a little C program to compute the frequency for each possible value. (Say, the possible values in the data set are 0,1,2,3.) So I have a frequency distribution which (for the sake of presentation) looks like this:

0.1 0.4 0.3 0.2

If I feed this data to ggplot2 using geom_histogram, I don't get the right histogram. So how can I draw a histogram with the above frequency distribution?

Upvotes: 0

Views: 742

Answers (2)

Al14
Al14

Reputation: 1814

My approach without creating an extra data frame. In the x axis you can find the number of your frequencies

 library(ggplot2)
x<-c(0.1, 0.4, 0.3, 0.2)
ggplot(data.frame(x), aes(y=x, x=1:length(x)))+
  geom_bar(stat = "identity")

Upvotes: 0

Peter
Peter

Reputation: 7790

You will want to use stat = 'identity' within the geom_bar call.

library(ggplot2)

dat <- data.frame(x = c(0, 1, 2, 3), y = c(0.1, 0.4, 0.3, 0.2))

ggplot(dat) +
  geom_bar(mapping = aes(x = x, y = y), stat = "identity")

enter image description here

Upvotes: 3

Related Questions