I have a ggplot histogram of a random normal variable bob_commute_duration_minutes
, with mean 30 and SD 10 (part of a Shiny app, if it matters). I'd like to limit the visual of the histogram to +/- 3SD. I have this:
outdf_r() %>% ggplot(aes(x = bob_commute_duration_minutes,
fill= dplyr::if_else(bob_commute_duration_minutes>=input$commutemean,"Above Average","Below Average"))) +
geom_histogram(bins=100) +
# ggtitle(paste( mean(outdf_r()$bob_commute_duration_minutes), sd(outdf_r()$bob_commute_duration_minutes),sep=","))+
ggtitle("Bob's Commute Time Histogram (2SD Marked, 3SD Clipped)") +
guides(fill=guide_legend(title="Commute Above or Below Average"))+
xlab("Bob's Commute Time")+
xlim((input$commutemean- 3*input$commutesd),(input$commutemean + 3*input$commutesd)) +
geom_vline(xintercept=(input$commutemean- 2*input$commutesd),color="black") +
geom_vline(xintercept=(input$commutemean+ 2*input$commutesd),color="black")
So far so good; the extreme outliers aren't displayed.
I'd also like to edit the x-axis so there are more tick marks. I tried using scale_x_continuous
, with the limits of the breaks being +/- 3SD and tick marks at units of 1, but that caused the outliers to be displayed again. Any suggestions on how to make more detailed tick marks on the x-axis while still hiding values more than 3SD away from the mean?