Kornpob Bhirombhakdi
Kornpob Bhirombhakdi

Reputation: 173

R plot, how to start y at non zero?

I have a set of data which goes from: 8e41 to ~ 1e44. I want to plot starting at 1e41 on the y-axis, however using the argument: ylim=c(1e41,1e45) does not work for me.

Here is the minimally reproducible code:

x = c(1,2)
y = c(1e41,1e44)
plot(x,y,ylim=c(1e41,1e45))

Upvotes: 0

Views: 1158

Answers (2)

Melissa Key
Melissa Key

Reputation: 4551

The problem is that 1e41 is so much closer to 0 than 1e45 that it's virtually the same. Have you considered working on the log scale?

plot(x,y,ylim=c(1e41,1e45), log = 'y')

or even

plot(x, y, log = 'y')

Think of this another way - rescale your data by dividing your range by 1e41: c(8e41, 1e44)/ 1e41 - you get 8 and 1000. Is there any significant difference between starting the scale at 0 (or 1) versus 8? If you chose to divide by 1e40 instead, you're looking at 80 and 10,000. Try the following code to see this:

m <- 1e41 # change this as desired
plot(x, y / m)
abline(h = c(0, 1e41 / m))

By changing m, the only thing that changes is the numbers on the y-axis, the relative positions do not change. Look at how close 0 and 8e41 are, and you'll see why it really doesn't matter whether or not the plot starts at 0 versus 1e41. As a fraction of the total height of the plot, the difference is 1/1000.

Changing the values at which the axis is labeled

Here's one more option for you - changing the values at which the plot is marked. That requires two steps - first, removing the axis labels when the plot is originally created, then adding in the ones you actually want:

plot(x, y, yaxt = 'none')
axis(2, c(1e41, seq(1e43, 1e44, 1e43)))

Upvotes: 2

Adam Warner
Adam Warner

Reputation: 1354

library(ggplot2)
x = c(1,2)
y = c(1e41,1e44)
data = data.frame(x,y)
ggplot(data, aes(x=x, y=log(y))) + geom_point() + ylim(90,150)

I think you should use the log of y, as it shows the same data.

Upvotes: 0

Related Questions