ggplot2
ggplot2 copied to clipboard
position argument is not passed to layer() in annotate()
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`
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
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.
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
statorpositionare passed toannotate(). - Allow
positionandstatinannotate(). I tried searching the history ofannotate()for a reason why positions and stats aren't allowed, but came up empty.