1

我想做一个图表,我使用 4 个 geom_vlines 和不同的线型,但在图例中线型不正确。如何使图例的线型适合 vline 的线型?ps:我已经缩写了数据框,因为它太大了

我必须使用 show.legend = FALSE,否则图例会变成第二张图片

我尝试使用 scale_linetype_manual(),但它不起作用。

非常感谢你帮助我!!

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())

show.legend=TRUE 时的图表 图例的线型与图形的线型不符

这就是我写 show.legend=TRUE 时图表的保留方式

4

0 回答 0