Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does ggplot annotate throw this warning: In is.na(x) : is.na() applied to non-(list or vector) of type 'expression'

I would like to annotate a ggplot plot with a simple equation. The code below does it but it throws a warning about applying is.na():

library(ggplot2)
ggplot() +
  annotate(geom = "text", x = 1, y = 1, 
           label = expression(paste(beta, pi, "(1-" , pi, ")")),
           hjust = "left")
Warning message:
In is.na(x) : is.na() applied to non-(list or vector) of type 'expression'

What is the proper syntax to include the expression without the warning?

Why does this not make the warning go away?

suppressWarnings(
  ggplot() +
    annotate(geom = "text", x = 1, y = 1, 
             label = expression(paste(beta, pi, "(1-" , pi, ")")),
             hjust = "left")
)

I am using R version 4.0.2 with ggplot2 version 3.3.2

like image 255
itsMeInMiami Avatar asked Dec 23 '22 17:12

itsMeInMiami


1 Answers

The annotate() function does not support expressions. you need to pass in a string and set parse=TRUE. You can do

  annotate(geom = "text", x = 1, y = 1, 
           label = 'paste(beta, pi, "(1-" , pi, ")")', parse=TRUE,
           hjust = "left")
like image 77
MrFlick Avatar answered May 16 '23 07:05

MrFlick