代码之家  ›  专栏  ›  技术社区  ›  Alexander

更改镶嵌面标签的外观大小

  •  16
  • Alexander  · 技术社区  · 9 年前

    我知道问题是在这里提出的: Is there a way to increase the height of the strip.text bar in a facet?

    我想降低带子的高度。文本栏而不更改文本大小。在当前情况下,文本和条形图墙之间始终留有空间。

    这是我迄今为止所尝试的,

    library(gcookbook) # For the data set
    library(ggplot2)
    
    ggplot(cabbage_exp, aes(x=Cultivar, y=Weight)) + geom_bar(stat="identity") +
    facet_grid(.~ Date) +
    theme(strip.text = element_text(face="bold", size=9,lineheight=5.0),
    strip.background = element_rect(fill="lightblue", colour="black",
    size=1))
    

    在我看来 lineheight 即使更改为,也不会影响任何内容 5 为什么?
    如何使条形图的大小稍小一些,但保持文本大小不变?

    enter image description here

    在@Sandy Muspratt回答后编辑

    如果只有一排 facets .

    g = ggplotGrob(p)
    g$heights[c(3)] = unit(.4, "cm")  # Set the height
    
    grid.newpage()
    grid.draw(g)
    

    enter image description here

    然而,在我的真实数据中,我有很多像下面这样的绘图行,当我改变g$heights的元素时,什么都没有发生!

    p = ggplot(cabbage_exp, aes(x=Cultivar, y=Weight)) + geom_bar(stat="identity") +
      facet_wrap(~ Date,ncol = 1) +
      theme(strip.text = element_text(face="bold", size=9),
            strip.background = element_rect(fill="lightblue", colour="black",size=1))
    

    enter image description here

     g = ggplotGrob(p)
    g$heights
    #    [1] 5.5pt               0cm                 0.66882800608828cm  #1null               0cm                 0.193302891933029cm
    #     [7] 0.66882800608828cm  1null               0cm                 #0.193302891933029cm 0.66882800608828cm  1null              
    #    [13] 0.456194824961948cm 0cm                 1grobheight         5.5pt
    

    然后我试图改变 1,7 and 11 元素

    g$heights[c(3,7,11)] = unit(.4, "cm")  # Set the height
    
    grid.newpage()
    grid.draw(g)
    

    enter image description here

    刻面标签大小没有变化。

    > g$heights
     [1] 5.5pt                                                       1grobheight                                                
     [3] sum(0.2cm, sum(0.15cm, 0.8128cm, 0cm, 0.15cm), 0.2cm)+0.2cm 0.2                                                        
     [5] 1null                                                       0cm                                                        
     [7] 0.193302891933029cm                                         0.2                                                        
     [9] 1null                                                       0cm                                                        
    [11] 0.193302891933029cm                                         0.2                                                        
    [13] 1null                                                       0cm                                                        
    [15] 0.193302891933029cm                                         0.2                                                        
    [17] 1null                                                       0.456194824961948cm                                        
    [19] 0cm                                                         1grobheight                                                
    [21] 5.5pt  
    
    1 回复  |  直到 4 年前
        1
  •  23
  •   Community CDub    7 年前

    使用边距

    来自ggplot2版本2.1.0:In theme ,在 strip_text 元素(请参见 here ).

    library(ggplot2)
    library(gcookbook) # For the data set
    
    p = ggplot(cabbage_exp, aes(x=Cultivar, y=Weight)) + geom_bar(stat="identity") +
    facet_grid(. ~ Date) +
    theme(strip.text = element_text(face="bold", size=9),
    strip.background = element_rect(fill="lightblue", colour="black",size=1))
    
      p +
      theme(strip.text.x = element_text(margin = margin(.1, 0, .1, 0, "cm")))
    



    原始答案更新为ggplot2 v2.2.0

    您的facet_grid图表

    这将减小条带的高度(如果需要,可以一直减小到零高度)。需要为一条带材和三条线材设置高度。这将适用于特定的facet_grid示例。

    library(ggplot2)
    library(grid)
    library(gtable)
    library(gcookbook) # For the data set
    
    p = ggplot(cabbage_exp, aes(x=Cultivar, y=Weight)) + geom_bar(stat="identity") +
    facet_grid(. ~ Date) +
    theme(strip.text = element_text(face="bold", size=9),
    strip.background = element_rect(fill="lightblue", colour="black",size=1))
    
    g = ggplotGrob(p)
    
    g$heights[6] = unit(0.4, "cm")  # Set the height
    
    for(i in 13:15) g$grobs[[i]]$heights = unit(1, "npc") # Set height of grobs
    
    grid.newpage()
    grid.draw(g)
    

    您的Facet_wrap图表

    这页下面有三条。因此,有三个带材高度需要更改,三个线材高度也需要更改。

    以下内容将用于您的特定facet_wrap示例。

    p = ggplot(cabbage_exp, aes(x=Cultivar, y=Weight)) + geom_bar(stat="identity") +
      facet_wrap(~ Date,ncol = 1) +
      theme(strip.text = element_text(face="bold", size=9),
            strip.background = element_rect(fill="lightblue", colour="black",size=1))
    
    g = ggplotGrob(p)
    
    for(i in c(6,11,16)) g$heights[[i]] = unit(0.4,"cm")   # Three strip heights changed
    for(i in c(17,18,19)) g$grobs[[i]]$heights <-  unit(1, "npc")   # The height of three grobs changed
    
    grid.newpage()
    grid.draw(g)
    

    如何找到相关的高度和长度?

    g$heights 返回高度向量。1空高度是绘图面板。带材高度为之前的一个,即6、11、16。

    g$layout 返回最后一列中包含grob名称的数据帧。需要改变身高的人是那些名字以“strip”开头的人。它们在第17、18、19行。

    概括一下

    p = ggplot(cabbage_exp, aes(x=Cultivar, y=Weight)) + geom_bar(stat="identity") +
      facet_wrap(~ Date,ncol = 1) +
      theme(strip.text = element_text(face="bold", size=9),
            strip.background = element_rect(fill="lightblue", colour="black",size=1))
    
    g = ggplotGrob(p)
    
    # The heights that need changing are in positions one less than the plot panels
    pos =  c(subset(g$layout, grepl("panel", g$layout$name), select = t))
    for(i in pos) g$heights[i-1] = unit(0.4,"cm")
    
    # The grobs that need their heights changed:
    grobs = which(grepl("strip", g$layout$name))
    for(i in grobs) g$grobs[[i]]$heights <-  unit(1, "npc")      
    grid.newpage()
    grid.draw(g)
    

    每行多个面板

    几乎相同的代码可以使用,即使标题和图例位于顶部。计算中有变化 pos ,但即使没有更改,代码也会运行。

    library(ggplot2)
    library(grid)
    
    # Some data
    df = data.frame(x= rnorm(100), y = rnorm(100), z = sample(1:12, 100, T), col = sample(c("a","b"), 100, T))
    
    # The plot
    p = ggplot(df, aes(x = x, y = y, colour = col)) +
       geom_point() +
       labs(title = "Made-up data") + 
       facet_wrap(~ z, nrow = 4) +
       theme(legend.position = "top")
    
    g = ggplotGrob(p)
    
    # The heights that need changing are in positions one less than the plot panels
    pos =  c(unique(subset(g$layout, grepl("panel", g$layout$name), select = t)))
    for(i in pos) g$heights[i-1] = unit(0.2, "cm")
    
    # The grobs that need their heights changed:
    grobs = which(grepl("strip", g$layout$name))
    for(i in grobs) g$grobs[[i]]$heights <-  unit(1, "npc") 
    
    grid.newpage()
    grid.draw(g)