【问题标题】:Plot LOESS (STL) decomposition using Ggvis使用 Ggvis 绘制 LOESS (STL) 分解图
【发布时间】:2015-10-22 14:29:45
【问题描述】:

我希望能够使用黄土 (STL) 和 Ggvis 绘制季节性趋势分解的三个不同元素。

但是,我收到此错误:

Error: data_frames can only contain 1d atomic vectors and lists

我正在使用 nottem 数据集。

# The Seasonal Trend Decomposition using Loess (STL) with Ggvis
# Load nottem data set
library(datasets)
nottem <- nottem

# Decompose using stl()
nottem.stl = stl(nottem, s.window="periodic")

# Plot decomposition
plot(nottem.stl)

现在,这是我感兴趣的信息。为了把它变成一个我可以玩的情节,我使用 xts-package 将它转换成一个数据框。到目前为止一切顺利。

# Transform nottem.stl to a data.frame
library(xts)
df.nottem.stl <- as.data.frame(as.xts(nottem.stl$time.series))

# Add date to data.frame
df.nottem.stl$date <- data.frame(time = seq(as.Date("1920-01-01"), by = ("months"), length =240))

# Glimpse data
glimpse(df.nottem.stl)

# Plot simple line of trend
plot(df.nottem.stl$date, df.nottem.stl$trend, type = "o")

这几乎是我想要的情节。但是,我希望能够将它与 Shiny 一起使用,因此 Ggvis 更可取。

# Plot ggvis
df.nottem.stl%>%
  ggvis(~date, ~trend)%>%
  layer_lines()

这是我得到错误的地方。

关于可能出错的任何提示?

【问题讨论】:

    标签: r plot stl dplyr ggvis


    【解决方案1】:

    首先,您的df.nottem.stl data.frame 包含一个Date data.frame,因此您应该使用date$time 列。然后使用layer_paths 函数而不是layer_lines 将使其工作。我总是发现layer_pathslayer_lines 工作得更好:

    所以这会起作用:

    library(ggvis)
    df.nottem.stl%>%
      ggvis(~date$time, ~trend)%>%
      #for points
      layer_points() %>% 
      #for lines
      layer_paths()
    

    输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-23
      • 1970-01-01
      • 2016-02-03
      • 1970-01-01
      • 2016-08-12
      相关资源
      最近更新 更多