我知道问题在这里被问到:有没有办法增加strip.text条的高度?
我想降低strip.text栏的高度而不改变文本大小.在当前情况下,文本和条形条墙之间总是留有空间.
这是我到目前为止所尝试的,
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))
Run Code Online (Sandbox Code Playgroud)
在我的情况下,似乎lineheight
即使更改为也不会影响任何内容5
.为什么?
如何使条形尺寸稍小但保持文字大小相同?
如果只有一行,我们可以减小条带尺寸facets
.
g = ggplotGrob(p)
g$heights[c(3)] = unit(.4, "cm") # Set the height
grid.newpage()
grid.draw(g)
Run Code Online (Sandbox Code Playgroud)
然而,在我的真实数据中,我有很多行如下图,当我改变g $高度的元素时,没有发生任何事情!
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))
Run Code Online (Sandbox Code Playgroud)
g = ggplotGrob(p)
g$heights …
Run Code Online (Sandbox Code Playgroud) 我想完美地对齐这些情节:
这是R代码:
library(tidyverse)
library(gridExtra)
groupes <- tribble(~type, ~group, ~prof, ~var,
1,1,1,12,
1,1,2,-24,
1,2,1,-11,
1,2,2,7,
2,1,1,10,
2,1,2,5,
2,2,1,-25,
2,2,2,2,
2,3,1,10,
2,3,2,3,
3,1,1,10,
3,1,2,-5,
3,2,1,25,
3,2,2,2,
3,3,1,-10,
3,3,2,3,
3,4,1,25,
3,4,2,-18)
hlay <- rbind(c(1,2,3),
c(1,2,3),
c(NA,2,3),
c(NA,NA,3))
p1 <- groupes %>% filter(type==1) %>% ggplot(aes(prof,var)) + geom_col() + facet_wrap(~group,ncol=1) +
coord_cartesian(ylim=c(-25,25)) +
labs(title="type 1",x="",y="")
p2 <- groupes %>% filter(type==2) %>% ggplot(aes(prof,var)) + geom_col() + facet_wrap(~group,ncol=1) +
coord_cartesian(ylim=c(-25,25)) +
labs(title="type 2",x="",y="")
p3 <- groupes %>% filter(type==3) %>% ggplot(aes(prof,var)) + geom_col() + facet_wrap(~group,ncol=1) +
coord_cartesian(ylim=c(-25,25)) + …
Run Code Online (Sandbox Code Playgroud) 有没有办法控制ggplot中刻面上条带的大小?我尝试过使用strip.background=element_rect(size=n)
但据我所知,它实际上没有做任何事情.这甚至可能吗?
例如,
library(ggplot2)
ggplot(mpg, aes(displ, cty)) + geom_point() + facet_grid(cols = vars(drv))
Run Code Online (Sandbox Code Playgroud)
如何更改条带和主图之间的距离?(例如,在条带和主图之间创建一个间隙。)
但我不需要更改条带大小(与此编辑条带大小 ggplot2 不同)。
我有一个带有灰色小平面和白色小平面文字的白色背景上的情节:
ggplot(data = data.frame(x = rep(1:2, 2), y = rep(1:2,2), color = c("Ap", "Ap", "B", "B")),
aes(x = x, y = y, color = color)) +
geom_point() +
facet_grid(~color) +
theme(panel.background = element_blank(),
strip.text = element_text(color = "white", size = 23))
Run Code Online (Sandbox Code Playgroud)
我的问题是下行(p,g,q,j)越过了方面.我希望条带背景在文本周围有一个边距,以便构面文本中的所有字形始终严格在构面矩形内.我可以在方面文本中添加换行符,color = c("Ap\n", "Ap\n", "B\n", "B\n")
但边距太大(或者lineheight
要求太难看了).这有ggplot2
解决方案吗?