将x和y轴添加到所有facet_wrap

Tyl*_*ker 30 r facet ggplot2 facet-wrap

通常希望最小化图中的墨水.我有一个刻面的情节(facet_wrap),并希望删除尽可能多的墨水,但保持可读性.我已按照我的意愿进行设置,除非在小平面(子图)中不存在x和y轴,除非在最左侧或底部.如此大量的墨水被移除,我相信眼睛需要这些暗示,并且正在询问如何将x和y轴放在a中的所有图中facet_wrap.下面是我的代码到目前为止,当前输出和所需的输出(红线是所需的加入):

library(ggplot); library(grid)

ggplot(mtcars, aes(mpg, hp)) + 
    geom_point() + 
    facet_wrap(~carb) +
    theme(panel.grid = element_blank(),
        panel.background = element_rect(fill = "white", colour = "black"), 
        panel.border = element_rect(fill = NA, colour = "white"), 
        axis.line = element_line(),
        strip.background = element_blank(),
        panel.margin = unit(2, "lines"))
Run Code Online (Sandbox Code Playgroud)

当前情节 在此输入图像描述

期望的情节 在此输入图像描述

Tho*_*mas 29

这应该大大简化了事情:

library('ggthemes')
ggplot(mtcars, aes(mpg, hp)) + geom_point() + facet_wrap(~carb, scales='free') + 
    theme_tufte() + theme(axis.line=element_line()) + 
    scale_x_continuous(limits=c(10,35)) + scale_y_continuous(limits=c(0,400))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


bap*_*ste 28

最简单的方法是在每个绘图面板中添加分段,

ggplot(mtcars, aes(mpg, hp)) + 
  geom_point() + 
  facet_wrap(~carb) +
  theme_minimal() +
  annotate("segment", x=-Inf, xend=Inf, y=-Inf, yend=-Inf)+
  annotate("segment", x=-Inf, xend=-Inf, y=-Inf, yend=Inf)
Run Code Online (Sandbox Code Playgroud)

例


Tal*_*ili 6

跟随托马斯从上面回答-

你只需要设置scales='free'facet_wrap,并确保在设定的范围scale_x_continuousscale_y_continuous

ggplot(mtcars, aes(mpg, hp)) + geom_point() + facet_wrap(~carb, scales='free') + 
    scale_x_continuous(limits=c(10,35)) + scale_y_continuous(limits=c(0,400))
Run Code Online (Sandbox Code Playgroud)


Aar*_*ica 6

柠檬包添加此功能; 看到 这个小插图。(示例代码并从此处进行绘制。)

library(lemon)
p + facet_rep_grid(drv ~ cyl) + coord_capped_cart(bottom='both', left='both') +
  theme_bw() + theme(panel.border=element_blank(), axis.line=element_line())
Run Code Online (Sandbox Code Playgroud)

从柠檬文档:


小智 6

ggh4x我自己刚刚找到了一个简单的解决方案 - 功能可以在下面的包中找到facet_wrap2(~carb, axes = "all", remove_labels = "all")

library(ggplot2)
library(ggh4x)

ggplot(mtcars, aes(mpg, hp)) + 
    geom_point() + 
    facet_wrap2(~carb, axes = "all", remove_labels = "all") + # this can also just by "x" or "y" to remove axis labels
    theme(panel.grid = element_blank(),
        panel.background = element_rect(fill = "white", colour = "black"), 
        panel.border = element_rect(fill = NA, colour = "white"), 
        axis.line = element_line(),
        strip.background = element_blank(),
        panel.margin = unit(2, "lines"))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述