问题
I am doing the following:
x = c(0, 1, 2, 3, 4, 5)
y = x ^ 2
plot(x, y, log="y")
What I want is that the graph also show me the scatter point at (x, y)=(0, 0).
I know that log(0) = -Inf
. This will be the case when I am doing log(x)
but here I am not doing log(x)
. Rather, I am just changing the scale of y-axis to be logarithmic. Therefore, I need to know if there is some way for me to display the scatter point (x, y) = (0, 0) as well.
回答1:
No, what you are asking is mathematically impossible, because log(0) = -Inf
. The point (0, 0) cannot be shown on a log-scale plot.
A log-scale is produced by log-transforming the data values and exponentiating the values at the axis ticks. For example, to plot the value 100 in a log-10 scale, you first log-transform 100 to log10(100) = 2
, and then you transform the corresponding axis tick from 2 to 10^2 = 100
. Thus, to plot the value 0 in a log-scale plot, you still need to calculate log10(0)
, even if the corresponding axis tick would be 10^-Inf = 0
.
回答2:
If your aim is to have a non-linear y-axis, and not necessarily a log-scale, then you can follow something like what's below.
# transfrom y-values
ny <- sqrt(y)
# plot the transformed values
plot(x, ny, yaxt='n', ylab = "y")
# label the y-axis
axis(side = 2, at = ny, labels = y)
Also, if you know what you want to replace log(0) with, then you can do that via ny
, but I don't advise using log-scale when there is a zero.
来源:https://stackoverflow.com/questions/47667042/extend-an-axis-limit-to-1-when-that-axis-is-logarithmic-in-r