ggplot facet_wrap에서 하위 그림 순서를 다시 정렬하고 하위 그림 텍스트 레이블에 특정 색상을 지정하려고합니다.
내 데이터 세트는 다음과 같습니다.
ID <- rep(c('ABC123','DEF456','GHI789','JKL012'),each = 10)
Vref <- c((runif(10,1,2)),(runif(10,3,5)),(runif(10,6,9)),(runif(10,0,2)))
Time <- rep(c(1:10),4)
df <- data.frame(ID,Vref,Time)
ggplot(df) + geom_point(aes(x=Time, y=Vref)) + facet_wrap(~ID, nrow = 2)
이제 하위 그림의 순서는 ABC123-> DEF456-> GHI789-> JKL012입니다. 모든 텍스트 레이블은 검은 색입니다.
내 질문은 :
순서를 GHI789-> ABC123-> JKL012-> DEF456으로 변경하는 방법은 무엇입니까? 빨간색, 녹색, 노란색 및 파란색으로 색상을 지정하는 방법은 무엇입니까?
감사.
스트립 텍스트의 색상을 지정하는 한 가지 방법은 grid
패키지의 편집 기능을 사용하는 것 입니다. 일반적으로 grid.ls()
다른 편집 기능은 grid.force()
grob 하나만 볼 수 있지만 ggplot의 모든 grob을 그리드의 편집 기능에 표시합니다.
library(grid)
library(ggplot2)
ID <- rep(c('ABC123','DEF456','GHI789','JKL012'),each = 10)
Vref <- c((runif(10,1,2)),(runif(10,3,5)),(runif(10,6,9)),(runif(10,0,2)))
Time <- rep(c(1:10),4)
df <- data.frame(ID,Vref,Time)
# Change order of strip texts
df$ID = factor(df$ID, levels = c("GHI789", "ABC123", "JKL012", "DEF456"))
p = ggplot(df) + geom_point(aes(x=Time, y=Vref)) + facet_wrap(~ID, nrow = 2)
gp <- ggplotGrob(p) # Get ggplot grob
# Get the names of the grobs
# grid.force makes all the grobs visible to grid's editing functions
names.grobs <- grid.ls(grid.force(gp))$name
# Inspect the list. The required grobs' names begin with GRID.text,
# but so do other text grobs - to do with the axes.
# Therefore, in the edit, use gPath to limit the editing to GRID.text in the strip,
# and not the GRID.text in the axes.
strip.text <- names.grobs[which(grepl("GRID.text", names.grobs))]
# Set up the colours
colour <- c("yellow", "blue", "red", "green")
# The edit
for(i in 1:4) gp = editGrob(grid.force(gp),
gPath("GRID.titleGrob", strip.text[i]),
grep = TRUE,
gp = gpar(col = colour[i]))
# Draw it
grid.newpage()
grid.draw(gp)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다