简体   繁体   English

为 facet_grid 中的每个图显示 x 轴和 y 轴

[英]Show x-axis and y-axis for every plot in facet_grid

As similar question has been asked before ( here ) but I can't adjust the solution provided there to my specific problem.由于之前( 此处)曾问过类似的问题,但我无法根据我的具体问题调整那里提供的解决方案。 For every plot shown below, there should be ax as well as y-axis.对于下面显示的每个图,应该有 ax 和 y 轴。

The data:数据:

dput(df_nSubj)
structure(list(nSubj = c(10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 
20L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 
30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 40L, 
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 50L, 50L, 50L, 
50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L), family = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 
3L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 
3L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 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 0.8, 
    0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 
    0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 
    0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 
    0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 
    0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 
    0.2, 0.5, 0.8), pow = c(0.12, 0.53, 0.84, 0.1, 0.4, 0.74, 
    0.13, 0.55, 0.84, 0.11, 0.44, 0.74, 0.12, 0.5, 0.82, 0.1, 
    0.43, 0.76, 0.12, 0.49, 0.81, 0.1, 0.43, 0.76, 0.2, 0.84, 
    0.99, 0.15, 0.72, 0.97, 0.21, 0.81, 0.98, 0.17, 0.69, 0.95, 
    0.2, 0.83, 0.99, 0.17, 0.75, 0.97, 0.19, 0.82, 0.99, 0.17, 
    0.75, 0.97, 0.32, 0.95, 1, 0.23, 0.87, 1, 0.32, 0.92, 1, 
    0.25, 0.83, 0.99, 0.3, 0.94, 1, 0.24, 0.89, 1, 0.3, 0.94, 
    1, 0.24, 0.89, 1, 0.41, 0.99, 1, 0.29, 0.96, 1, 0.4, 0.97, 
    1, 0.3, 0.92, 1, 0.38, 0.99, 1, 0.32, 0.97, 1, 0.37, 0.98, 
    1, 0.32, 0.97, 1, 0.5, 1, 1, 0.36, 0.98, 1, 0.47, 0.99, 1, 
    0.36, 0.96, 1, 0.47, 1, 1, 0.4, 0.99, 1, 0.46, 1, 1, 0.4, 
    0.99, 1)), class = "data.frame", .Names = c("nSubj", "family", 
"Spher", "effSize", "pow"), row.names = c(NA, -120L))

plot:阴谋:

require(ggplot2)
require(grid)
    pl1 <- ggplot(data=df_nSubj,aes(x=nSubj,y=pow,group=family))+
      geom_point(aes(shape=family))+geom_line()+
      labs(x="Number of subjects",y="Power",shape="")+
      scale_y_continuous(limits=c(0.2,1),breaks=c(0.2,0.4,0.6,0.8,1))+
      guides(shape = guide_legend(ncol = 4))+
      facet_grid(Spher~effSize)+
      theme_bw()+
      theme(legend.position = "top",
            panel.margin = unit(2, "lines"),
            legend.key = element_blank(),
            strip.text.x = element_blank(),
            strip.text.y = element_blank(),
            strip.background = element_blank(),
            panel.border=element_blank(),
            axis.line=element_line(),
            axis.title.x = element_text(vjust=-0.5))

这是它的样子

Thanks in advance提前致谢

Change facet_grid(Spher~effSize) to facet_wrap(Spher~effSize, scales = "free")facet_grid(Spher~effSize)更改为facet_wrap(Spher~effSize, scales = "free")

在此处输入图片说明

I'm late to this party, but I have two workarounds currently.我参加这个聚会迟到了,但我目前有两种解决方法。

The first option is to add horizontal and/or vertical line geoms with intercepts set to -Inf, which requires turning off plot clipping.第一个选项是添加水平和/或垂直线几何,截距设置为 -Inf,这需要关闭绘图剪辑。 To the original plot, add the following lines:在原始图中,添加以下几行:

geom_hline(aes(yintercept=-Inf)) + 
geom_vline(aes(xintercept=-Inf)) + 
coord_cartesian(clip="off")

Fake Axis Option假轴选项

Alternatively, use the lemon package and its facet command.或者,使用柠檬包及其 facet 命令。 This will reproduce the axes and their ticks without adding the labels.这将在不添加标签的情况下重现轴及其刻度。

library(lemon)

And change facet_grid(Spher~effSize) to facet_rep_grid(Spher~effSize)并将facet_grid(Spher~effSize)更改为facet_rep_grid(Spher~effSize)

Lemon Package Option柠檬套餐选项

The ggh4x package also has this functionality via its facet_wrap2 functionggh4x也通过其facet_wrap2函数具有此功能

ggh4x facet_wrap2 函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM