llewmills
llewmills

Reputation: 3568

Histogram in ggplot2 with pre-defined probability values

I am trying to make my own histogram in ggplot2 where I supply ggplot2 with the probability for each value of the parameter values in a vector instead of having it calculate the probability values itself from a single vector.

Toy data:

This is the parameter values

p <- seq(from = 0.05, to = 0.95, by = 0.1)

I assign each of these a weight

prior <- c(1, 5.2, 8, 7.2, 4.6, 2.1, 0.7, 0.1, 0, 0)

Then calculate a prior probability for each of these weights

prior <- prior/sum(prior)

Now using the plot() function we can create a histogram using those two vectors

plot(p, prior, "h", ylab = "prior probability")

But how can I get a similar graph in ggplot2?

If I try putting these into a dataframe

gDF <- data.frame(p = p, priorProb = prior)

Then graphing it

ggplot(gDF, aes(x = p, y = priorProb)) + geom_histogram()

I get the error Error: stat_bin() must not be used with a y aesthetic, which makes sense because usually for a histogram ggplot only uses a single vector.

Any help appeciated.

Upvotes: 2

Views: 504

Answers (1)

Nate
Nate

Reputation: 10671

ggplot(gDF, aes(x = p, y = priorProb)) + geom_bar(stat = "identity")

Looks

Upvotes: 2

Related Questions