如何在分解的时间序列图中自定义标题,轴标签等

时间:2017-03-27 19:00:57

标签: r plot time-series timeserieschart

我非常熟悉通过编写自己的x轴标签或主标题来修改绘图的常用方法,但在绘制时间序列的结果时,我无法自定义输出分解。

例如,

library(TTR)
t <- ts(co2, frequency=12, start=1, deltat=1/12)
td <- decompose(t)
plot(td)
plot(td, main="Title Doesn't Work") # gets you an error message

为您提供了观察到的时间序列,趋势等的基本情节。根据我自己的数据(水面以下的深度变化),我希望能够切换方向y轴(例如,ylim = c(40,0)用于&#39;观察&#39;或ylim = c(18,12)用于&#39;趋势&#39;),更改季节&# 39;为了潮汐&#39;,包括x轴的单位(&#39;时间(天)&#39;),并为该图提供更具描述性的标题。

我的印象是,我所做的那种时间序列分析是非常基本的,最终,我可能最好使用另一个包,可能有更好的图形控制,但我想使用ts()和decompose()如果我现在可以(是的,蛋糕和消费)。假设这并不太可怕。

有办法做到这一点吗?

谢谢!皮特

1 个答案:

答案 0 :(得分:6)

您可以修改plot.decomposed.ts函数(即plot“方法”,当您在类plot的对象上运行decomposed.ts时,该td方法将被调度getAnywhere(plot.decomposed.ts) )。

function (x, ...) 
{
    xx <- x$x
    if (is.null(xx)) 
        xx <- with(x, if (type == "additive") 
            random + trend + seasonal
        else random * trend * seasonal)
    plot(cbind(observed = xx, trend = x$trend, seasonal = x$seasonal, random = x$random), 
         main = paste("Decomposition of", x$type, "time series"), ...)
}
my_plot.decomposed.ts = function(x, title="", ...) {
  xx <- x$x
  if (is.null(xx)) 
    xx <- with(x, if (type == "additive") 
      random + trend + seasonal
      else random * trend * seasonal)
  plot(cbind(observed = xx, trend = x$trend, seasonal = x$seasonal, random = x$random), 
       main=title, ...)
}

my_plot.decomposed.ts(td, "My Title")

请注意,在上面的代码中,该函数对标题进行了硬编码。所以让我们修改它,以便我们可以选择自己的标题:

library(tidyverse) # Includes the packages ggplot2 and tidyr, which we use below

# Get the time values for the time series
Time = attributes(co2)[[1]]
Time = seq(Time[1],Time[2], length.out=(Time[2]-Time[1])*Time[3])

# Convert td to data frame
dat = cbind(Time, with(td, data.frame(Observed=x, Trend=trend, Seasonal=seasonal, Random=random)))

ggplot(gather(dat, component, value, -Time), aes(Time, value)) +
  facet_grid(component ~ ., scales="free_y") +
  geom_line() +
  theme_bw() +
  labs(y=expression(CO[2]~(ppm)), x="Year") +
  ggtitle(expression(Decomposed~CO[2]~Time~Series)) +
  theme(plot.title=element_text(hjust=0.5))

enter image description here

这是情节的ggplot版本。 ggplot需要一个数据框,所以第一步是将分解的时间序列转换为数据框形式,然后绘制它。

//MARK: - upload multiple photos

func uploadImagesAndData(params:[String : AnyObject]?,image1: UIImage,image2: UIImage,image3: UIImage,image4: UIImage,headers : [String : String]?, completionHandler:@escaping CompletionHandler) -> Void {

    let imageData1 = UIImageJPEGRepresentation(image1, 0.5)!
    let imageData2 = UIImageJPEGRepresentation(image2, 0.5)!

    let imageData3 = UIImageJPEGRepresentation(image3, 0.5)!

    let imageData4 = UIImageJPEGRepresentation(image4, 0.5)!


    Alamofire.upload(multipartFormData: { multipartFormData in

            for (key, value) in params! {
                if let data = value.data(using: String.Encoding.utf8.rawValue) {
                    multipartFormData.append(data, withName: key)
                }
            }

            multipartFormData.append(imageData1, withName: "file", fileName: "image.jpg", mimeType: "image/jpeg")
            multipartFormData.append(imageData2, withName: "file", fileName: "image.jpg", mimeType: "image/jpeg")
            multipartFormData.append(imageData3, withName: "file", fileName: "image.jpg", mimeType: "image/jpeg")
            multipartFormData.append(imageData4, withName: "file", fileName: "image.jpg", mimeType: "image/jpeg")

    },
        to: K_BASEURL + K_API_LOGINDATA, encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload
                    .validate()
                    .responseJSON { response in
                        switch response.result {
                        case .success(let value):
                            print("responseObject: \(value)")
                        case .failure(let responseError):
                            print("responseError: \(responseError)")
                        }
                }
            case .failure(let encodingError):
                print("encodingError: \(encodingError)")
            }
    })

enter image description here