How to change the y-axis scale on a line chart

Asked

Viewed 3,753 times

5

I’m trying to plot a graph where the points on the y-axis vary in decimal places but I couldn’t adjust the axis scale to vary in decimal places as well.

t_mean = c(30.24, 30.73, 30.94, 31.97, 31.28, 31.84, 31.56, 32.00)
time   = c(0,2,4,6,8,10,12,14)

plot=(t_mean~time, type ="l",  ylim=c(30.0,32), yaxt ="n")
axis(2, at=c(30.0:32.0))
  • Note: m:n is always a vector of integers, c(30.0:32.0) is not wrong but does not need to c() and discards the decimals. Much more readable will be 30:32.

1 answer

5


One way to do this is by creating a vector with the function seq. As the name suggests, the function seq creates a sequence of numbers. Simply enter the initial value, the final value and the increment. In the example below, I create a sequence that starts at 30 and goes up to 32, increasing from 0.1 to 0.1:

seq(from=30, to=32, by=0.1)
 [1] 30.0 30.1 30.2 30.3 30.4 30.5 30.6 30.7 30.8 30.9 31.0 31.1 31.2 31.3 31.4
[16] 31.5 31.6 31.7 31.8 31.9 32.0

Now just put this command inside the function axis of your code:

t_mean = c(30.24, 30.73, 30.94, 31.97, 31.28, 31.84, 31.56, 32.00)
time   = c(0,2,4,6,8,10,12,14)

plot(t_mean~time, type ="l",  ylim=c(30.0,32), yaxt ="n")
axis(2, at=seq(from=30, to=32, by=0.1))

inserir a descrição da imagem aqui

Attention: depending on the screen size of your computer or its resolution, it may not appear all the desired numbers when running the code I passed. See what happens when I shoot this same image on my computer, which has 13 inch screen and no zoom:

inserir a descrição da imagem aqui

Note that the numbers are no longer ranging from 0.2 in 0.2, but 0.3 in 0.3. If I wanted the variation to be identical to the one originally defined, from 0.1 to 0.1, I would have to necessarily reduce the graph source through the function par with the argument cex appropriate:

par(cex=.5)
plot(t_mean~time, type ="l",  ylim=c(30.0,32), yaxt ="n")
axis(2, at=seq(from=30, to=32, by=0.1))

inserir a descrição da imagem aqui

Note that all decimal values between 30 and 32 appeared, but the visualization was not good. It is the duty of those who are producing the graph to decide what is the best way to present it. None of the three I presented here is wrong, but in my opinion, the first (ranging from 0.2 to 0.2) is the best.

  • There is also the graphical parameter las. With las = 2 usually there is room for all values. (I tried axis(..., las = 2) and gave.)

Browser other questions tagged

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