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
答案 0 :(得分:1)
有两个问题 - 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"))
-输出
答案 1 :(得分: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 :(得分:0)
我们可以定义一个系数,然后为线条着色以指示哪条线条属于哪个 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)
)