Home > Mobile >  Having trouble making boxplots in R
Having trouble making boxplots in R

Time:06-20

I am using the following code:

library(tidyverse)
Ns <- c(1298, 533, 1342, 897, 774, 254, 812, 324, 1291, 1056, 2172, 516)
p <- (d 1)/2
polls <- map_df(Ns, function(N) {
  x <- sample(c(0,1), size=N, replace=TRUE, prob=c(1-p, p))
  x_hat <- mean(x)
  se_hat <- sqrt(x_hat * (1 - x_hat) / N)
  list(estimate = 2 * x_hat - 1,
       low = 2*(x_hat - 1.96*se_hat) - 1,
       high = 2*(x_hat   1.96*se_hat) - 1,
       sample_size = N)
}) %>% mutate(poll = seq_along(Ns))
ggplot(data = polls , aes(x = polls$estimate, y = polls$poll))   geom_boxplot() 
  xlim(-.2,.2)   ylim(1,12)

I am getting the following graph:

enter image description here

But the required graph is as follows:

enter image description here

CodePudding user response:

You may edit some color or range.

polls %>%
  mutate(poll = as.factor(poll)) %>%
  ggplot(aes(x = poll, y = estimate))  
  geom_point()  
  geom_errorbar(aes(ymin = low, ymax = high), width = .2)  
  coord_flip()   theme_minimal()

enter image description here

  • Related