2

I want to do a graph where I use 4 geom_vlines with differents linetypes, but in the legend the linetype isn't right. How can I make the linetype of the legend fits with the linetype of the vline? ps: I have abbreviate the dataframe because it's too big

I had to use show.legend = FALSE, otherwise the legend becames like the second image

And I tried to use scale_linetype_manual(), but it doesn't work.

thank you very much for helping me!!

library("ggplot2")


Data<-c('2021-1-10','2021-1-11','2021-1-11','2021-1-12','2021-1-12','2021-1-13','2021-1-13','2021-1-14',
        '2021-1-14','2021-1-15','2021-1-15','2021-1-16','2021-1-17','2021-1-17','2021-1-18',
        '2021-1-19','2021-1-19','2021-1-20','2021-1-20','2021-1-21','2021-1-21','2021-1-22',
        '2021-1-23','2021-1-23','2021-1-24','2021-1-25','2021-1-25','2021-1-26','2021-1-26',
        '2021-1-27','2021-1-27','2021-1-28','2021-1-29','2021-1-30','2021-1-31','2021-1-31',
        '2021-2-1','2021-2-1','2021-2-2','2021-2-2','2021-2-3','2021-2-3','2021-2-4','2021-2-4',
        '2021-2-5','2021-2-6','2021-2-6','2021-2-7','2021-2-7','2021-2-8','2021-2-10',
        '2021-2-11','2021-2-11','2021-2-12')

Acumulado_CP_positivo<-c(0.417, 0.427,  0.427,  0.438,  0.438,  0.508,  0.508,  0.628,  0.628,  0.663,  0.663,  0.663,  0.693,  0.693,  0.705,  0.727,  0.727,  0.727,  0.727,  0.729,  0.729,  0.747,  0.747,  0.793,  0.793,  1.018,  1.018,  1.031,  1.031,  1.031,  1.031,  1.035,  1.107,  1.107,  1.107,  1.107,  1.148,  1.15,   1.162,  1.162,  1.162,  1.162,  1.194,  1.209,  1.209,  1.209,  1.209,  1.209,  1.209,  1.281,  1.281,  1.284,  1.284,  1.284)


Data<-as.Date(Data, format="%Y-%m-%d", origin = "2021-1-10")
df<-data.frame(Data, Acumulado_CP_positivo)

ggplot(df, aes(x = Data, y = Acumulado_CP_positivo))+
  geom_line(lwd = 0.8, aes(colour="Afluência"))+
  geom_vline(aes(colour="\nVazão Operativa \nVertedouro",
                 xintercept=as.Date("2021-01-21")),
             linetype=2,
             lwd = 0.75,
             show.legend=FALSE)+
  geom_vline(aes(colour="\nVazão Operativa \nSDT",
                 xintercept=as.Date("2021-01-12")),
             linetype=2,
             lwd = 0.75,
             show.legend=FALSE)+
  geom_vline(aes(colour="1° Rompimento LBMD",
                 xintercept=as.Date("2021-01-28")),
             linetype=3,
             lwd = 0.75,
             show.legend=FALSE)+
  geom_vline(aes(colour="2° Rompimento LBMD",
                 xintercept=as.Date("2021-02-08")),
             linetype=3,
             lwd = 0.75,
             show.legend=FALSE)+
  scale_color_brewer(palette="Dark2")+
  scale_y_continuous(breaks = c(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20))+
  theme_bw()+
  labs(title = "Variação Diária Acumulada (ha)",
       subtitle = "Vertedouro - MD", x = "Data", y = "Área Acumulada (ha)")+
  theme(legend.title = element_blank())

the graph when show.legend=TRUE the linetype of the legend doesn't fits with the linetype of the graphic

this is how the graph stays when I write show.legend=TRUE

  • 2
    Your `Data` is relatively unusable: my guess is that you intend them to be dates, but currently the first element of `Data` resolves to `0.004948046`, likely not what you intend. Additionally, your `Acumulado_CP_positivo` has a *parsing error* because you forgot to include the `c` before the first paren. Please *test code that you give us*, it is difficult and frustrating to parse through things like this to determine if issues are because more of your typos or some other fundamental issue. – r2evans Jan 19 '22 at 15:18
  • Also, your `as.Date` is wrong, you need to include `format="%d/%m/%Y"`. How is it that your plot looks like it does when the code here has so many unrelated problems? – r2evans Jan 19 '22 at 15:20
  • What is `MD`? Is that supposed to be `df`? What is `Acumulado_VT_positivo`? – r2evans Jan 19 '22 at 15:21
  • @r2evans I'm really really sorry for the mistakes in the code... I rewrite it and tested, I think now it will works. Thanks for trying to help me – helodambrat Jan 19 '22 at 19:29

0 Answers0