R 制作一个ggplot';s条带文本延伸到轴文本上
使用ggplot2,我想制作一个刻面图,其中条形文本在每个子图及其y轴文本上居中,而不是仅在子图上居中。以下是我希望创建的绘图示例,以供参考: 期望图 这与相同的绘图相反,但条形文本仅位于子绘图区域的中心,如下所示 默认ggplot2绘图 以下是用于生成此绘图的代码,以供参考:R 制作一个ggplot';s条带文本延伸到轴文本上,r,ggplot2,gridextra,R,Ggplot2,Gridextra,使用ggplot2,我想制作一个刻面图,其中条形文本在每个子图及其y轴文本上居中,而不是仅在子图上居中。以下是我希望创建的绘图示例,以供参考: 期望图 这与相同的绘图相反,但条形文本仅位于子绘图区域的中心,如下所示 默认ggplot2绘图 以下是用于生成此绘图的代码,以供参考: library(ggplot2) library(dplyr) diamonds_plot <- diamonds %>% filter(clarity %in% c("IF"
library(ggplot2)
library(dplyr)
diamonds_plot <- diamonds %>%
filter(clarity %in% c("IF", "I1")) %>%
ggplot(aes(x = price, y = cut)) +
geom_point() +
facet_wrap(~ clarity, scales = 'free')
库(ggplot2)
图书馆(dplyr)
钻石图%
过滤器(在%c(“如果”,“I1”)中的透明度%)%>%
ggplot(aes(x=价格,y=切割))+
几何点()+
面_包裹(~清晰度,刻度=‘自由’)
如何以编程方式进行此更改
我猜这可以通过编辑从调用
diamonds\u plot%>%ggplot\u build%>%ggplot\u gtable()
获得的布局对象来实现,但是要进行的确切更改很难确定。是的,您的想法是正确的
g <- ggplotGrob(diamonds_plot)
g$layout
# t l b r z clip name
# 20 1 1 11 11 0 on background
# 1 7 4 7 4 1 on panel-1-1
# 2 7 8 7 8 1 on panel-2-1
# 3 5 4 5 4 3 off axis-t-1-1
# 4 5 8 5 8 3 off axis-t-2-1
# 5 8 4 8 4 3 off axis-b-1-1
# 6 8 8 8 8 3 off axis-b-2-1
# 7 7 7 7 7 3 off axis-l-1-2
# 8 7 3 7 3 3 off axis-l-1-1
# 9 7 9 7 9 3 off axis-r-1-2
# 10 7 5 7 5 3 off axis-r-1-1
# 11 6 4 6 4 2 on strip-t-1-1
# 12 6 8 6 8 2 on strip-t-2-1
# 13 4 4 4 8 4 off xlab-t
# 14 9 4 9 8 5 off xlab-b
# 15 7 2 7 2 6 off ylab-l
# 16 7 10 7 10 7 off ylab-r
# 17 3 4 3 8 8 off subtitle
# 18 2 4 2 8 9 off title
# 19 10 4 10 8 10 off caption
g或更多编程方式:g$layout$l[grepl(“strip-t”,gp$layout$name)]多么优雅的解决方案!你知道移位是否总是需要“-1”?@bschneidr y轴是绘图左侧的一个表格“列”<代码>gtable::gtable_show_布局(g)
可能会帮助您可视化
g$layout$l[g$layout$name == "strip-t-1-1"] <- 3
g$layout$l[g$layout$name == "strip-t-2-1"] <- 7
# or more programmatically, with thanks to Mike H.
# g$layout$l[grepl("strip-t", gp$layout$name)] <- g$layout$l[grepl("strip-t", gp$layout$name)] - 1
grid::grid.draw(g)