如何用 Autoplot.zoo 不同颜色制作我的绘图线?如何在背景中添加柔和的网格线?

How do I make my plotted line in Autoplot.zoo different colors? How to add soft gridlines in the background?

我正在努力寻找一种方法使我绘制的数据具有不同的颜色,例如 "palegreen2" 而不是黑色。这是我的代码。请让我知道我可以在我的代码中放入什么。

p <- autoplot.zoo(SP500)
p + labs(title = "SP500",
              subtitle = "(1997-2018)",
              caption = "Data from Yahoo Finance (^GSPC)",
              x = "Date",
              y = "Billions of Dollars",
              colour = "palegreen2") + theme_classic() + 
         theme(plot.title= element_text(hjust=0.5, margin = margin(t=0, r=0, b=5, l=0)), 
         axis.title.y = element_text(margin=margin(t=0, r=10, b=0, l=0)), 
         axis.title.x = element_text(margin=margin(t=10, r=0, b=0, l=0)),
         plot.margin = margin(1, 1, 0, 0, "cm"))

下面的代码考虑了两种情况,希望能解决你的问题

## packages
library(ggplot2)
library(zoo)

## simulation data
x=zoo(matrix (rnorm(30), nrow = 10), as.Date("2008-08-01") + 0:9)

## Case 1 All lines are drawn in one plot 
p=autoplot.zoo(x,facets = NULL)
p+scale_color_brewer(palette = "Set1")+
  theme_bw()

## Case 2 Lay out panels in a grid
p1=autoplot.zoo(x, colour='red')
p1$mapping$colour=rep(c('green','red','black'),rep(10, 3))
p1 + scale_color_brewer(palette = "Set1",
                        labels = c("four", "six", "eight"),
                        name = 'Name')+
  theme_bw()