Search code examples
rggplot2histogram

How to recreate this plot in ggplot2?


Here is my data and original plot:

z <- dbinom(0:6, size=6, p=0.512)
names(z) <- as.character(0:6)
barplot(z, space=0, ylab="Probability", col = "firebrick", las = 1, xlab = "Number of boys")

I need to recreate this same plot in ggplot2 but I'm struggling to make it look remotely similar. Any help would be appreciated.


Solution

  • Building on your code:

    library(ggplot2)
    ggplot(data.frame(z = z, num_boys = names(z)), aes(x = num_boys, y = z)) +
            geom_bar(stat = "identity", fill = "firebrick", col = "black", width = 1) +
            labs(y = "Probability", x = "Number of boys") +
            ggthemes::theme_base()
    

    enter image description here

    NOTE: I used ggthemes::theme_base() to make the plot look like the base plot your original code produces.