更改构面标签大小的外观 [英] Changing the Appearance of Facet Labels size
问题描述
我知道这里提出的问题是:
在@Sandy Muspratt答案后编辑
如果有的话,我们可以减少带的大小只有一行 facets
。
g = ggplotGrob(p)
g $ heights [c(3)] =单位(.4,cm)#设置高度
grid.newpage()
grid.draw(g)
然而,在我的真实数据中,我有许多行,如下图所示,当我改变g $ heights的元素时,什么也没有发生!
p = ggplot(cabbage_exp,aes(x = Cultivar,y = Weight))+ geom_bar(stat =identi ty)+
facet_wrap(〜Date,ncol = 1)+
主题(strip.text = element_text(face =bold,size = 9),
strip.background = element_rect (fill =lightblue,color =black,size = 1))
g = ggplotGrob(p)
g $高度
#[ 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和11
元素
g $ heights [c(3,7,11)] =单位(.4,cm)#设置高度
grid.newpage()
gr id.draw(g)
标签尺寸不变。
> 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
使用边距
从ggplot2版本2.1.0开始:在主题
,请在 strip_text
元素中指定边距(请参阅
$ b
library(ggplot2)
library(size-ggplot2 / 41428958#41428958> here ) gcookbook)#对于数据集
p = ggplot(cabbage_exp,aes(x = Cultivar,y = Weight))+ geom_bar(stat =identity)+
facet_grid(。 〜日期)+
主题(strip.text = element_text(face =bold,size = 9),
strip.background = element_rect(fill =lightblue,color =black,size = 1))
p +
theme(strip.text.x = element_text(margin = margin(.1,0,.1,0,cm)))
原始答案已更新为ggplot2 v2.2.0
$ b $ h2您的facet_grid图表
这会降低条带的高度(全部如果你想要的话,以零高度的方式)。高度需要设置为一个条和三个槽。这将与您的特定facet_grid示例一起使用。
库(ggplot2)
库(网格)
库(gtable)
库( gcookbook)#对于数据集
p = ggplot(cabbage_exp,aes(x = Cultivar,y = Weight))+ geom_bar(stat =identity)+
facet_grid(。〜Date )+
主题(strip.text = element_text(face =bold,size = 9),
strip.background = element_rect(fill =lightblue,color =black,size = 1 ))
g = ggplotGrob(p)
g $ heights [6] =单位(0.4,cm)#设置身高
(我在13:15)g $ grobs [[i]] $ heights = unit(1,npc)#设置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,color =black,size = 1))
g = ggplotGrob(p)
for(i in c(6,11,16))g $ heights [[i ]] =单位(0.4,厘米)#三条高度改变
for(i in c(17,18,19))g $ grobs [[i]] $ heights< - unit(1, npc)#三个grobs的高度改为
grid.newpage()
grid.draw(g)
如何找到相关的高度和汇率?
g $ heights
返回一个高度向量。 1null高度是情节板。带高度是前一个 - 即6,11,16。
g $ layout
返回数据帧与最后一列中的grobs名称。需要高度的grobs改变了名字以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,color =black,size = 1))
g = ggplotGrob(p)
#需要改变的高度位于小于面板的位置
pos = c(subset(g $ layout,grepl(panel, $ g $ heights [i-1] =单位(0.4,cm)
#grobs
grobs =哪个(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)
#一些数据
df = data.frame(x = rnorm(100),y = rnorm (100),z = sample(1:12,100,T),col = sample(c(a,b),100,T))
#plot
p = ggplot(df,aes(x = x,y = y,color = col))+
geom_point()+
实验室(title =编制数据)+
facet_wrap(〜z,nrow = 4)+
theme(legend.position =top)
g = ggplotGrob(p)
#需要改变的位置比绘图板小
pos = c(唯一(子集(g $ layout,grepl(panel,g $ layout $ name),select = t)))
对于(我在pos中)g $ heights [i-1] =单位(0.2,cm)
#需要高度的grobs变为:
grobs = which(grepl( strip,g $ layout $ name))
for(i in grobs)g $ grobs [[i]] $ heights< - unit(1,npc)
grid.newpage()
grid.draw(g)
I know the question was asked here: Is there a way to increase the height of the strip.text bar in a facet?
I want to decrease the height of the strip.text bar without changing the text size. In the current case there is always a space left between text and strip bar walls.
Here is what I tried so far,
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))
In my case it seems that lineheight
does not affect anything even if changed to 5
. Why?
How can I make the strip bar size a little smaller but keeping the text size the same?
edit after @Sandy Muspratt answer
we are able to reduce the strip size if there is only one row of facets
.
g = ggplotGrob(p)
g$heights[c(3)] = unit(.4, "cm") # Set the height
grid.newpage()
grid.draw(g)
However, in my real data I have many rows of plot like below and when I changed the elements of g$heights nothing happened!
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
then I attempted to change 1,7 and 11
elements
g$heights[c(3,7,11)] = unit(.4, "cm") # Set the height
grid.newpage()
grid.draw(g)
No change in the facet label size.
> 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
Use margins
From about ggplot2 ver 2.1.0: In theme
, specify margins in the strip_text
element (see 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")))
The original answer updated to ggplot2 v2.2.0
Your facet_grid chart
This will reduce the height of the strip (all the way to zero height if you want). The height needs to be set for one strip and three grobs. This will work with your specific facet_grid example.
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)
Your Facet_wrap chart
There are three strips down the page. Therefore, there are three strip heights to be changed, and the three grob heights to be changed.
The following will work with your specific facet_wrap example.
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)
How to find the relevant heights and grobs?
g$heights
returns a vector of heights. The 1null heights are the plot panels. The strip heights are one before - that is 6, 11, 16.
g$layout
returns a data frame with the names of the grobs in the last column. The grobs that need their heights changed are those with names beginning with "strip". They are in rows 17, 18, 19.
To generalise a little
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)
Multiple panels per row
Nearly the same code can be used, even with a title and a legend positioned on top. There is a change in the calculation of pos
, but even without that change, the code runs.
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)
这篇关于更改构面标签大小的外观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!