Search code examples
rggplot2axis-labelsfacet-wrapfacet-grid

Additional right axis for groups


I'm trying to make a right-side axis title (but only a title) for grouped (facet_grid()) variables.

I have tried the scale_y_continuous(sec.axis = (name= "title") command, it didn't work without a variable which I don't have. I have also tried axis.title.y.right = element_text("Sex of groups") element in the theme function. It didn't work either.

What I have as code is this:

temp.data = data.frame (Species  = rep(c("A","B"),each=2, times=2),
                           Status = rep(c("An","Bac"), times=4),
                           Sex = rep(c("Male","Female"), each=4, times=1),
                           Proportion = c(6.86, 7.65, 30.13, 35.71, 7.13, 10.33, 29.24, 31.09))

ggplot(temp.data, aes(x = Species, y = Proportion, fill = Status)) +
  geom_bar(stat='identity', position = position_dodge(width = 0.73), width=.67) +
  facet_grid(Sex ~ .) + xlab("X title") + 
  ylab("Y title") + 
  scale_fill_manual(name = "Status", labels = c("A","B"),
                    values = c("#0d4e04","#a36424")) +
 # scale_y_continuous(sec.axis = (name= "Sex of groups")) +
  theme_light() + theme(legend.title= element_text(size = 8),
                        legend.title.align=0.5,
                        legend.position = c(),
                        legend.background=element_blank(),
                        legend.text=element_text(size=7),
                        legend.key.size = unit(0.6, 'cm'),
                        axis.title.x = element_text(size = 9),
                        axis.title.y = element_text(size = 9),
                        axis.title.y.right = element_text("Sex of groups"),
                        strip.background = element_rect(
                          color="lightgrey", fill="white", size=0.5, linetype="solid"),
                        strip.text.y = element_text(color = "darkgrey", face = "bold"))

This is the graph I have produced:

enter image description here

So basically I'm looking for a way to put a title on the right Y axis, just near the facetted groups. It'd be great to know, if you know a solution for it.


Solution

  • Basically you were on the right track. You could add your title via a duplicated y scale using scale_y_continuous(sec.axis = dup_axis(name= "Sex of groups")). Besides that we need some additional theme adjustments to get rid of the axis text and ticks where the latter also requires to set the tick length to 0:

    library(ggplot2)
    
    ggplot(temp.data, aes(x = Species, y = Proportion, fill = Status)) +
      geom_bar(stat = "identity", position = position_dodge(width = 0.73), width = .67) +
      facet_grid(Sex ~ .) +
      xlab("X title") +
      ylab("Y title") +
      scale_fill_manual(
        name = "Status", labels = c("A", "B"),
        values = c("#0d4e04", "#a36424")
      ) +
      scale_y_continuous(sec.axis = dup_axis(name= "Sex of groups")) +
      theme_light() +
      theme(
        legend.title = element_text(size = 8),
        legend.title.align = 0.5,
        legend.position = c(),
        legend.background = element_blank(),
        legend.text = element_text(size = 7),
        legend.key.size = unit(0.6, "cm"),
        axis.title.x = element_text(size = 9),
        axis.title.y = element_text(size = 9),
        axis.title.y.right = element_text(margin = margin(l = 5.5)),
        axis.text.y.right = element_blank(),
        axis.ticks.y.right = element_blank(),
        axis.ticks.length.y.right = unit(0, "pt"),
        strip.background = element_rect(
          color = "lightgrey", fill = "white", size = 0.5, linetype = "solid"
        ),
        strip.text.y = element_text(color = "darkgrey", face = "bold")
      )
    

    enter image description here