Kaja
Kaja

Reputation: 3057

unable changing linetype ggplot2

I would like to change color and line type in my ggplot. I am using this code:

 a <- runif(84, 20, 80)
a<-ts(a,start = 2009,frequency = 12)
#a<-ts(result$`dataset1$Summe`,start = 2009,frequency = 12)
a3 <- zoo(a, order.by = as.Date(yearmon(index(a))))
p1 <- autoplot(a3)
p1 + scale_x_date(labels = date_format("%m/%Y"),breaks = date_breaks("2 months"), limits = as.Date(c('2009-01-01','2017-08-01')))+ theme(axis.text.x = element_text(angle = 90))+ theme(axis.text.x = element_text(angle = 90))+
  labs(x = "Date",y="Test") + theme(panel.background = element_rect(fill = 'white', colour = 'black'))+geom_line(linetype="dotted", color="red")

but only the color is changed. What should I do to change line type?

Upvotes: 1

Views: 883

Answers (1)

GGamba
GGamba

Reputation: 13680

autoplot() will pick a sensible default for the object it's passed. If you want to customize its appearance it's probably better to use the standard ggplot() function.
To be able to do that the zoo object should be passed trough fortify():

ggplot(fortify(a3, melt = TRUE)) +
    geom_line(aes(x = Index, y = Value), linetype='dashed', color="red") +
    scale_x_date(labels = date_format("%m/%Y"),
                 breaks = date_breaks("2 months"), 
                 limits = as.Date(c('2009-01-01','2017-08-01')))+ 
    theme(axis.text.x = element_text(angle = 90), 
          axis.text.x = element_text(angle = 90),
          panel.background = element_rect(fill = 'white', colour = 'black'))+
    labs(x = "Date",y="Test")

enter image description here

(NB: the dashed line at the top is caused by the panel.background theme option)

Upvotes: 2

Related Questions