Search code examples
rggplot2scientific-notation

How to print p-values with exponent (scientific) notation?


When printing values in R using 'ggplot', I'm facing issues to write the exponent proprerly. See the plot below, and an example of what I would like to obtain hand-written in red.

data <- data.frame(var = 1:10,
                  pvalue=c(0.1,0.001,0.0000133233,0.2,0.2222,0.43254,0.1,0.67,0.6777,0.2)) %>% 
  mutate(pvalue=as.character(signif(pvalue,digits=2)))# %>%mutate(pvalue = sub("e","10^",pvalue) )


data %>% 
  ggplot(aes(y = var)) +
  geom_text(aes(x = 0, label = pvalue), parse=T)

Ggplot


Solution

  • If you only need to change the values with e to exponent forms, you only need to add %.% to your commented code.

    data <- data.frame(var = 1:10,
                       pvalue = c(0.1,0.001,0.0000133233,0.2,0.2222,
                                  0.43254,0.1,0.67,0.6777,0.2)) %>% 
                       mutate(pvalue = as.character(signif(pvalue,digits=2))) %>%
                       mutate(pvalue = sub("e","%.% 10^",pvalue) )
    data %>% 
      ggplot(aes(y = var)) +
      geom_text(aes(x = 0, label = pvalue), parse = TRUE)
    

    The result:

    enter image description here