【问题标题】:Dual y-axis while using facet_wrap in ggplot with varying y-axis scale在具有不同 y 轴刻度的 ggplot 中使用 facet_wrap 时的双 y 轴
【发布时间】:2021-02-04 20:14:37
【问题描述】:

我正在尝试在使用 facet_wrap 时在 ggplot2 中绘制几个面板。我想要两个 y 轴。左侧 y 轴范围从 5 到 27,右侧 y 轴范围从 25 到 27。我想用回归线显示所有数据点。但是,当我绘图时,右侧 y 轴的变化与其他数据集相比较低,因此显示为一条平线。我想将右侧 y 轴保持在 25 到 27 之间,以便可以清楚地看到数据的变化。我使用了此代码1,但无法对其进行排序。非常感谢任何帮助。

library(ggplot2)

scaleFactor <- max(d1$weeks) / max(d1$income)

ggplot(mtcars, aes(x=Year)) +
  geom_smooth(aes(y=weeks), method="loess", col="blue") +
  geom_smooth(aes(y=income * scaleFactor), method="loess", col="red") +
  scale_y_continuous(name="weeks", sec.axis=sec_axis(~./scaleFactor, name="income")) +
  theme(
    axis.title.y.left=element_text(color="blue"),
    axis.text.y.left=element_text(color="blue"),
    axis.title.y.right=element_text(color="red"),
    axis.text.y.right=element_text(color="red")
  )

【问题讨论】:

  • 对不起,我没有看到您在代码生成的图中提到的问题。特别是,您在不同的面板中提到了一个问题,但您没有在代码中包含facet_wrap()。您能否修改代码以更明显地显示问题?
  • @teunbrand 上面的代码工作正常。但是,我想将一个 y 轴限制在 25 到 27 之间,另一个限制在 5 到 30 之间。因为两个变量的最大值相似,所以转换没有帮助。基本上,我想将 25-27 的比例扩展到 5-30。 +facet_warap(~group) 也可以正常工作。
  • 我知道代码有效,它运行没有错误。我看到的是:蓝色位的范围是 4-8,红色位的范围是 52-335。这与您提到的 25-27 和 5-30 范围有什么关系?另外,这个组在 mtcars 数据中来自哪里?
  • @teunbrand 对所有的混乱感到非常抱歉。我使用此代码对我的数据集运行分析。我有一个非常大的数据集,现在我添加了一个示例来显示数据的结构。
  • 这能回答你的问题吗? How to use facets with a dual y-axis ggplot

标签: r ggplot2 figure facet-wrap yaxis


【解决方案1】:

如果这是关于使数据范围重叠而不是仅仅重新调整最大值,您可以尝试以下方法。

首先我们将创建函数工厂以简化我们的工作:

library(ggplot2)
library(scales)
#> Warning: package 'scales' was built under R version 4.0.3

# Function factory for secondary axis transforms
train_sec <- function(from, to) {
  from <- range(from)
  to   <- range(to)
  # Forward transform for the data
  forward <- function(x) {
    rescale(x, from = from, to = to)
  }
  # Reverse transform for the secondary axis
  reverse <- function(x) {
    rescale(x, from = to, to = from)
  }
  list(fwd = forward, rev = reverse)
}

然后,我们可以使用函数工厂对数据和次轴进行转换函数。

# Learn the `from` and `to` parameters
sec <- train_sec(mtcars$hp, mtcars$cyl)

你可以这样申请:

ggplot(mtcars, aes(x=disp)) +
  geom_smooth(aes(y=cyl), method="loess", col="blue") +
  geom_smooth(aes(y= sec$fwd(hp)), method="loess", col="red") +
  scale_y_continuous(name="cyl", sec.axis=sec_axis(~sec$rev(.), name="hp")) +
  theme(
    axis.title.y.left=element_text(color="blue"),
    axis.text.y.left=element_text(color="blue"),
    axis.title.y.right=element_text(color="red"),
    axis.text.y.right=element_text(color="red")
  )
#> `geom_smooth()` using formula 'y ~ x'
#> `geom_smooth()` using formula 'y ~ x'

这是一个使用不同数据集的示例。

sec <- train_sec(economics$psavert, economics$unemploy)

ggplot(economics, aes(date)) +
  geom_line(aes(y = unemploy), colour = "blue") +
  geom_line(aes(y = sec$fwd(psavert)), colour = "red") +
  scale_y_continuous(sec.axis = sec_axis(~sec$rev(.), name = "psavert"))

reprex package (v1.0.0) 于 2021-02-04 创建

【讨论】:

  • 很棒的工作。终于找到了一个我可以永远使用的解决方案!
猜你喜欢
  • 2019-01-05
  • 2013-01-22
  • 1970-01-01
  • 1970-01-01
  • 2017-10-12
  • 2020-04-05
  • 1970-01-01
  • 2016-12-12
  • 1970-01-01
相关资源
最近更新 更多