如何使用ggplot2更改每个方面的轴的限制和划分?处理 "error in x$clone() attempt to apply non-function"

How to change the limits and divisions of the axes of each facet using ggplot2? Dealing with "error in x$clone() attempt to apply non-function"

我正在创建一个分为多个方面的图表。其中一些面需要缩小比例才能更好地查看。我已经应用了几个技巧来实现这一点。但是,我需要更改某些方面的轴的限制和划分。

调用必要的包,创建数据库并制作绘图

devtools::install_github("zeehio/facetscales")
library(ggplot2)
library(facetscales)

#Constructing data frame
Source <- c(rep("Water", 12), rep("Oil", 12))
Range <- rep((c(rep("First", 4), rep("Second", 8))),2)
Xaxis <- c(0,1,2,5,0,1,2,5,10,20,30,40,
           0,1,2,5,0,1,2,5,10,20,30,40)
Yaxis <- c(0,1,2,5,0,1,2,5,10,20,30,40,
           0,1,2,5,0,1,2,5,10,20,30,40)

DF <- data.frame(Source, Range, Xaxis, Yaxis)
      
#plot
p <- ggplot(data = DF, aes(x = Xaxis, y = Yaxis)) +
  geom_smooth(method = "lm",
              formula = y ~ x) +
  geom_point()

我最初将 facet_wrapscales = "free" 一起使用,但我真正想要的是能够修改 "First" 中刻面的 x 轴的限制和划分行。

#plot using facet wrap
p + facet_wrap(Range~Source,
               scales = "free") +
  ggtitle("Using facet_wrap")

所以我选择使用 facetscales 包并遵循 and this (2) 个示例

#axis specifications
scale_x <- list(
  "First" = scale_x_continuous(limits = c(0, 10), breaks = c(0, 2, 10)),
  "Second"= scale_x_continuous(limits = c(0, 40), breaks = c(0, 5, 40))
)

#plot using facet grid and the facetscales package
p + facet_grid_sc(rows = vars(Range),
                  cols = vars (Source),
                  scales = list(x = scale_x)) +
  ggtitle("Using facet_grid_sc")

当尝试制作具有我需要的规格的图表时出现:

error in x$clone() attempt to apply non-function

我不明白我做错了什么。有人可以帮帮我吗?

我想我找到了它不起作用的原因,

facet_grid 中,scales 参数用于指示每个面板是否具有与 c("free","free_x","free_y") 相似或不同的比例。但是在facet_grid上有free_x只在水平方向上是自由的,所有的垂直分组都有相同的刻度范围。

当指定 facet_grid_sc 时,scales 参数用于区分每个比例将如何呈现,特别是 scales = list(x = scale_x) 其中 scale_x 匹配 cols = vars(Range)

library(ggplot2)
library(facetscales)

#Constructing data frame
Source <- c(rep("Water", 12), rep("Oil", 12))
Range <- rep((c(rep("First", 4), rep("Second", 8))),2)
Xaxis <- c(0,1,2,5,0,1,2,5,10,20,30,40,
           0,1,2,5,0,1,2,5,10,20,30,40)
Yaxis <- c(0,1,2,5,0,1,2,5,10,20,30,40,
           0,1,2,5,0,1,2,5,10,20,30,40)

DF <- data.frame(Source, Range, Xaxis, Yaxis)

p <- ggplot(data = DF, aes(x = Xaxis, y = Yaxis)) +
  geom_smooth(method = "lm",
              formula = y ~ x) +
  geom_point()

#old plot without facet_grid_sc
p + facet_grid(rows = vars(Range),
               cols = vars (Source))

您看到如果按 Range 对行进行分面,它们是垂直分割的而不是水平分割的吗?我认为 facet_grid_sc 不支持 free_x 同时还指定了 scales

的 x 元素
scale_x <- list(
  First = scale_x_continuous(limits = c(0, 10), breaks = c(0, 2, 10)),
  Second= scale_x_continuous(limits = c(0, 40), breaks = c(0, 5, 40))
)

#new plot where scale_x matches cols argument
p + facet_grid_sc(cols = vars(Range),
                  rows = vars (Source),
                  scales = list(x = scale_x))

reprex package (v1.0.0)

于 2021 年 3 月 16 日创建

编辑

需要注意的是,如果您想单独控制每个面板的范围或标签 cols/rows,facet_grid_sc 可能更有用。在这个例子中,在 facet_grid 中使用 scales = "free_x" 实际上已经足够了。


p + facet_grid(cols = vars(Range),
               rows = vars (Source),
               scales = "free_x")

reprex package (v1.0.0)

于 2021 年 3 月 16 日创建