0

I have code that plot histograms.

library(ggplot2)

sample.range <- 50:150
mean <- 100
sd <- 15
set.seed(1)
n.samples <- c(1000, 10000)
my.df <- do.call(
           rbind, 
           lapply(
             n.samples, 
             function(x) data.frame(
                           "SampleSize" = x, 
                           "control" = rnorm(x, mean, sd)
                         )
           )
         )

ggplot() + 
  geom_histogram(data = my.df, aes(x = control)) + 
  facet_wrap(.~SampleSize, scales = "free_y")

I want to get graphs exactly like in the attached picture. The green bar is mean. [plot  (https://i.stack.imgur.com/kib87.png)

Unfortunately, these are my first attempts to draw such complicated graphs, so please help me

1 Answers1

2

You could do something like:

ggplot(data = within(my.df, SampleSize <- paste(SampleSize, 'Samples'))) + 
  geom_histogram(aes(x = control, fill = factor(SampleSize)),
                 color = 'gray70') + 
  geom_vline(data = . %>% group_by(SampleSize) %>% 
                    summarize(mean = mean(control)), size = 5,
             aes(xintercept = mean, color = factor(SampleSize))) +
  facet_wrap(.~SampleSize, scales = "free_y") +
  scale_fill_manual(values = c("#abb0b2", "#596160")) +
  scale_color_manual(values = c("#00862a", "#00862a")) +
  guides(fill = guide_none(), color = guide_none()) +
  theme_minimal() +
  theme(strip.text = element_text(size = 16, face = 'bold'))

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87