繁体   English   中英

在ggplot2中混合facet_grid()和facet_wrap()

[英]Mixing facet_grid() and facet_wrap() in ggplot2

我的问题有点奇怪。 我有四种方法(M1,M2,M3和M4),每种方法都在两个独立的阶段(T1和T2)和10种不同的尺寸(1,2,......,10)进行了实验。 下表显示了数据的结构:

phase size  mean      stdev   method
T1    1     0.005     0.001    M1
T1    2     0.009     0.004    M1
T1    3     0.011     0.004    M1
...
T2    9     269.020   12.003   M4
T2    10    297.024   10.004   M4

第一阶段结果(T1)的范围小于第二阶段的范围。 以下命令生成第一个图

p <- ggplot(ds, aes(x=size, y=mean, color=method)) 
      + geom_line() 
      + geom_smooth(aes(ymin=mean-stdev, ymax=mean+stdev), stat="identity") 
      + scale_x_discrete(breaks=1:10) 
      + labs(x="Size", y="Time in Seconds", fill="Method") 
      + theme(panel.margin=unit(0.5, "lines"), legend.position="none") 
      + facet_grid(method~phase, scales="free_y")

结果: facet_grid()的使用
虽然下面的代码给出了第二个数字

p <- ggplot(ds, aes(x=size, y=mean, color=method))
      + geom_line() 
      + geom_smooth(aes(ymin=mean-stdev, ymax=mean+stdev), stat="identity") 
      + scale_x_discrete(breaks=1:10) 
      + labs(x="Size", y="Time in Seconds", fill="Method") 
      + theme(panel.margin=unit(0.5, "lines"), legend.position="none") 
      + facet_wrap(method~phase, ncol=2, scales="free_y")

facet_wrap()的使用
现在,问题是“如何使用facet_grid()并强制y轴自由,类似于使用facet_wrap() ?” 或“如何改变的标签facet_wrap()是类似facet_grid() ?”
先感谢您

编辑:对其他人的警告,OP正在使用但未包含的数据在低值时具有足够的动态范围,这个答案将无效。

我不认为你可以用facet_grid做你想要的,但如果你愿意交换M和T,你可以在你的特定实现中非常接近:

ggplot(ds, aes(x=size, y=mean, color=method)) + geom_line() + 
  #geom_smooth(aes(ymin=mean-stdev, ymax=mean+stdev), stat="identity") + 
  scale_x_discrete(breaks=1:10) + 
  labs(x="Size", y="Time in Seconds", fill="Method") +
  theme(panel.margin=unit(0.5, "lines"), legend.position="none") + 
  facet_grid(phase~method, scales="free_y")

在此输入图像描述

facet_grid ,Y刻度在行之间是自由的,但不是列,因此通过重新定向刻面,我们可以将高幅度和低幅度值分离到行上,以便行方式释放具有所需的效果。

暂无
暂无

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

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