0

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 funktion 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 soloution is fixing the paste(expression()) function. But i didnt 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.stack.imgur.com/9axhZ.png

Using R Version 4.0.5 and ggplot v3.3.3.

Jasper
  • 3
  • 2

1 Answers1

0

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.

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • If i do this i get a warning: In is.na(x) : is.na() applied to non-(list or vector) of type 'expression' and it still does not work. – Jasper Feb 06 '23 at 15:36
  • And if i use the ggsave function i get: In grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, : Metric information not available for this family/device – Jasper Feb 06 '23 at 15:44
  • It's a known issue, see my edit (with no more warning ... though the warning was not breaking anything). – r2evans Feb 06 '23 at 15:45
  • as for the error, perhaps https://www.mail-archive.com/r-devel@r-project.org/msg43261.html? – r2evans Feb 06 '23 at 15:46
  • I don't know if the problem is with `ggsave` or base R's `svg`. Try `svg("mt.svg", ...)` (setting dims and such), then print your graphics (you won't see it in your graphics pane), then `dev.off()`. Again, not sure if that'll make a difference. It does not error for me. (win11, R-4.2.2, ggplot2-3.4.0) – r2evans Feb 06 '23 at 15:48
  • 1
    Yes t-hanks to you, i removed the * and now the svg file works with both ways fine. – Jasper Feb 06 '23 at 16:01
  • You may be able to use `%*%` instead of `*`, not sure if that's the sole issue. Glad it worked! – r2evans Feb 06 '23 at 16:07