1

I'm trying to add a caption to geom_hline () but I don't know how I can do it.

I tried geom_hline (aes (yintercept = 18, 'Historical serie'), size = 1.5, col =" # ffff00 ") but it returns an error. I took 'Historical serie' it worked, but the caption does not appear.

library(tidyr)
library(dplyr)
library(lubridate)
library(ggplot2)

date2<-c('01/01/2000','08/08/2000','16/03/2001','06/09/2001')
name<-c('A','B','A','B')
total<-c(23,25,36,41)
esp<-c(0.363,0.4123,0.09,0.65)

df<-data.frame(date2,name, total, name, esp)%>%
  mutate(date3=as.Date(date2, "%d/%m/%Y"))%>%
  mutate(year_date=as.Date(trunc(as.POSIXlt(date3), "years")))

ggplot(data=df, aes(x=year_date, y=total, fill=name))+ 
  geom_col(position = "dodge")+
  scale_fill_manual(values=c("#08088A","#9F81F7"))+
  theme_light()+labs(fill="rain", title ="rain anual", x="Estation", y="total mm")+
  theme(axis.text.x = element_text(angle = 90))+
  geom_hline(aes(yintercept = 18, 'Historiacal serie'), size=1.5, col="#ffff00")

Expectative

enter image description here

I would like the value 18 to appear as well as its description Historical serie for the yellow line.

wesleysc352
  • 579
  • 1
  • 8
  • 21
  • 3
    Does this answer your question? [Add geom\_hline to legend](https://stackoverflow.com/questions/55429396/add-geom-hline-to-legend) – B Williams May 17 '21 at 18:17
  • @B Williams it is not the answer, as it does not add the value "18" in the caption, only her name. So it doesn't fit my situation. – wesleysc352 May 17 '21 at 18:22
  • 1
    change the linetype to `linetype='Historical series \n18'` – B Williams May 17 '21 at 18:31
  • @B Williams, but the term "linetype" will appear in the caption. in the latter case, your response may be a way of getting around the situation. – wesleysc352 May 17 '21 at 19:39
  • Edit: This question is not duplicated as you will notice that the question caption is different from those on Stack Overflow. See that we have a title and a value associated with the reference line, while another suggested post has only one title associated with the line – wesleysc352 May 19 '21 at 14:22

2 Answers2

2

Update: OP request:

p <- ggplot(data=df, aes(x=year_date, y=total, fill=name))+ 
  geom_col(position = "dodge")+
  scale_fill_manual(values=c("#08088A","#9F81F7"))+
  theme_light()+labs(fill="rain", title ="rain anual", x="Estation", y="total mm")+
  theme(axis.text.x = element_text(angle = 90)) 

data_line <- data.frame(yintercept=18, data_line=factor(18))

p + 
  geom_hline(data=data_line, aes(yintercept=yintercept, linetype="18"), size=1.5, col="#ffff00") +
  scale_linetype_manual(name = "Historiacal serie",values = c(1,1)) 

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66
  • hi, the geom_hline () would not natively have commands for me to add the caption. Because I saw that you created another intermediate data.frame `p + geom_hline ()` to get around the problem – wesleysc352 May 17 '21 at 18:46
  • I noticed now here at Rstudio, that your solution didn’t look like the expectation image, as I would like the value 18 to appear. – wesleysc352 May 17 '21 at 18:50
  • 1
    Please see my edit. You can change title by `scale_linetype_manual`. – TarJae May 17 '21 at 20:12
  • 1
    very good friend. This is a good solution. I've been researching geom_hline and I saw on a github post that this command has problems, but strangely they haven't solved it yet: https://github.com/tidyverse/ggplot2/issues/1267 – wesleysc352 May 17 '21 at 20:15
0

I got it with help from https://community.rstudio.com/t/how-to-add-legend-in-goem-hline-in-r/105127/2 using only geom_hline ()

alternative

date2<-c('01/01/2000','08/08/2000','16/03/2001','06/09/2001')
name<-c('A','B','A','B')
total<-c(23,25,36,41)
esp<-c(0.363,0.4123,0.09,0.65)

ggplot(data=df, aes(x=year_date, y=total, fill=name))+ 
  geom_col(position = "dodge")+
  scale_fill_manual(values=c("#08088A","#9F81F7"))+
  theme_light()+labs(fill="rain", title ="rain anual", x="Estation", y="total mm")+
  theme(axis.text.x = element_text(angle = 90))+
  geom_hline(aes(yintercept = 18, linetype = "18"), colour = "green", size=1.5) +
scale_linetype_manual(name ="Historical serie", values = c('solid'))

enter image description here

wesleysc352
  • 579
  • 1
  • 8
  • 21