更改 ggfortify::autoplot 创建的 ggplot 的分面标签
Change facet labels for a ggplot created by ggfortify::autoplot
我正在尝试更改 stl
分解图的分面标签,如下所示:
library(ggplot2)
library(ggfortify)
p <- autoplot(stl(AirPassengers, s.window = 'periodic'), ts.colour = "black", ts.size = 0.2)
p
剧情来源于ggfortify package。
我希望将分面标签更改为:
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"))
如何在顶部不显示这些空图的情况下更改构面标签?
一种可能是更改绘图对象的相关组件。
# 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))
我正在尝试更改 stl
分解图的分面标签,如下所示:
library(ggplot2)
library(ggfortify)
p <- autoplot(stl(AirPassengers, s.window = 'periodic'), ts.colour = "black", ts.size = 0.2)
p
剧情来源于ggfortify package。 我希望将分面标签更改为:
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"))
如何在顶部不显示这些空图的情况下更改构面标签?
一种可能是更改绘图对象的相关组件。
# 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))