Search code examples
rggplot2svgannotate

ggplot annotate brakes axis


I want to insert a text into my ggplot chart. The text got a subscript and a supersubscript like X[X} = 1,00*Y R^2 = 0,90. For that I used the function paste(expression()) into annotate(label=()).

In the graphics pane the whole plot looks fine, like i needed. But if i want to save the plot into a svg using the svg.save function the plot missing the Axis and the annotate Text.

Without annotate the whole svg is fine. So I guess, the solution is fixing the paste(expression()) function. But I didn't found a solution for that.

Data <- read.csv2("Data.csv", header = TRUE, sep = ";", dec = ",")
Data$ï..Date <- as.Date(Data$ï..Date, format = "%d.%m.%Y")

Gesamt_Plot1 <- ggplot() +
  geom_point(aes(X$EC,Y$BC), show.legend = FALSE, shape = 1, size = 1.5, na.rm = TRUE)+
  geom_abline(linetype = "dashed")+
  geom_segment(aes(x = 0.19,xend = 5.49, y = G1_slp*0.19,  yend = G1_slp*5.49), color = "black", size = 1.2, na.rm = TRUE)+
   annotate(geom="text", x = 4, y=6.4, parse = T, label=paste(expression(EBC~"="~"0,92*"*EC[TOR]~~R²~"="~"0,89")), color="black")+
   annotate(geom="text", x=0.1, y=7, label="a)", color="black")+
   labs(colour = "", x=expression(EC[TOR]~"in"~mu*g~m^-3), y=expression(EBC~"in"~mu*g~m^-3))+
    theme_bw()+
    theme(axis.text.y=element_text(size = 12), axis.text.x = element_text(size = 12),
        axis.title = element_text(size = 12), legend.position = "bottom", panel.grid.minor = element_blank())+
    scale_x_continuous(limit = c(0,7), 
                     breaks = c(0, 1, 2, 3, 4, 5, 6, 7) 
    )+
    scale_y_continuous(limit = c(0,7), 
                     breaks = c(0, 1, 2, 3, 4, 5, 6, 7)
    )

https://i.sstatic.net/9axhZ.png

Using R Version 4.0.5 and ggplot v3.3.3.


Solution

  • Not sure I can reproduce your problem, but I think you should not be using paste(expression(..)), just use expression(..). (Incidentally, I changed from your to R^2 in the expression; I'm not certain if it's a UTF problem in my emacs/ess or not. Try it both ways if you prefer.)

    ggplot(mtcars, aes(mpg, disp)) +
      geom_point() +
      annotate(geom="text", x=20, y=60, 
               label=expression(EBC~"="~"0,92*"*EC[TOR]~~R^2~"="~"0,89"))
    # Warning in is.na(x) :
    #   is.na() applied to non-(list or vector) of type 'expression'
    

    In the R graphics pane, I see

    ggplot2 graphics pane with expression annotation and axis labels

    Saving to svg:

    ggsave(filename="mt.svg")
    # Warning in is.na(x) :
    #   is.na() applied to non-(list or vector) of type 'expression'
    

    which then shows

    ggplot2 saved as svg, viewed in a web browser


    Note: due to a long-standing ... bug/feature (?) in ggplot2, it warns about expressions. Granted, it's just a warning (and it plots fine), so it can be ignored. See Why does ggplot annotate throw this warning: In is.na(x) : is.na() applied to non-(list or vector) of type 'expression'. Another method is this:

    ggplot(mtcars, aes(mpg, disp)) +
      geom_point() +
      annotate(geom="text", x=20, y=60, 
               label=list('EBC~"="~"0,92*"*EC[TOR]~~R^2~"="~"0,89"'), 
               parse=TRUE) +
      labs(colour = "", x=expression(EC[TOR]~"in"~mu*g~m^-3), y=expression(EBC~"in"~mu*g~m^-3)) +
      theme_bw()
    

    (using list('...') instead of expression(...), and adding parse=TRUE). This produces the same effect.