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

Asked

Viewed 702 times

3

I have two sets of data with different scales and would like to plot the two on the same graph, the second is derived from the first. I’m using the code below, but it doesn’t 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 graph. What is the solution?

matplot(dataTGA$V1,
    cbind(dataTGA$V3, dataDTGA$V3), 
    xlab = "Temperatura (K)",
    ylab = "Perda de massa (%)",
    type= "l")

2 answers

4


You can use the function axis to add an extra axis to the chart and if necessary mtext to put 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)

inserir a descrição da imagem aqui

Note that you have to adjust the right margin with par and that you can put different values of the real position on the axis using the argument labels.

If the data is from different scales and you prefer not to transform the data from the second axis, you can use the following form:

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)

inserir a descrição da imagem aqui

In this case the argument new = TRUE of par makes the graphic area not "clean" when you call the function plot again. Be aware that from now on the axes will be relative to the values of x and y of the second graph.

Several examples and alternatives (with other package functions) are available at similar question by Soen.

  • the problem is that the second series has a different scale than the first, so I would have to "transform" the data so that they are suitable to be plotted on this second scale. What I need is a way to plot two series on different scales in the same area. The product I want is similar to this image: http://www.chamotlabs.com/ApplicationNotes/HTC/WoodTGA.gif

  • 1

    @Rodrigosimetti I edited the answer with a solution for data at different scales.

  • Perfect, @Molx. Exactly that.

-1

And in case I use barplot or boxplot for both variavies? What argument should I use so that Plots do not overlap?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.