Change line style in autoplot

Asked

Viewed 118 times

0

I’m not sure how to change the style of a single line in a graph that contains two lines in autoplot in r. I can change the style using the ts.linetype command, but it changes the style of the two lines and not just one as I would like. What should I do? Forward the photo of the graphic and the code

autoplot(PMC, facets = FALSE, size=1, linetype = 'dashed') +

  scale_x_date(breaks = date_breaks("1 months"),

  labels = date_format("%b/%Y")) + theme_minimal()+

  guides(color=guide_legend(title="PMC")) + 

  scale_linetype_manual(labels = c("Restrita", "Ampliada"), values = c(1, 2))

inserir a descrição da imagem aqui

In case anyone can help me, I’d appreciate it.

  • 3

    Look, linetype was supposed to be inside aes for scale_linetype_manual can work. But since I don’t know the name of your variables or the PMC format, I can’t give a clearer answer. I recommend to put a part or example of the database for those who will help to reproduce.

  • 1

    As to the question, perhaps linetype = c('dashed', 'dotted') or another style of your choice. And if there are 3 lines, pass a vector with 3 styles, etc.

  • 1

    The @Jorgemendes says do not know the name of its variables or the format of PMC, this makes a huge difference. The function autoplot is generic, and the method called in the question code depends on the class of PMC. I’ve seen charts of autoplot completely different from the question graph.

  • Rui, he does not accept this form linetype = c('dashed', 'dotted'). Only in autoplot does not accept, when I use geom_line so it works.

2 answers

1

           PMC.restrita    PMC.ampliada

Jan 2018 2.4 4.6

Feb 2018 2.9 5.4

Mar 2018 3.8 6.3

Apr 2018 3.7 7.0

May 2018 3.7 6.8

Jun 2018 3.6 6.7

Jul 2018 3.3 6.5

Aug 2018 3.3 6.4

Sep 2018 2.8 5.9

Oct 2018 2.8 5.7

Nov 2018 2.6 5.5

Dec 2018 2.3 5.0

Jan 2019 2.2 4.7

Feb 2019 2.4 4.9

Mar 2019 1.3 3.9

Apr 2019 1.4 3.5

May 2019 1.3 3.9

Jun 2019 1.1 3.7

This is my database!

0

I passed your data to a csv file, in the following form:

date,PMC.restrita,PMC.ampliada
01 Jan 2018,2.4,4.6
01 Feb 2018,2.9,5.4
01 Mar 2018,3.8,6.3
01 Apr 2018,3.7,7.0
01 May 2018,3.7,6.8
01 Jun 2018,3.6,6.7
01 Jul 2018,3.3,6.5
01 Aug 2018,3.3,6.4
01 Sep 2018,2.8,5.9
01 Oct 2018,2.8,5.7
01 Nov 2018,2.6,5.5
01 Dec 2018,2.3,5.0
01 Jan 2019,2.2,4.7
01 Feb 2019,2.4,4.9
01 Mar 2019,1.3,3.9
01 Apr 2019,1.4,3.5
01 May 2019,1.3,3.9
01 Jun 2019,1.1,3.7

I couldn’t play your code with autoplot and so I adapted the dataframe to be able to use ggplot2.

It is necessary to include in Aesthetic the variable that discriminates the linetype. In doing so, we can manually modify the line types.

df <- read.csv(file = "dados.csv") # Leitura dos dados em csv
df$date <- as.Date(df$date, format="%d %b %Y") # Transformação da variável para tipo date
PMC <- melt(df, id.vars = "date") # Utilizando Melt para 

# Construção do gráfico
ggplot(PMC, aes(x=date, y=value,color = variable, linetype = variable, group=variable)) +
    geom_line() +
    ggtitle("PMC restrita e ampliada acumulada em 12 meses (%)") +
    xlab("Anos") + ylab("Percentual") +
    scale_colour_discrete(labels = c("Restrita", "Ampliada")) +
    scale_linetype_manual(labels = c("Restrita", "Ampliada"), values = c(1, 2))+
    scale_x_date(breaks = scales::date_breaks("1 months"),
                 labels = scales::date_format("%b/%Y")) + theme_minimal()+
    guides(color=guide_legend(title="PMC"), linetype=guide_legend(title="PMC"))

alteração de linha no ggplot2

Browser other questions tagged

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