ggplot2:具有独立“Y”轴的不同小平面宽度
ggplot2: Varying facet width with independent `Y` axes
虚拟数据
d = data.frame(
x = factor(LETTERS[c(1,2,3,4,1,2,3,4,1,2,1,2,1,2,1,2)]),
y = c(100,80,70,60,130,90,65,60,2,3,3,3,2,2,1,2),
grid = rep(letters[1:2], each=8)
)
问题
ggplot(d, aes(x=x, y=y)) + facet_grid(~grid, scales="free",space="free_x") + geom_point()
我喜欢这张图。我唯一的问题是两个网格使用相同的 Y
轴。所以,我尝试使用 facet_wrap
而不是 facet_grid
并得到
ggplot(d, aes(x=x, y=y)) + facet_wrap(~grid, scales="free") + geom_point()
但不幸的是,facet_wrap
没有 "space" 参数,因此左右图形的宽度相同。
问题
我怎样才能使变量 d$x
的级别之间的 space 在两个方面都相等(导致具有不同宽度的方面)并且有一个单独的 Y
每个方面的轴。当然,我希望小平面保持水平对齐。
使用 ggplot grob 并修改 table
中的宽度
# Capture the plot
q = ggplot(d, aes(x=x, y=y)) + facet_grid(~grid, scales="free",space="free_x") + geom_point()
gt = ggplotGrob(q)
# Modify the widths
gt$widths[5] = unit(8, "cm")
gt$widths[9] = unit(4, "cm")
# Plot the graph
grid.newpage()
grid.draw(gt)
虚拟数据
d = data.frame(
x = factor(LETTERS[c(1,2,3,4,1,2,3,4,1,2,1,2,1,2,1,2)]),
y = c(100,80,70,60,130,90,65,60,2,3,3,3,2,2,1,2),
grid = rep(letters[1:2], each=8)
)
问题
ggplot(d, aes(x=x, y=y)) + facet_grid(~grid, scales="free",space="free_x") + geom_point()
我喜欢这张图。我唯一的问题是两个网格使用相同的 Y
轴。所以,我尝试使用 facet_wrap
而不是 facet_grid
并得到
ggplot(d, aes(x=x, y=y)) + facet_wrap(~grid, scales="free") + geom_point()
但不幸的是,facet_wrap
没有 "space" 参数,因此左右图形的宽度相同。
问题
我怎样才能使变量 d$x
的级别之间的 space 在两个方面都相等(导致具有不同宽度的方面)并且有一个单独的 Y
每个方面的轴。当然,我希望小平面保持水平对齐。
使用 ggplot grob 并修改 table
中的宽度# Capture the plot
q = ggplot(d, aes(x=x, y=y)) + facet_grid(~grid, scales="free",space="free_x") + geom_point()
gt = ggplotGrob(q)
# Modify the widths
gt$widths[5] = unit(8, "cm")
gt$widths[9] = unit(4, "cm")
# Plot the graph
grid.newpage()
grid.draw(gt)