ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

position argument is not passed to layer() in annotate()

Open dmi3kno opened this issue 2 years ago • 3 comments

The argument position is not passed through ellipsis to the layer() command from annotate()

ggplot(mtcars,aes(disp,mpg))+geom_point()+
annotate("text", x=min(mtcars$disp), y=max(mtcars$mpg), label="Value", hjust=0, position=position_nudge(x=10))
#> Warning message:
#> In annotate("text", x = min(mtcars$disp), y = max(mtcars$mpg), label = "Value",  :
#>  Ignoring unknown parameters: `position`

dmi3kno avatar Jan 15 '23 19:01 dmi3kno

you can just use geom_text

library(ggplot2)

ggplot(mtcars,aes(disp,mpg))+
  geom_point()+
  annotate("text",
           x=-Inf,
           y=Inf,
           label="Value",
           hjust=0,
           vjust=1.5)


ggplot(mtcars,aes(disp,mpg))+
  geom_point()+
  geom_text(data=data.frame( x=min(mtcars$disp),
                             y=max(mtcars$mpg),
                             label="Value"),
            aes(x=x,y=y,label=label),
            hjust=0, position=position_nudge(x=10))

Created on 2023-01-16 with reprex v2.0.2

smouksassi avatar Jan 16 '23 08:01 smouksassi

I am aware, thank you. Something goes irrecoverably bad in PDF when I use plotmath in the label for geom_text(). This is not happening in annotate(). I would like annotate() to work as advertised.

dmi3kno avatar Jan 16 '23 09:01 dmi3kno

It also doesn't pass any stat to layer() either. The position and stat of the layer are hardcoded to be the identity position/stat. I think there are three options:

  • Improve documentation of the ... to explicitly state what can and cannot be passed to layer this way (WIP).
  • Throw an error when stat or position are passed to annotate().
  • Allow position and stat in annotate(). I tried searching the history of annotate() for a reason why positions and stats aren't allowed, but came up empty.

teunbrand avatar Jan 16 '23 19:01 teunbrand