更改刻面标签尺寸的外观

时间:2016-04-22 01:29:47

标签: r ggplot2 facet-wrap

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

我想降低strip.text栏的高度而不改变文字大小。在当前情况下,文本和条形条墙之间总是留有空间。

这是我到目前为止所尝试的,

function titleCase(str)
{
    return str.replace(/\w\S*/g, function(txt) { 
        return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
    });
}

就我而言,即使更改为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似乎也不会影响任何内容。为什么呢?
如何使条形尺寸更小但保持文本大小相同?

enter image description here

在@Sandy Muspratt回答之后编辑

如果只有一行5,我们可以减小条带大小。

facets

enter image description here

然而,在我的真实数据中,我有很多行的情节,如下所示,当我改变g $高度的元素时,没有发生任何事情!

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

grid.newpage()
grid.draw(g)

enter image description here

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

enter image description here

小平面标签尺寸没有变化。

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

grid.newpage()
grid.draw(g)

1 个答案:

答案 0 :(得分:14)

使用边距

从ggplot2 ver 2.1.0开始:在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返回高度向量。 1null高度是情节面板。条带高度是前一个 - 即6,11,16。

g$layout返回一个数据框,其中包含最后一列中的grob的名称。需要改变高度的凹陷是名称以“条带”开头的凹凸。它们排在第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)