How to add a second Y-axis using the matplot function?

3

I have two data series with different scales and would like to plot the two on a single graph, the second is the derivative of the first. I'm using the code below but it does not work the way I want because it plots the data with only one Y-axis. I wanted to add a second Y-axis, preferably on the right side of the chart. What is the solution?

matplot(dataTGA$V1,
    cbind(dataTGA$V3, dataDTGA$V3), 
    xlab = "Temperatura (K)",
    ylab = "Perda de massa (%)",
    type= "l")
    
asked by anonymous 23.10.2015 / 22:12

2 answers

3

You can use the axis function to add an extra axis to the graph and if necessary mtext to place your label. For example, for a simple chart:

par(mar = c(5, 4, 4, 4) + 0.1)
plot(1:10, ylab = "Eixo Y Esquerdo")
lines(10:1)
axis(4, at = 1:10, labels = seq(10, 100, 10))
mtext("Eixo Y Direito", side = 4, line = 2)

Notethatyouhavetoadjusttherightmarginwithparandthatyoucanputdifferentvaluesoftheactualpositionontheaxisusingthelabelsargument.

Ifthedataisofdifferentscalesandyouprefernottotransformthedataofthesecondaxis,youcanusethefollowingform:

par(mar=c(5,4,2,4)+0.1)plot(1:10,ylab="Eixo Y Esquerdo")
par(new = TRUE, mar = c(5, 4, 2, 4) + 0.1)
plot(100:1, type = "l", axes = FALSE, xlab = "", ylab = "")
axis(4, at = seq(0, 100, 10), labels = seq(0, 100, 10))
mtext("Eixo Y Direito", side = 4, line = 2)

Inthiscase,theargumentnew=TRUEofparcausesthegraphicareanottobe"clean" when you call the plot function again. Be aware that from this moment the axes will be relative to the values of x and y of the second graph.

Several examples and alternatives (with functions of other packages) are available in similar SOen question .

    
24.10.2015 / 00:50
-1

What if I use barplot or boxplot for both variables? Which argument should I use so that the plots do not overlap?

    
02.10.2017 / 17:21