0

In a faceted histogram in ggplot2 (see attached) and I want to change the x and y axes breaks at different intervals on individual facets.

DemographicsSimPatients %>%
  keep(is.numeric) %>% 
  gather() %>% 
  ggplot(aes(value, fill = key)) +
  facet_wrap(~ key, scales = "free") +
  geom_histogram(bins=10)+
  labs(title = "SimPatient Demographics",
       y= "Number of Simulated Patients",
       x="") +
  theme(legend.position = "none")

UPDATE: thank you @stefan for showing me the ggh4x package, however can you please help I don't know how to change both x and y scales: I can only do one at a time and won't let me combine both:

scales <- list(
scale_x_continuous(breaks = c(20, 30, 40, 50, 60, 70)),
  scale_x_continuous(breaks = c(0, 50, 100, 150, 200, 250, 300)),                    
  scale_x_continuous(breaks = c(0, 40, 80, 120, 160, 200)),
  scale_x_continuous(breaks = c(1, 1.5, 2, 2.5, 3, 3.5, 4)),                                        
  scale_x_continuous(breaks = c(120, 130, 140, 150)),     
  scale_x_continuous(breaks = c(40, 50, 60, 70, 80))                     
                  )

FIGURE2 <- FIGURE1 + facet_wrap(vars(key), scales = "free_x") +
  facetted_pos_scales(x = scales) 

But now I want to addd the following scales to the y axis but don't know how to combine them into one graph?:

scales <- list(
  scale_y_continuous(breaks = c(0,25000)),
  scale_y_continuous(breaks = c(0,50000)),                    
  scale_y_continuous(breaks = c(0,100000)),
  scale_y_continuous(breaks = c(0,75000)),                                        
  scale_y_continuous(breaks = c(0,60000)),     
  scale_y_continuous(breaks = c(0,80000))                     
)
        

UPDATE2: Thank you @teunbrand for solving this for me. Last query is how do I add 2 intercepts that are only in my last facet? (on my last facet)? Thank you again. Thank you in advance

Banterny
  • 25
  • 4
  • Welcome to SO! It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. – stefan Apr 02 '22 at 22:35
  • 1
    ... this said: Have a look at the [ggh4x](https://teunbrand.github.io/ggh4x/articles/Facets.html#position-scales-1) package which offers some options to individually tweak the scales per facet. – stefan Apr 02 '22 at 22:37
  • Dear @stefan thank you so much for these comments. I've checked out the package and put an update to the question, let me know your thoughts. Thank you again. – Banterny Apr 02 '22 at 23:33
  • If you rename your scale lists as `scales_x` and `scales_y`, you should be able to do `+ facetted_pos_scales(x = scales_x, y = scales_y)`. – teunbrand Apr 03 '22 at 10:03
  • @teunbrand that is extremely helpful, you have solved it for me, I am extremely appreciative. My last question is how do I only add intercepts on my last facet (UKELD one)? I have updated the question. – Banterny Apr 03 '22 at 17:27
  • I'm not sure what you mean with intercept. A horizontal or vertical line? – teunbrand Apr 03 '22 at 19:06
  • Sorry... I mean two vertical lines that are dashed at different x coordinates of x=49 and x=60, and also a caption for each? @teunbrand – Banterny Apr 03 '22 at 21:34

0 Answers0