首页 文章

无法更改线型ggplot2

提问于
浏览
1

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

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 回答

  • 2

    autoplot() 将为对象选择合理的默认值's passed. If you want to customize its appearance it'可能更好地使用标准的 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 主题选项引起的)

相关问题