Change facet labels for a ggplot created by ggfortify::autoplot

MattV

I'm trying to change the facet labels for an stl decomposition plot like the following:

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

The plot originates from the ggfortify package. I wish to change the facet labels to:

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

I've tried to get into the structure of a ggplot (a lot of str'ing), and found that the following stores these names:

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

However, when I change this factor in-place. I get four empty plots followed by the proper plots:

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

Outcome when editing factor in-place

How do I change the facet labels without getting these empty plots at the top?

Henrik

One possibility is to change the relevant components of the plot object.

# 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))

enter image description here

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Why does stacked bar plot change when add facet in r ggplot2

분류에서Dev

R : 그래프 플로팅 (ggplot vs autoplot)

분류에서Dev

add labels to facet_wrap boxplot

분류에서Dev

sf 용 ggplot의 facet_wrap

분류에서Dev

ggplot中带有facet_wrap的facet不同的alpha值

분류에서Dev

Align labels to pyramid barplot in ggplot2

분류에서Dev

Dynamic legend labels in ggplot2

분류에서Dev

Two column/row Positioning of labels in ggplot

분류에서Dev

Flow duration curve using facet_wrap of ggplot in R?

분류에서Dev

ggplot facet_grid x 축 단계

분류에서Dev

R : facet_grid (ggplot)의 t.test

분류에서Dev

y 매핑이 다른 ggplot facet_grid

분류에서Dev

ggplot2의 facet_grid 오류

분류에서Dev

GGplot_annotate 및 facet_wrap 함수

분류에서Dev

Change properties of programmatically created buttons

분류에서Dev

ggplot2의 scale_x_labels 오류

분류에서Dev

facet_wrap 사용시 ggplot 재정렬 변경

분류에서Dev

ggplot의 facet_grid에서 행 방향 색상 지정

분류에서Dev

ggplot2 :: facet_wrap ()의 기본 패널 레이아웃?

분류에서Dev

패싯 된 ggplot (facet_wrap)을 R의 cowplot과 정렬

분류에서Dev

ggplot2에서 facet_wrap () 수동 중단

분류에서Dev

ggplot에서 facet_grid를 사용한 플로팅 의미

분류에서Dev

패싯 제목이있는 ggplot2 facet_grid

분류에서Dev

ggplot의 facet_grid와 bmrs conditional_effects 결합

분류에서Dev

R에서 ggplot2의 ggforce facet_zoom 오류

분류에서Dev

Add multiple titles/text above brackets in facet_wrap ggplot in R

분류에서Dev

facet_wrap 플롯에 다른 ggplot 추가 / 주석

분류에서Dev

ggplot + facet : 데이터가없는 감소 요인

분류에서Dev

ggplot + facet : 데이터가없는 감소 요인