rggplot2facet-wrap

Manipulating axis labels in ggplot2 facet plots


I produced some kind of "pyramid plot" or "back to back plot" to compare two groups and examine the proportion of a certain event in each group (in this example the administering of certain drugs). I am 90% of the way there thanks to the ggh4x-package enabling me to mirror the scales. This is the code I used to produce the first plot below:

library(ggplot2)
library(ggh4x)

data <- data.frame(group = c("Group 1", "Group 1", "Group 2", "Group 2", "Group 1", "Group 1", "Group 2", "Group 2", "Group 1", "Group 1", "Group 2", "Group 2"),
                   administered = c("no", "yes", "no", "yes", "no", "yes", "no", "yes", "no", "yes", "no", "yes"),
                   medication = c("DRUG A with a long name", "DRUG A with a long name", "DRUG A with a long name", "DRUG A with a long name", "DRUG B", "DRUG B", "DRUG B", "DRUG B", "DRUG C", "DRUG C", "DRUG C", "DRUG C"),
                   count = c(100,200,50,88,99,300,77,45,12,0,9,27))

ggplot(data, aes(x = medication,
                 y = count,
                 fill = administered)) +
  geom_col(position = "fill") +
  ylab("proportion") +    
  scale_fill_manual(values = c("no"="#FF0000FF",
                               "yes"="#0000FFFF"))+
  theme(axis.ticks.y = element_blank()) +
  facet_wrap2(~ group, scales = "free") +
  facetted_pos_scales(y = list(
  scale_y_reverse(labels = scales::percent),
  scale_y_continuous(labels = scales::percent))) +
  coord_flip()

Result from my code

I would like to get rid of the labels in the first facet and centre the labels between the first and second facets. My desired result is something like this:

The photoshopped result I would like to achieve.

Your help is greatly appreciated!


Solution

  • This could be achieved via ggh2x as well. Note first that I switched the x and y aes to get rid of the coord_flip. As with the x axis you could sepcify the y-axis for each facet too and position the one for the frist facet on the right. Afterwards you could get rid of the right y scale via theme options. To center the axis text use hjust=.5 for the left y scale and as pointed out by @YoannPageaud in the comments by setting the margin on the left/right to the same amount and by removing the spacing between the panels:

    library(ggplot2)
    library(ggh4x)
    
    ggplot(data, aes(
      y = medication,
      x = count,
      fill = administered
    )) +
      geom_col(position = "fill") +
      labs(x = "proportion") +
      scale_fill_manual(values = c(
        "no" = "#FF0000FF",
        "yes" = "#0000FFFF"
      )) +
      facet_wrap2(~group, scales = "free") +
      facetted_pos_scales(
        x = list(
          scale_x_reverse(labels = scales::percent),
          scale_x_continuous(labels = scales::percent)
        ),
        y = list(
          scale_y_discrete(position = "right"),
          scale_y_discrete()
        )
      ) +
      theme(
        axis.ticks.x = element_blank(),
        axis.text.y.right = element_blank(),
        axis.line.y.right = element_blank(),
        axis.text.y.left = element_text(
          hjust = .5,
          margin = margin(l = 5.5, r = 5.5)
        ),
        panel.spacing.x = unit(0, "pt")
      )