【发布时间】:2017-01-13 09:56:45
【问题描述】:
如何将灰色构面标签(A 和 B)更改为带有白色文本的红色背景?
library(data.table)
A = data.table(x = 1:4, y = 1:4, z = c('A','A','B','B'))
ggplot(A) + geom_point(aes(x = x, y = y)) + facet_wrap(~z) + theme_bw()
【问题讨论】:
如何将灰色构面标签(A 和 B)更改为带有白色文本的红色背景?
library(data.table)
A = data.table(x = 1:4, y = 1:4, z = c('A','A','B','B'))
ggplot(A) + geom_point(aes(x = x, y = y)) + facet_wrap(~z) + theme_bw()
【问题讨论】:
【讨论】:
对于其他希望更改单个方面标签的人,有一个解决方案here:
g <- ggplot_gtable(ggplot_build(p))
stripr <- which(grepl('strip-r', g$layout$name))
fills <- c("red","green","blue","yellow")
k <- 1
for (i in stripr) {
j <- which(grepl('rect', g$grobs[[i]]$grobs[[1]]$childrenOrder))
g$grobs[[i]]$grobs[[1]]$children[[j]]$gp$fill <- fills[k]
k <- k+1
}
grid::grid.draw(g)
【讨论】:
Error in *tmp*[[j]] : attempt to select less than one element in get1index。有什么想法吗?
striprt <- which( grepl('strip-r', g$layout$name) | grepl('strip-t', g$layout$name) ) 和 fills <- c("red","green","blue","yellow", "red","green","blue")(并将第 5 行中的 stripr 更改为 striprt。2. 随着最近对 R 的重大更新,可能即将推出对 ggplot 和网格包的更改也会改变事情。¯_(ツ)_/¯
g$grobs[[i]]$grobs[[1]]$childrenOrder) 中没有包含'rect' 的任何内容供grepl 查找。