conditional strip text format

37 views
Skip to first unread message

Dr. Hans Hansen

unread,
May 13, 2014, 9:56:41 AM5/13/14
to ggp...@googlegroups.com
Hi,

is there a way to format the strip text conditional on values in the facet? Like red color when the mean > 10? This unfortunately doesn't work:

df = data.frame(a = rnorm(100), b = letters[1:2])
ggplot(df, aes(x = a)) + geom_density() + facet_grid(.~b) + theme(strip.text.y = element_text(color = c("black", "red")))


Thanks a lot, felix

Dr. Hans Hansen

unread,
May 16, 2014, 3:37:09 AM5/16/14
to ggp...@googlegroups.com
ok, i take that as a not then :P

Allan Just

unread,
May 16, 2014, 10:56:04 AM5/16/14
to ggp...@googlegroups.com
If you figure out a vector of colors for your facets ahead of time you can hack it like this:

library(ggplot2)
library(grid)

df = data.frame(a = rnorm(100), b = letters[1:2])

ggplot(df, aes(x = a)) + geom_density() + facet_grid(.~b) + theme(strip.text.y = element_text(color = c("black", "red")))

mapply(
 function(g, co)grid.edit(g$name, gp=gpar(fill=co)),
 grid.gget(gPath("strip.background"),grep=T),
 c("#F8766D", "#00BA38")
 )

#R 3.1.0 ggplot2_0.9.3.1 on Win7 64

Dr. Hans Hansen

unread,
May 16, 2014, 11:16:37 AM5/16/14
to ggp...@googlegroups.com

that's great, thanks!
Reply all
Reply to author
Forward
0 new messages