How to change the legend, leaving it with the same strokes that are represented in the graph?

Asked

Viewed 60 times

0

I generated a graph that has three types of line; dotted, dashed and continuous. The problem is that subtitles are displayed as if all lines were equal. How can I fix this?

ggplot(dados,aes(x = Mês,y = PPS)) +
  geom_line(aes(y = PPS,col = "2019/2020"),size = 1.1,lty="dotted") +
  geom_line(aes(y = PPS2,col = "2020/2021"),size = 1.1,lty="longdash") +
  geom_line(aes(y = PPS3,col = "Experimento 2 (20/21)"),size = 1.1) +
  theme_bw() +
  labs(x = 'Período de Avaliação' ,y = 'Plantas com Sintomas (%)',color=NULL,title = 'Incidência (CABMV)') +
  theme(legend.position = c(0.2,0.8)) +
  scale_y_continuous(name = 'Plantas com Sintomas (%)',breaks = seq(0,100,10)) +
  scale_color_manual(values = c("black","black","black"))

inserir a descrição da imagem aqui

1 answer

-1

require(xts)
require(ggplot2)

set.seed(1)
Serie_A <- (1:10) + rnorm(10)
Serie_B <- (1:10)/2 + rnorm(10)
Serie_C <- (1:10)/3 + rnorm(10)

Mês <- seq(as.Date("2020-7-1"), as.Date("2021-4-1"), by = "months")
dados <- xts(cbind(Serie_A,Serie_B,Serie_C),order.by=Mês)
dados

             Serie_A    Serie_B    Serie_C
2020-07-01 0.3735462  2.0117812  1.2523107
2020-08-01 2.1836433  1.3898432  1.4488030
2020-09-01 2.1643714  0.8787594  1.0745650
2020-10-01 5.5952808 -0.2146999 -0.6560184
2020-11-01 5.3295078  3.6249309  2.2864924
2020-12-01 5.1795316  2.9550664  1.9438713
2021-01-01 7.4874291  3.4838097  2.1775378
2021-02-01 8.7383247  4.9438362  1.1959143
2021-03-01 9.5757814  5.3212212  2.5218499
2021-04-01 9.6946116  5.5939013  3.7512749

plot_dados<-ggplot(dados,aes(x = Mês, colour = aux)) +
    geom_line(stat="identity",aes(x = Mês,y = dados[,1], colour = "Série_A",linetype="Série_A")) +
    geom_line(stat="identity",aes(x = Mês,y = dados[,2], colour = "Série_B",linetype="Série_B")) +
    geom_line(stat="identity",aes(x = Mês,y = dados[,3], colour = "Série_C",linetype="Série_C")) +
    scale_colour_manual("aux",breaks = c("Série_A","Série_B","Série_C"),values=c("black","black","black")) +
    scale_linetype_manual("aux",breaks =c("Série_A","Série_B","Série_C"),values=c("solid","dotted","longdash")) +
    theme_grey(base_size = 15) +
    labs(title = "Séries A, B e C",x = "",y = "") +
    theme(plot.title = element_text(hjust = 0.5)) +
    theme(legend.title = element_blank()) +
    theme(legend.position = c(0.2, 0.8)) +
    guides(colour = guide_legend(nrow = 3)) +
    theme(legend.background = element_rect(fill="white",linetype="solid",colour ="black")) +
    theme(panel.background = element_rect(fill = "white",linetype="solid",colour = "black"))
plot_dados

Exemplo Séries A, B e C

Browser other questions tagged

You are not signed in. Login or sign up in order to post.