Search code examples
rggplot2geom-textplotmath

Adding superscript and minus signs to data and axis labels in ggplot2


I'm trying to make a forest plot using some data:

dt <- data.frame(outcome = c("A", "B", "C", "D"), 
                 beta = c(-0.007, -0.008, -0.009, -0.009),
                 LCI = c(-0.005, -0.006, -0.007, -0.008), 
                 UCI = c(-0.008, -0.009, -0.01, -0.01), 
                 P = c(0.000001, 0.00002, 0.000003, 0.00004))

and this script:

pt <- ggplot(data = dt, aes(
  y = reorder(outcome, beta),
  x = beta,
  xmin = LCI,
  xmax = UCI
)) +
  geom_point(color = "black",
             shape = 18,
             size = 5) +
  geom_errorbarh(height = 0.1,
                 linewidth = 1,
                 color = "black") +
  geom_text(
    aes(
      label = paste0(beta, " (", LCI, ";", UCI,  "), p = ", P),
      x = beta,
      y = outcome
    ),
    size = 4,
    color = "black",
    position = position_dodge(0),
    vjust = -1.5,
    show.legend = FALSE#, check_overlap = FALSE
  ) +
  labs(title = 'My plot', x = 'Beta', y = 'Outcome') +
  theme_classic() +
  theme(plot.title = element_text(hjust = 0.05, size = 10, face = "bold")) +
  theme(
    axis.text = element_text(size = 10, face = "bold"),
    axis.title = element_text(size = 10, face = "bold")
  ) 

However, I need my p values be in the form of explicit notation (e.g. 7 × 10-3) and not in the E form (e.g. 7e-3) and to have minus signs instead of hyphens.

I tried replacing hyphens with minus signs:

t %>% mutate(beta = as.character(beta)) %>%
  mutate(beta = gsub(("-", "\u2013", beta)))

and for the LCI and UCI, accordingly, but it didn't make any difference.

Also tried adding the following line to fix the minus signs in the axis labels:

scale_x_continuous(breaks = seq(-0.01, -0.009, -0.008, -0.007, -0.006, -0.005), 
                   labels = \~sub("-", "\\u2212", .x)) 

but it also didn't help. Could anyone suggest any solution?


Solution

  • library(ggplot2)
    library(dplyr)
    
    dt <- data.frame(outcome = c("A", "B", "C", "D"), 
                     beta = c(-0.007, -0.008, -0.009, -0.009),
                     LCI = c(-0.005, -0.006, -0.007, -0.008), 
                     UCI = c(-0.008, -0.009, -0.01, -0.01), 
                     P = c(0.000001, 0.00002, 0.000003, 0.00004))
    
    dt %>% 
       mutate(P_labs = paste0(beta, "~(", LCI, "*';'", UCI, ")*','~p == ", 
                              gsub("(.*)e(.*)", "\\1~'x'~10^\\2", P))) %>% 
     ggplot(data = ., aes( x = beta, y = reorder(outcome, beta),  
                           xmin = LCI, xmax = UCI )) +
      geom_point(color = "black", shape = 18, size = 5) +
      geom_errorbarh(height = 0.1, linewidth = 1, color = "black") +
      geom_text(aes(label = P_labs,
                    x = beta, y = outcome),
                size = 4, color = "black",
                position = position_dodge(0), 
                vjust = -1.5, parse = TRUE, show.legend = FALSE) +
      labs(title = 'My plot', x = 'Beta', y = 'Outcome') +
      theme_classic() +
      theme(plot.title = element_text(hjust = 0.05, size = 10, face = "bold")) +
      theme(axis.text = element_text(size = 10, face = "bold"),
            axis.title = element_text(size = 10, face = "bold")) 
    

    Created on 2024-04-12 with reprex v2.0.2