简体   繁体   English

确定辅助 Y 轴的比例因子

[英]Determining the Scale factor for the secondary Y-axis

The dput(Q_Sheet) is below. dput(Q_Sheet)如下。 How can properly introduce a second y-axis that is different in scale from the primary axis?如何正确引入与主轴比例不同的第二个 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"))    

The output is as follows:输出如下:

     <ScaleContinuousPosition>
 Range:  
 Limits:    0 --    1

There are two issues - 1) scale_y_continuous typo and 2) there is a missing + connecting the last expression有两个问题 - 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"))

-ouptut -输出

在此处输入图片说明

You can use the below formula to keep the secondary Y-axis at the same level as Corrected_aa .您可以使用以下公式将辅助 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"))

在此处输入图片说明

We could define a coefficient and then color the lines to indicate wich lines belongs to which y-scale:我们可以定义一个系数,然后为线条着色以指示哪些线条属于哪个 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)
  ) 

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM