无法更改线型ggplot2

时间:2017-03-28 11:20:44

标签: r ggplot2

我想更改color中的ggplot和行类型。我正在使用此代码:

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

但只改变了颜色。我该怎么做才能改变线型?

1 个答案:

答案 0 :(得分:2)

autoplot()将为其传递的对象选择合理的默认值。如果要自定义其外观,最好使用标准ggplot()功能 为此,zoo对象应通过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

(注意:顶部的虚线是由panel.background主题选项引起的)