ggplot2 facet_grid()strip_text_x()基于因子的不同颜色 [英] ggplot2 facet_grid() strip_text_x() different colours based on factor

查看:358
本文介绍了ggplot2 facet_grid()strip_text_x()基于因子的不同颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法给名称或条形背景着色(不是实际网格的背景,正如已在本文中回答的那样:

在以下例如:

$ $ p $ code $#为绘图
创建简单的数据框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

有没有办法为主题的文字或背景着色(strip.text.x = element_text( size = 11,face =bold))(?strip.background.x?)dep结束于一个分类变量(例如,我在上面指定的数据框中的category,所以所有的控件都有一个红色条,所有的exp - 一个绿色的)?

预先感谢!



PS
我实际上想要做的是呈现生物学实验的结果:




  • 每个基因的两个盒图显示变异性;基因名称在条带中


  • 网格的背景为白色或灰色,灰色显示为对照的基因


  • 我想在标题中使用文本的颜色来表示基因在我的实验中是否有意义(我需要将验证和未验证的基因同时显示在同样的情节)

  • 此问题 中的 grid 图形对象:



    使用他的建议,你可以( hackily )遍历复杂的 grob 对象并且可以随意更改元素(但记住 ggplot 的设计目的是为了将这些决策从您的手中拿出来,原因很可能是 - 您可能会做出一些糟糕的图形设计选择! / p>

    试着研究下面的工作方式,你可以将它扩展到c改变其他所有
    元素的方式。 $ gp 基本上是等价于 par 选项的网格 base 绘图。

      gplot < -  ggplotGrob(plot)$ bpp(gplot $ grobs,function(x)names(x [] $ children))
    grbs< - (sapply(lapply(nms,function(x)grepl(strip
    cl< -c(#2b8cbe,#fc9272,#2b8cbe)
    for(i in 1 :长度(grbs)){
    col< - cl [i]
    i < - grbs [i]
    gplot $ grobs [[i]] $ children [[1]] [ ] $ gp $ fill< - col

    }
    require(gridExtra)
    grid.arrange(gplot)


    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?

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

    Thanks in advance!

    PS What I'm actually trying to do is present the results of a biological experiment:

    • 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:

    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!).

    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.

    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 )
    

    这篇关于ggplot2 facet_grid()strip_text_x()基于因子的不同颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆