Y轴最小值在双轴图表上

huangapple go评论79阅读模式
英文:

Y min value on Dual Axis Chart

问题

我正在尝试设置双轴图表的最小y值。我理解在绘图时使用双轴并不完全符合最佳实践,但这是一个被请求的项目。我想尝试将最小值设置为大于零的任何值。

在线查找时,除了调整或使用“expansion”之外,我没有找到答案。这个方法的问题是,由于它缩小,不在该区域内的任何内容都会被裁剪。该图是使用tenubrand在这里提供的答案制作的。
https://stackoverflow.com/questions/3099219/ggplot-with-2-y-axes-on-each-side-and-different-scales

我认为他在帖子中使用的y最大值可以更改为最小值,但这并没有起作用。所以我包括了“expansion”,但它不是动态的,并且对于不同的数据集效果不佳。有些图甚至在某些点被截断。

library(scales)
library(ggplot2)
# 辅助轴变换的函数工厂
train_sec <- function(primary, secondary, na.rm = TRUE) {
  from <- range(secondary, na.rm = na.rm)
  to   <- range(primary, na.rm = na.rm)
  forward <- function(x) {
    rescale(x, from = from, to = to)
  }
  reverse <- function(x) {
    rescale(x, from = to, to = from)
  }
  list(fwd = forward, rev = reverse)
}

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

ggplot(economics, aes(date)) +
  geom_col(aes(y = unemploy), colour = "blue") +
  geom_line(aes(y = sec$fwd(psavert)), colour = "red") +
  scale_y_continuous(name = NULL, 
                     expand = expansion(mult = c(-.2, .1)),
                     sec.axis = sec_axis(~sec$rev(.), name = "psavert"))
英文:

I'm trying to set the minimum y value for a dual axis chart. I understand having dual axis is not entirely best practices when plotting, but this is a requested item. I'd like to try and have the minimum set to anything above zero.

Looking online I have not found an answer other than adjusting or using expansion. The issue with this is, since it contracts, anything not in the area gets cut off. The plot was made using tenubrand's answer here.
https://stackoverflow.com/questions/3099219/ggplot-with-2-y-axes-on-each-side-and-different-scales

I thought his use of y max values in his post could be changed to min but it didn't work. So I included expansion, but it's not dynamic and doesn't work well for varying data sets. Some graphs even cut of at some points.

library(scales)
library(ggplot2)
# Function factory for secondary axis transforms
train_sec &lt;- function(primary, secondary, na.rm = TRUE) {
  from &lt;- range(secondary, na.rm = na.rm)
  to   &lt;- range(primary, na.rm = na.rm)
  forward &lt;- function(x) {
    rescale(x, from = from, to = to)
  }
  reverse &lt;- function(x) {
    rescale(x, from = to, to = from)
  }
  list(fwd = forward, rev = reverse)
}


sec &lt;- with(economics, train_sec(unemploy, psavert))


ggplot(economics, aes(date)) +
  geom_col(aes(y = unemploy), colour = &quot;blue&quot;) +
  geom_line(aes(y = sec$fwd(psavert)), colour = &quot;red&quot;) +
  scale_y_continuous(name = NULL, 
                     expand = expansion(mult = c(-.2, .1)),
                     sec.axis = sec_axis(~sec$rev(.), name = &quot;psavert&quot;))

答案1

得分: 1

以下是您要翻译的内容:

一个简单的选项,适用于某些(许多?)情况,是将两个范围的缩放最小值都硬编码为零,以便这两个系列共享垂直原点。

train_sec <- function(primary, secondary, na.rm = TRUE) {
  from <- range(secondary, na.rm = na.rm)
  from[1] = 0
  to   <- range(primary, na.rm = na.rm)
  to[1] = 0
  forward <- function(x) {
    rescale(x, from = from, to = to) 
  }
  reverse <- function(x) {
    rescale(x, from = to, to = from)
  }
  list(fwd = forward, rev = reverse)
}

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

Y轴最小值在双轴图表上

英文:

One simple option that works for some (many?) situations would be to hardcode the scaling min for both ranges as zero, so that the two series share a vertical origin.

train_sec &lt;- function(primary, secondary, na.rm = TRUE) {
  from &lt;- range(secondary, na.rm = na.rm)
  from[1] = 0
  to   &lt;- range(primary, na.rm = na.rm)
  to[1] = 0
  forward &lt;- function(x) {
    rescale(x, from = from, to = to) 
  }
  reverse &lt;- function(x) {
    rescale(x, from = to, to = from)
  }
  list(fwd = forward, rev = reverse)
}

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

Y轴最小值在双轴图表上

huangapple
  • 本文由 发表于 2023年8月4日 05:45:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831773.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定