Text box in ggplot2

hi,

I have this code:
p <- ggplot(dataset, aes(x =i, y = um)) +
geom_point()

p <- p + geom_line(aes(y = EXPMEDIA, colour = "EXP"))+ geom_line(aes(y = simple, colour = "simple"))

p <- p + scale_y_continuous(sec.axis = sec_axis(~.*1, name = ""))

p<- p + scale_colour_manual(values = c("blue", "red"))
p <- p + labs(y = "UMIDADE ",
x = "DATES ",
colour = "Legenda")
p <- p + theme(legend.position = c(0.955, 0.95)) +
ggtitle(" Exponential Moving Average ")
p

I need plot in this graphic the last value of last<=tail(dataset$EXPMEDIA, n=1) how i can plot this value in the graphic ?

thank you !!

Hi @rafael,
Might you be able to post a reproducible example with a bit of data? That way we can see what's happening at each step— it's hard to envision what you've got going on (at least for me) without seeing the output or the input!

Maybe take a look at @njtierney's reprex post

https://www.njtierney.com/post/2017/01/11/magic-reprex/

2 Likes

Do geom_text or geom_label provide what you need? [http://ggplot2.tidyverse.org/reference/geom_text.html]

1 Like

I tend to feel that if you start from the general principle that ggplot is about columns preforming jobs when making graphs, there is normally one solution by have what you want in a column- so setting up the data then using ggplot to generate a figure from it

library(dplyr)
library(ggplot2)
data(iris) 
iris %>%
  mutate(glab = ifelse(row_number() < n(), "  ",as.character(Species))) %>% 
ggplot(aes(x=Sepal.Width, y=Sepal.Length, label=glab)) + geom_point() +
  geom_text()```
1 Like