37

通常希望尽量减少绘图中的墨水。我有一个多面图 ( 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"))

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

想要的情节 在此处输入图像描述

4

5 回答 5

40

这应该大大简化事情:

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))

在此处输入图像描述

于 2014-03-01T16:20:36.260 回答
33

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

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)

例子

于 2014-03-01T17:15:53.630 回答
15

按照上面的托马斯回答 -

您只需要设置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))
于 2018-03-20T14:00:15.360 回答
12

柠檬包增加了这个功能;看到 这个小插曲。(示例代码和图从那里。)

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())

从柠檬文档:

于 2018-06-11T16:20:50.770 回答
4

这是很长的解决方法。

首先,将原始图存储为对象,然后创建另一个没有轴刻度和轴文本的图。

p1<-ggplot(mtcars, aes(mpg, hp)) + 
  geom_point() + 
  facet_wrap(~carb) +
  theme(panel.grid = element_blank(),
        panel.background = element_blank(), 
        panel.border = element_blank(), 
        axis.line = element_line(),
        strip.background = element_blank(),
        panel.margin = unit(2, "lines"))

p2<-ggplot(mtcars, aes(mpg, hp)) + 
  geom_point() + 
  facet_wrap(~carb) +
  theme(panel.grid = element_blank(),
        panel.background = element_blank(), 
        panel.border = element_blank(), 
        axis.line = element_line(),
        strip.background = element_blank(),
        panel.margin = unit(2, "lines"),
        axis.ticks=element_blank(),
        axis.text=element_blank())

现在使用函数ggplotGrob()将两个图都转换为 grobs。如果我们查看这些 grobs 的结构,您会看到可见的 y 轴是 grob 14 和 17(其他是 0 grobs),x 轴是 grobs 23 到 25。

g1<-ggplotGrob(p1)
g2<-ggplotGrob(p2)
g2
TableGrob (12 x 12) "layout": 28 grobs
    z         cells       name                                     grob
1   0 ( 1-12, 1-12) background          rect[plot.background.rect.3481]
2   1 ( 4- 4, 4- 4)    panel-1                gTree[panel-1.gTree.3356]
3   2 ( 4- 4, 7- 7)    panel-2                gTree[panel-2.gTree.3366]
4   3 ( 4- 4,10-10)    panel-3                gTree[panel-3.gTree.3376]
5   4 ( 8- 8, 4- 4)    panel-4                gTree[panel-4.gTree.3386]
6   5 ( 8- 8, 7- 7)    panel-5                gTree[panel-5.gTree.3396]
7   6 ( 8- 8,10-10)    panel-6                gTree[panel-6.gTree.3406]
8   7 ( 3- 3, 4- 4)  strip_t-1    absoluteGrob[strip.absoluteGrob.3448]
9   8 ( 3- 3, 7- 7)  strip_t-2    absoluteGrob[strip.absoluteGrob.3453]
10  9 ( 3- 3,10-10)  strip_t-3    absoluteGrob[strip.absoluteGrob.3458]
11 10 ( 7- 7, 4- 4)  strip_t-4    absoluteGrob[strip.absoluteGrob.3463]
12 11 ( 7- 7, 7- 7)  strip_t-5    absoluteGrob[strip.absoluteGrob.3468]
13 12 ( 7- 7,10-10)  strip_t-6    absoluteGrob[strip.absoluteGrob.3473]
14 13 ( 4- 4, 3- 3)   axis_l-1 absoluteGrob[axis-l-1.absoluteGrob.3433]
15 14 ( 4- 4, 6- 6)   axis_l-2         zeroGrob[axis-l-2.zeroGrob.3434]
16 15 ( 4- 4, 9- 9)   axis_l-3         zeroGrob[axis-l-3.zeroGrob.3435]
17 16 ( 8- 8, 3- 3)   axis_l-4 absoluteGrob[axis-l-4.absoluteGrob.3441]
18 17 ( 8- 8, 6- 6)   axis_l-5         zeroGrob[axis-l-5.zeroGrob.3442]
19 18 ( 8- 8, 9- 9)   axis_l-6         zeroGrob[axis-l-6.zeroGrob.3443]
20 19 ( 5- 5, 4- 4)   axis_b-1         zeroGrob[axis-b-1.zeroGrob.3407]
21 20 ( 5- 5, 7- 7)   axis_b-2         zeroGrob[axis-b-2.zeroGrob.3408]
22 21 ( 5- 5,10-10)   axis_b-3         zeroGrob[axis-b-3.zeroGrob.3409]
23 22 ( 9- 9, 4- 4)   axis_b-4 absoluteGrob[axis-b-4.absoluteGrob.3415]
24 23 ( 9- 9, 7- 7)   axis_b-5 absoluteGrob[axis-b-5.absoluteGrob.3421]
25 24 ( 9- 9,10-10)   axis_b-6 absoluteGrob[axis-b-6.absoluteGrob.3427]
26 25 (11-11, 4-10)       xlab             text[axis.title.x.text.3475]
27 26 ( 4- 8, 2- 2)       ylab             text[axis.title.y.text.3477]
28 27 ( 2- 2, 4-10)      title               text[plot.title.text.3479]

因此,使用情节 2 的相应 grobs 替换情节 1 中的零 grobs,您将获得轴线。

g1[[1]][[15]]<-g2[[1]][[14]]
g1[[1]][[16]]<-g2[[1]][[14]]
g1[[1]][[18]]<-g2[[1]][[14]]
g1[[1]][[19]]<-g2[[1]][[14]]
g1[[1]][[20]]<-g2[[1]][[23]]
g1[[1]][[21]]<-g2[[1]][[23]]
g1[[1]][[22]]<-g2[[1]][[23]]

grid.draw(g1)

在此处输入图像描述

于 2014-03-01T16:38:31.233 回答