0

I want the the x axis (before using coord_flip() the y axis) in the following plot to start at one value (say at 20) for the left chart and at another value (say at 80) for the right chart. I tried using ylim() inside coord_flip() but that does not work for me since (1) in reality the left axis has negative values and (2) it would make the axis in both plots start at the same value.

I found this post where they use geom_segment() in combination with scale_y_continuous but I also don't know how to make this work for two plots with a shared axis.

ggplot2 flipped y-axis bars not showing with correctly set limits

This is my code:

librarian::shelf(ggpol, tidyverse)

dat <- 
  tibble(group = rep(letters[1:5], each = 2),
         type = factor(paste0("type", rep(c(1:2), 5))),
         value = c(-50, 110, -45, 120, -40, 130, -35, 140, -30, 150))

dat %>% 
  ggplot(aes(x = group, y = value)) +
  geom_col() +
  coord_flip() +
  facet_share(~type, scales = "free", reverse_num = TRUE)
r-newbie
  • 149
  • 7
  • For a related issue and an answer using the `ggh4x` package see [Adjust axis limits on facet_share population pyramid](https://stackoverflow.com/questions/75088808/adjust-axis-limits-on-facet-share-population-pyarmid/75089376#75089376). – stefan Mar 01 '23 at 20:22
  • Thank you. That made it work. A small problem remains with this solution, however. Using ```facet_pos_scales``` I am not able to modify the axis labels on using for example ```scale_x_continuous(labels = scales::label_number(decimal.mark = ","))``` because it introduces NAs. – r-newbie Mar 02 '23 at 07:52

1 Answers1

0

Thanks to the helpful comment from stefan pointing out the ggh4x package I was able to solve it:

dat %>% 
  ggplot(aes(x = value, y = group)) +
  geom_col() +
  facet_share(~type, scales = "free", reverse_num = TRUE) +
  facetted_pos_scales(
    x = list(scale_x_continuous(limits = c(-40, -30)),
             scale_x_continuous(limits = c(35, 215))))

For some reason, however, I can't modify the axis labels of the left plot via for example scal_x_continuous(labels = scales::label_number(decimal.mark = ",")). I posted another question here: Modifying axis labels via scales::number() introduces NAs in combination with facetted_pos_scales()

r-newbie
  • 149
  • 7