1

The dput(Q_Sheet) is below. How can properly introduce a second y-axis that is different in scale from the primary axis?

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
tjebo
  • 21,977
  • 7
  • 58
  • 94
Toy L
  • 55
  • 6
  • There is a typo. it should be `scale_y_continuous` – akrun Jul 22 '21 at 02:13
  • There is an update. Please see the output. – Toy L Jul 22 '21 at 02:22
  • Now, there is a missing `+` – akrun Jul 22 '21 at 02:22
  • Does this answer your question? [Using secondary y-axis in ggplot2 with different scale factor when using facet\_wrap](https://stackoverflow.com/questions/61565304/using-secondary-y-axis-in-ggplot2-with-different-scale-factor-when-using-facet-w) – tjebo Jul 22 '21 at 10:33
  • actually I wanted to point to this question here https://stackoverflow.com/questions/3099219/ggplot-with-2-y-axes-on-each-side-and-different-scales – tjebo Jul 22 '21 at 10:33

3 Answers3

1

There are two issues - 1) scale_y_continuous typo and 2) there is a missing + connecting the last expression

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

enter image description here

akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thank you for the correction and help! While it is not advised to have two y-axis, is there a better way to find the correct factor for my secondary y-x scale? – Toy L Jul 22 '21 at 02:32
1

You can use the below formula to keep the secondary Y-axis at the same level as 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"))

enter image description here

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
0

We could define a coefficient and then color the lines to indicate wich lines belongs to which y-scale:

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)
  ) 

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66
  • TarJae, how did you know the coef would be 0.010? Is there a formula you used? – Toy L Jul 22 '21 at 22:52
  • Coeff is arbitrary and up to you which factor you take to transform your data. While you could use the formula used by Ronak or akrun. In my opinion in both cases the corrected_aa y axis does not show the values in detail, which in essence is the job of the second y axis. – TarJae Jul 23 '21 at 05:25