简体   繁体   English

ggplot2 facet_grid()strip_text_x()基于因素的不同颜色

[英]ggplot2 facet_grid() strip_text_x() different colours based on factor

Is there a way to colour the names or strip backgrounds (not the backgrounds of the actual grids, as has been answered in this post: Conditionally change panel background with facet_grid? ) of the facets in ggplot2's facet_grid? 有没有一种方法可以为ggplot2的facet_grid中的构面涂上名称或去除背景的颜色(不是实际网格的背景,正如本文已回答的那样: 使用facet_grid有条件地更改面板背景? )?

In the following example: 在以下示例中:

#create simple dataframe for plotting
xy <- data.frame(x = rep(1:10, times=3), y = rep(10:1, times=3), type = rep(LETTERS[1:2], each=5), type2 = rep(LETTERS[3:5], each=10), category = rep(c("control", "exp", "control"), each=10))
xy
#create base plot
plot <- ggplot(data = xy)+
geom_point(aes(x = x, y = y))+
facet_grid(type ~ type2)

#plot base plot   
plot

Is there a way to colour the text or the background of theme(strip.text.x = element_text(size = 11, face="bold")) (?strip.background.x?) depending upon a categorical variable (for example, "category" in the data frame I have specified above, so all the controls have a red strip, all of the exp - a green one)? 有没有一种方法可以根据类别变量为主题的文本或背景着色(strip.text.x = element_text(size = 11,face =“ bold”))(?strip.background.x?) ,我上面已指定的数据框中的“类别”,因此所有控件都有一条红色的条带,所有的exp-绿色的一条)?

Thanks in advance! 提前致谢!

PS What I'm actually trying to do is present the results of a biological experiment: PS我实际上想做的是呈现生物实验的结果:

  • two boxplots for every gene to show the variability; 每个基因有两个箱形图以显示变异性; gene name is in the strip 基因名称在地带

  • the background of the grid is either white or grey, with grey showing genes which are controls 网格的背景是白色或灰色,灰色表示控制基因

  • I'd like to use the colour of the text in the header to denote whether the gene is significant or not in my experiment (I need to show both the validated and non-validated genes in the same plot) 我想使用标题中的文本颜色来表示该基因在我的实验中是否有意义(我需要在同一图中显示经过验证的基因和未经验证的基因)

@Roland taught me to hack around with the grid graphics objects on this question : @Roland教我解决这个问题上的grid图形对象:

Using his advice, you can ( hackily ) traverse the complex grob objects and alter elements at will (but remember ggplot was kinda designed to take those decisions out of your hands for very good reason - you might make some bad graphical design choices!). 根据他的建议,您可以( hackyly )遍历复杂的grob对象并随意更改元素(但请记住, ggplot设计是出于非常充分的理由而将这些决定从您手中ggplot -您可能会做出一些错误的图形设计选择!)。

Try and work out how the below works and you will be able to extend it to change all manner of other elements. 尝试找出以下内容的工作方式,您将可以对其进行扩展以更改其他所有方式。 The $gp is basically the grid equivalent of par options for base plotting. $gp基本上与base绘图的par选项的grid等效。

gplot <- ggplotGrob( plot )
nms <- lapply( gplot$grobs , function(x) names( x[]$children ) )
grbs <- which( sapply( lapply( nms , function(x) grepl( "strip.background.rect" , x ) ) , any ) == 1 )
cl <- c("#2b8cbe","#fc9272","#2b8cbe")
for ( i in 1:length(grbs) ){
    col <- cl[i]
    i <- grbs[i]
    gplot$grobs[[i]]$children[[1]][]$gp$fill <- col

}
require(gridExtra)
grid.arrange( gplot )

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM