【发布时间】:2021-10-01 03:45:46
【问题描述】:
我正在尝试使用带有不平衡分组数据的facet_wrap 生成分面图,它提供了一个带有额外空白轴列的图。
这是一个示例代码:
library(ggplot2)
name <- c(factor(letters[1:4]),factor(LETTERS[1:3]))
room <- rep(c('A','B'),c(4,3))
goal <- c(rnorm(7,mean=60,sd=10))
test <- data.frame(name,goal,room)
test %>% ggplot(aes(name, goal))+
facet_wrap(~factor(room))+
geom_bar(stat = "identity")
'scales="free"' 方式:自动设置,可以手动设置吗?
@teunbrand 开发的ggh4x 中的facetted_pos_scales 解决了这个问题,谢谢!这是补充代码:
library(ggh4x)
scales <- list(
scale_y_continuous(limits = c(0, 100)),
scale_y_continuous(limits = c(0, 80))
)
test %>% ggplot(aes(name, goal))+
facet_wrap(~factor(room), scales="free")+
geom_bar(stat = "identity")+
facetted_pos_scales(y=scales)
【问题讨论】:
-
在
facet_wrap中使用scales = 'free_x',即facet_wrap(~factor(room), scales = 'free_x') -
谢谢!它运作良好。还有一个问题,如何在右侧子图中添加 y 轴并最小化它们之间的间隙?
-
您可以从可以使用
scale_y_continuous(limits = function(x){do_something_here})的预先存在的限制中得出所需的限制。否则,您可能会求助于a bit of a hack(免责声明:我编写了链接函数)。 -
@teunbrand ,真是一个hack包,正是我需要的!这是代码` library(ggh4x) scales % ggplot(aes(name, goal) )+ facet_wrap(~factor(room), scales="free")+ geom_bar(stat = "identity")+ facetted_pos_scales(y=scales) `
标签: r ggplot2 facet-wrap