当应用于具有facet_wrap的盒图时,删除一个tableGrob [英] Removing one tableGrob when applied to a box plot with a facet_wrap

查看:345
本文介绍了当应用于具有facet_wrap的盒图时,删除一个tableGrob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码来丰富一个箱形图,其中包含在x轴上创建的分类变量的汇总表。

 #Libs 
require(ggplot2); require(gridExtra); require(grid); require(ggthemes)

#Data
data(mtcars)

#用于汇总数据的函数
fun_dta_sum< - function(var_sum,数据){
sum_dta< - data.frame(
aggregate(var_sum_group,FUN = min,data = data),
aggregate(var_sum_group,FUN = max,data =数据),
聚合(var_sum_group,FUN = mean,data = data))

sum_dta < - sum_dta [,c(1,2,4,6)]
colnames(sum_dta)< - c(Group(x axis),min,max,mean)
rownames(sum_dta)< - NULL
sum_dta [ -1] <-round(sum_dta [, - 1],1)
return(sum_dta)

}

#图
ggplot data = mtcars,aes(x = cyl,y = qsec,fill = as.factor(gear)))+
scale_x_discrete()+
geom_boxplot(outlier.shape = NA)+
scale_y_continuous(limits = quantile(mtcars $ qsec,c(0.1,0.9)))+
scale_fill_tableau(palette =tableau10)+
xlab(am)+ ylab
facet_wrap(〜am,shrink = TRUE)+
theme_pander()+
annotation_custom(tableGrob(
fun_dta_sum(var_sum = mtcars $ qsec,group = mtcars $ cyl,
data = mtcars)
))+
theme(axis.title = element_text(color ='black',face ='bold',size = 12,
family ='sans' ),
axis.text.x = element_text(color ='black',size = 14,hjust = 1,vjust = 0.5),
axis.text.y = element_text(color ='black' ,size = 12),
axis.line = element_line(size = 1,color ='black'),
plot.title = element_text(size = 17,face =bold,color =黑色),
panel.background = element_rect(fill = NA,color ='black'),
panel.grid.major = element_line(color ='grey',linetype ='dotted'
panel.grid.minor = element_line(color ='gray',linetype ='dotted'),
panel.margin = unit(1,lines),
strip.background = element_rect(fill = NA,color = NA),
strip.text = element_text(color ='black',face ='plain',size = 13),
plot.background = element_rect NA,color ='black',size = 0.25),
plot.margin = unit(c(10,10,10,10),mm),
legend.position =bottom ,
legend.background = element_rect(color =black))



我想以下列方式修改代码:


  1. 我只需要一个表格,而不是两个

  2. 我希望表格显示在左侧第一个框图的右上角

  3. 我不想让 rownames 或其他任何产生斜体的数字( 1,2,3

  4. 解决方案

    这可能是有意义的,让annotation_custom访问facetting信息*;这个小改变似乎做的诀窍,

     库(ggplot2)
    库(gridExtra)

    annotation_custom2 < -
    函数(grob,xmin = -Inf,xmax = Inf,ymin = -Inf,ymax = Inf,data)
    {
    layer(data = data,stat = StatIdentity,position = PositionIdentity,
    geom = ggplot2 ::: GeomCustomAnn,
    inherit.aes = TRUE,params = list(grob = grob,
    xmin = xmin,xmax = xmax,
    ymin = ymin,ymax = ymax))
    }

    p < - ggplot(mtcars)+ geom_point )+ facet_wrap(〜cyl)


    tg < - tableGrob(iris [1:2,1:2],rows = NULL)
    #注释区
    tg $ vp = viewport(x = unit(0,npc)+ 0.5 * sum(tg $ width),
    y = unit(0,npc)+ 0.5 * sum tg $ heights))
    #需要在gTree中换行,因为annotation_custom会覆盖vp
    g< - grobTree(tg)
    p + annotation_custom2(g,data = data.frame ))

    编辑 * hadley


    I'm using the code below to enrich a box plot with a summary table for categorical variable created on the x-axis.

    # Libs
    require(ggplot2); require(gridExtra); require(grid); require(ggthemes)
    
    # Data
    data(mtcars)
    
    # Function to summarise the data
    fun_dta_sum <- function(var_sum, group, data) {
        sum_dta <- data.frame(
            aggregate(var_sum ~ group, FUN = min, data = data),
            aggregate(var_sum ~ group, FUN = max, data = data),
            aggregate(var_sum ~ group, FUN = mean, data = data))
    
        sum_dta <- sum_dta[,c(1,2,4,6)]
        colnames(sum_dta) <- c("Group (x axis)", "min", "max", "mean")
        rownames(sum_dta) <- NULL
        sum_dta[,-1] <-round(sum_dta[,-1],1)
        return(sum_dta)
    
    }
    
    # Graph
    ggplot(data = mtcars, aes(x = cyl, y = qsec, fill = as.factor(gear))) +
        scale_x_discrete() +
        geom_boxplot(outlier.shape = NA) +
        scale_y_continuous(limits = quantile(mtcars$qsec, c(0.1, 0.9))) +
        scale_fill_tableau(palette = "tableau10") +
        xlab("am") + ylab("qsec") +
        facet_wrap(~am, shrink = TRUE) +
        theme_pander() +
        annotation_custom(tableGrob(
            fun_dta_sum(var_sum = mtcars$qsec, group = mtcars$cyl, 
                        data = mtcars)
        )) +
        theme(axis.title = element_text(colour = 'black', face = 'bold', size = 12,
                                        family = 'sans'),
              axis.text.x = element_text(colour = 'black', size = 14, hjust = 1, vjust = 0.5),
              axis.text.y = element_text(colour = 'black', size = 12),
              axis.line = element_line(size = 1, colour = 'black'),
              plot.title = element_text(size = 17, face = "bold", colour = "black"),
              panel.background = element_rect(fill = NA, colour = 'black'),
              panel.grid.major = element_line(colour = 'gray', linetype = 'dotted'),
              panel.grid.minor = element_line(colour = 'gray', linetype = 'dotted'),
              panel.margin = unit(1,"lines"),
              strip.background = element_rect(fill = NA, colour = NA),
              strip.text = element_text(colour = 'black', face = 'plain', size = 13),
              plot.background = element_rect(fill = NA, colour = 'black', size = 0.25),
              plot.margin = unit(c(10,10,10,10),"mm"),
              legend.position = "bottom",
              legend.background = element_rect(colour = "black"))
    

    I'm looking to alter the code in a following manner:

    1. I want only one table, not two
    2. I want for the table to appear in the top right corner of the first box plot from the left
    3. I don't want for the rownames or whatever else creates italicised (1,2,3) figures on the left hand side to appear.

    解决方案

    It would probably make sense to let annotation_custom access facetting info *; this trivial change seems to do the trick,

    library(ggplot2)
    library(grid)
    library(gridExtra)
    
    annotation_custom2 <- 
    function (grob, xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, data) 
    {
      layer(data = data, stat = StatIdentity, position = PositionIdentity, 
            geom = ggplot2:::GeomCustomAnn,
            inherit.aes = TRUE, params = list(grob = grob, 
                                              xmin = xmin, xmax = xmax, 
                                              ymin = ymin, ymax = ymax))
    }
    
    p <- ggplot(mtcars) + geom_point(aes(mpg, wt)) + facet_wrap(~ cyl)
    
    
    tg <- tableGrob(iris[1:2,1:2], rows=NULL)
    # position the table within the annotation area
    tg$vp=viewport(x=unit(0,"npc") + 0.5*sum(tg$widths),
                   y=unit(0,"npc") + 0.5*sum(tg$heights))
    # need to wrap in a gTree since annotation_custom overwrites the vp
    g <- grobTree(tg)
    p + annotation_custom2(g, data=data.frame(cyl=8))
    

    Edit * hadley has a different view though, annotation is designed to appear in all panels. It's not clear to me how to produce the geom equivalent for this particular case, if possible.

    这篇关于当应用于具有facet_wrap的盒图时,删除一个tableGrob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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