【问题标题】:Determining the Scale factor for the secondary Y-axis确定辅助 Y 轴的比例因子
【发布时间】:2021-07-22 02:07:50
【问题描述】:

dput(Q_Sheet) 在下方。如何正确引入与主轴比例不同的第二个 y 轴?

structure(list(Amino_acids = c(4, 12, 20, 28, 32), Protein_length_Ang = c(7, 
24, 40, 56, 64), length_no_ratio = c(1.75, 2, 2, 2, 2), Corrected_aa = c(1.24459201924769e-12, 
3.71007650662474e-09, 1.10594599229843e-05, 0.0319159404863842, 
0.642857142857143), aa_frequency = c(3.99735380592756, 6.96840672963299, 
4.58228895300999, 3.12310921028256, 4.67560843680985), T_degC = c(50.3857804818545, 
52.8464583426248, 60.0760389538482, 58.1895053328481, 67.628202708438
)), row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame"
), na.action = structure(c(`2` = 2L, `4` = 4L, `6` = 6L), class = "omit"))
`
ggplot(data = Q_Sheet, aes(x = T_degC))+
       geom_line(aes(y = Amino_acids), color="red")+
       geom_line(aes(y = Corrected_aa), color = "blue") + 
scale_y_continuous(name = "Amino_acids", sec.axis = sec_axis(~.*10, name = "Corrected_aa"))    

输出如下:

     <ScaleContinuousPosition>
 Range:  
 Limits:    0 --    1

【问题讨论】:

标签: r ggplot2


【解决方案1】:

您可以使用以下公式将辅助 Y 轴保持在与Corrected_aa 相同的水平。

library(ggplot2)

ggplot(data=Q_Sheet, aes(x=T_degC))+
  geom_line(aes(y=Amino_acids),color="red")+
  geom_line(aes(y=Corrected_aa),color="blue")+
  scale_y_continuous(name="Amino_acids",
                     sec.axis=sec_axis(~{
                       a <- min(Q_Sheet$Corrected_aa)
                       b <- max(Q_Sheet$Corrected_aa)
                       ((((b-a) * (. - min(.)))/diff(range(.))) + a)
                      },name="Corrected_aa"))

【讨论】:

  • 在这里感谢您的帮助!
【解决方案2】:

有两个问题 - 1) scale_y_continuous 拼写错误和 2) 缺少 + 连接最后一个表达式

ggplot(data=Q_Sheet, aes(x=T_degC))+
             geom_line(aes(y=Amino_acids),color="red")+
             geom_line(aes(y=Corrected_aa),color="blue") +
             scale_y_continuous(name="Amino_acids",
        sec.axis=sec_axis(~.*10,name="Corrected_aa"))

-输出

【讨论】:

  • 感谢指正和帮助!虽然不建议有两个 y 轴,但有没有更好的方法来为我的次要 y-x 比例找到正确的因子?
【解决方案3】:

我们可以定义一个系数,然后为线条着色以指示哪些线条属于哪个 y 尺度:

library(ggplot2)

value used to transform the data
coeff <- 0.01

# colors
Amino_acidsColor = "red"
Corrected_aaColor = "blue"

ggplot(data=Q_Sheet, aes(x=T_degC))+
  geom_line(aes(y=Amino_acids), size = 2, color=Amino_acidsColor)+
  geom_line(aes(y=Corrected_aa/coeff), size = 2, color=Corrected_aaColor) +
  scale_y_continuous(name="Amino_acids",
                     sec.axis=sec_axis(~.*coeff,name="Corrected_aa")) +
  theme_bw() +
  theme(
    axis.title.y = element_text(color = Amino_acidsColor, size=13),
    axis.title.y.right = element_text(color = Corrected_aaColor, size=13)
  ) 

【讨论】:

  • TarJae,你怎么知道系数是 0.010?有没有你用过的公式?
  • Coeff 是任意的,取决于您采用哪种因素来转换数据。虽然您可以使用 Ronak 或 akrun 使用的公式。在我看来,在这两种情况下,corrected_aa y 轴都没有详细显示值,这本质上是第二个 y 轴的工作。
猜你喜欢
  • 1970-01-01
  • 2020-08-17
  • 1970-01-01
  • 1970-01-01
  • 2021-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多