r - ggplot2:如何为facet_wrap中的每个方面单独设置轴中断?

标签 r ggplot2 facet-wrap

我想修改facet_wrap中每个图表的y轴的断点和限制。例如,我希望减少其中一些的休息时间,或者我希望它们从 0 开始。

ggplot(granuControlLw, aes(distance, group=time)) 
+ geom_line(aes(y = D))  + xlab("Distance from the gate (m)")
+ geom_point(aes(y = D, shape = factor(time)), size=4.5) + theme_bw()  
+ theme(axis.text.x = element_text(size = 15, angle = 90), axis.text.y = element_text(size = 15, angle = 0)) 
+ scale_x_continuous(breaks=seq(-50,1100,50))  
+ theme(axis.text = element_text(size = 15),axis.title = element_text(size = 15),legend.title = element_text(size = 15, face = 'bold'),legend.text= element_text(size=15), axis.line = element_line(colour = "gray"))
 + theme(strip.background = element_blank())
 + facet_wrap (evolution~ section, scale="free_y", ncol=1) 

Here is a few raws of my input file:
evolution   time    distance    section D
D5     After    680     (> S450)    0.8286370543
D5     After    710     (> S450)    1.0857412286
D5     After    950     (> S450)    0.29524528
D5     After    1010    (> S450)    0.7115190438
D16    After    680     (> S450)    2.7797109467
D16    After    710     (> S450)    4.2948672219
D16    After    950     (> S450)    0.5445345574
D16    After    1010    (> S450)    2.9139811532
D25    After    680     (> S450)    5.3764331372
D25    After    710     (> S450)    6.6094309926
D25    After    950     (> S450)    0.789626722
D25    After    1010    (> S450)    6.25184791
D50    After    680     (> S450)    13.0637943297
D50    After    710     (> S450)    17.155345894
D50    After    950     (> S450)    3.2134971025
D50    After    1010    (> S450)    18.9873626321
D75    After    680     (> S450)    19.491433335
D75    After    710     (> S450)    26.1926456265
D75    After    950     (> S450)    12.4823051787
D75    After    1010    (> S450)    45.0209667314

最佳答案

我不知道如何修改分面图中各个面的轴中断和范围。然而,另一种选择是为分面变量的每个级别创建单独的图,然后将所有图布置在一起。单独创建每个图可以让您更好地控制每个图的轴中断和范围。

以下是内置 mtcars 数据框的示例:

library(scales)     # For pretty_breaks
library(grid)       # For textGrob
library(gridExtra)  # For grid.arrange
library(cowplot)    # For plot_grid

下面的代码创建了一个绘图列表,每个 cyl 级别对应一个绘图列表。请注意使用 scale_y_continuous 来设置每个图的 y 范围。这只是一个例子。如果您愿意,您可以对每个图的轴范围和中断进行更精细的控制。

pl = lapply(sort(unique(mtcars$cyl)), function(i) {

  p = ggplot(mtcars[mtcars$cyl==i, ], aes(wt, mpg)) + 
    facet_wrap(~cyl) +
    geom_point() + 
    labs(x="Weight", y="") +
    scale_y_continuous(limits=c(ifelse(i==4, 10, 0), 1.1 * max(mtcars$mpg[mtcars$cyl==i])),
                       breaks=pretty_breaks(ifelse(i==6, 6, 3))) +
    scale_x_continuous(limits=range(mtcars$wt)) +
    theme(plot.margin=unit(c(0, 0.1, 0, -1),"lines"))

  # Remove x-axis labels and title except for last plot
  if(i < max(mtcars$cyl)) p = p + theme(axis.text.x=element_blank(),
                                        axis.title.x=element_blank())

  return(p)
})

现在,将图布置在一列中。我们还添加了 y 轴标签。

grid.arrange(textGrob("MPG", rot=90), plot_grid(plotlist=pl, ncol=1, align="h"), 
             widths=c(0.03,0.97), ncol=2)

enter image description here

关于r - ggplot2:如何为facet_wrap中的每个方面单独设置轴中断?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36432292/

相关文章:

R tidyr 正则表达式 : extract ordered numbers from character column

R: "InvalidArgument ` -delay' 带动画和 ggplot

r - 在箱线图上使用 geom_jitter 重叠时标记数据点

ggplot2 - 使用facet_wrap_paginate分离出facet_wrap

r - 朴素贝叶斯的特征选择

jquery - R Shiny 的鼠标悬停到所有表格单元格

r - 使用cowplot的plot_grid放置图例

r - ggplot2 中的等高线

r - 提高 R 和 ggplot2 中 EMF 输出的质量

r - ggplot : geom_text does not center-align above geom_col()