Jim_dddd Jim_dddd - 3 months ago 20
R Question

Flip coin simulation with R programming

I'm new to R and I'm doing a practice question.
Simulate rolling a fair coin 200 times, then plot a histogram of the data.
I did:

outcomes <- c("heads", "tails")
sim_fair_coin <- sample(outcomes, size = 200, replace = TRUE)
hist(table(sim_fair_coin))


It does give me a histogram, but I think I expect a more intuitive histogram. With the X-axis shows "heads" or "tails"; and the Y-axis shows how many times I got for "heads" and "tails".
I believe there is a way to accomplish this in R. Can someone tell me more about it please?

Answer

You can use the ggplot2 package as well.

x <- as.data.frame(sim_fair_coin)
ggplot(x, aes(factor(sim_fair_coin))) + geom_bar(width=.5)