Home > Software engineering >  The title of my discrete y axis won't move in ggplot2
The title of my discrete y axis won't move in ggplot2

Time:11-29

I've got the following data:

library(ggplot2)
library(magrittr)
library(tidyverse)
library(scales)

mydata <- 
  structure(list(group = c("All participants", "Subgroup 1", "Subgroup 2", "Subgroup 3"), 
                 group_factor = structure(4:1, 
                                          .Label = c("Subgroup 3", "Subgroup 2", "Subgroup 1", "All participants"), 
                                          class = "factor"), 
                 estimate = c(0.81, 0.74, 0.88, 0.83), 
                 conf_low = c(0.55, 0.35, 0.53, 0.54), 
                 conf_high = c(1.2, 1.58, 1.44, 1.27), 
                 p_value = c(0.3, 0.43, 0.62, 0.38), 
                 label = structure(c(2L, 1L, 4L, 3L), 
                                   .Label = c("0.74 (0.35-1.58)", "0.81 (0.55-1.20)", "0.83 (0.54-1.27)", "0.88 (0.53-1.44)"), 
                                   class = "factor")), 
            row.names = c(NA, -4L), 
            class = c("tbl_df", "tbl", "data.frame"))

# Take a look at data.
head(mydata)
# A tibble: 4 x 7
  group            group_factor     estimate conf_low conf_high p_value label           
  <chr>            <fct>               <dbl>    <dbl>     <dbl>   <dbl> <fct>           
1 All participants All participants     0.81     0.55      1.2     0.3  0.81 (0.55-1.20)
2 Subgroup 1       Subgroup 1           0.74     0.35      1.58    0.43 0.74 (0.35-1.58)
3 Subgroup 2       Subgroup 2           0.88     0.53      1.44    0.62 0.88 (0.53-1.44)
4 Subgroup 3       Subgroup 3           0.83     0.54      1.27    0.38 0.83 (0.54-1.27)

Using ggplot I've managed to make the following figure, but I cant seem to adjust the position of the y axis title horizontally... I want it to be just centered above the y axis lables:

ggplot(data=mydata, 
       aes(x=estimate, 
           y=group_factor, 
           color=group))   
  geom_point(size=4, shape=c(0, 1, 2, 3))                              # Set different shapes for the groups 
  geom_errorbarh(aes(xmin=conf_low, xmax=conf_high, height=0.2))   
  scale_colour_manual(values=c("black", "black", "black", "black"))   
  scale_y_discrete(name="IRR (95%CI)", 
                   labels=c("0.83 (0.54-1.27)",                        # Apply the correct lables on the Y axis
                            "0.88 (0.53-1.44)", 
                            "0.74 (0.35-1.58)", 
                            "0.81 (0.55-1.20)"))   
  scale_x_continuous(trans=log_trans(),                                # Set the x axis to log scale  
                     breaks=c(0.3678794, 0.6065307, 1.00, 1.6487213), 
                     labels=c("0.37", "0.60", "1.00", "1.65"),
                     name="Incidence rate ratio")   
  theme(text=element_text(family="Calibri", size=12), 
        legend.title=element_blank(), 
        axis.title.y=element_text(angle=0, face="bold", hjust=0.2))    # Here adjusting hjust does not do anything
  guides(color=guide_legend(override.aes=list(shape=c(0, 1, 2, 3)))) 

enter image description here

CodePudding user response:

margin will solve this

see the line before the last line.

ggplot(data=mydata, 
       aes(x=estimate, 
           y=group_factor, 
           color=group))   
  geom_point(size=4, shape=c(0, 1, 2, 3))  
  geom_errorbarh(aes(xmin=conf_low, xmax=conf_high, height=0.2))   
  scale_colour_manual(values=c("black", "black", "black", "black"))   
  scale_y_discrete(name="IRR (95%CI)", 
                   labels=c("0.83 (0.54-1.27)",
                            "0.88 (0.53-1.44)", 
                            "0.74 (0.35-1.58)", 
                            "0.81 (0.55-1.20)"))   
  scale_x_continuous(trans=log_trans(),  
                     breaks=c(0.3678794, 0.6065307, 1.00, 1.6487213), 
                     labels=c("0.37", "0.60", "1.00", "1.65"),
                     name="Incidence rate ratio")   
  theme(text=element_text(family="Calibri", size=12), 
        legend.title=element_blank(), 
        axis.title.y=element_text(angle=0, face="bold", hjust=0.2,
                                  margin = margin(r = -70)))    # !! <--  !!
  guides(color=guide_legend(override.aes=list(shape=c(0, 1, 2, 3)))) 

enter image description here

CodePudding user response:

Another solution is to use put the y-axis label in the plot title (ggtitle) and play with the hjust argument. Note that you also need to remove the y-axis label (name="").

ggplot(data=mydata, 
       aes(x=estimate, 
           y=group_factor, 
           color=group))   
  geom_point(size=4, shape=c(0, 1, 2, 3))                             
  geom_errorbarh(aes(xmin=conf_low, xmax=conf_high, height=0.2))   
  scale_colour_manual(values=c("black", "black", "black", "black"))   
  scale_y_discrete(name="",                                       # HERE
                   labels=c("0.83 (0.54-1.27)",                        
                            "0.88 (0.53-1.44)", 
                            "0.74 (0.35-1.58)", 
                            "0.81 (0.55-1.20)"))   
  scale_x_continuous(trans=log_trans(),                                
                     breaks=c(0.3678794, 0.6065307, 1.00, 1.6487213), 
                     labels=c("0.37", "0.60", "1.00", "1.65"),
                     name="Incidence rate ratio")   
  ggtitle("IRR (95% CI)")                                         # HERE
  theme(text=element_text(family="Calibri", size=12),
        legend.title=element_blank() 
        plot.title = element_text(hjust = -0.28, face="bold"))    # HERE
  guides(color=guide_legend(override.aes=list(shape=c(0, 1, 2, 3)))) 

Which gives you this: image code

  • Related