2

我正在尝试更改stl分解图的构面标签,如下所示:

library(ggplot2)
library(ggfortify)
p <- autoplot(stl(AirPassengers, s.window = 'periodic'), ts.colour = "black", ts.size = 0.2)
p

该图源自ggfortify 包。我希望将构面标签更改为:

c("Original Data", "Seasonal component", "Trend component", "Remainder")

我试图进入一个ggplot(很多str'ing)的结构,并发现以下存储这些名称:

str(p$layers[[1]]$data$variable)
# Factor w/ 4 levels "Data","seasonal",..: 1 1 1

但是,当我就地更改此因素时。我得到四个空地块,然后是正确的地块:

p$layers[[1]]$data$variable <- factor(p$layers[[1]]$data$variable,
                                      labels=c("Original series", "Seasonal Component", "Trend component", "Remainder"))    

就地编辑因子时的结果

如何更改构面标签而不使这些空图位于顶部?

4

1 回答 1

3

一种可能性是更改绘图对象的相关组件。

# generate plot data which can be rendered
g <- ggplot_build(p)

# inspect the object and find the relevant element to be changed
# str(g)

# perform desired changes
g$panel$layout$variable <- c("Original Data", "Seasonal component", "Trend component", "Remainder")

# build a grob and 'draw' it
grid.draw(ggplot_gtable(g))

在此处输入图像描述

于 2015-05-20T16:02:29.473 回答