Working with null log values ​​in R?

I draw the scatter plot(x, y)

, and I want it to be a log-rated, so I do: plot(log(x), log(y))

. I would like to deal with cases where any value in x

is 0, and therefore not in the graph, whereas the corresponding value is y

nonzero.

I would like to display the scatter with the log type, but the values ​​are natural numbers, that is, if in log2

, then there should be ticks 2^0, 2^1, 2^2, ...

, which would allow me to plot 0 values ​​on the scale as well, so as not to miss these points.

Here's an example:

> x = c(0, 1, 20, 100, 200, 500)
> y = c(1, 16, 32, 105, 300, 50)
> plot(x, y)

      

There are six points. If I use:

> plot(log2(x), log2(y))

      

There are only 5 plotted since x [0], y [0] is omitted (x value is 0). So I would like to plot the log values, but with tick labels, these are natural numbers that are simply labeled on the log scale. Then you can easily have on the same axis, 0, 2 ^ 0 (which of course is 1), 2 ^ 1, 2 ^ 2, etc. Etc. Then the point (x [0], y [0]) will still be built , keeping the scale of the log.

Side note: I don't think it's fair to post a question that would be very reasonable with an example. This is definitely up to date and relevant, and will be used for pretty much anyone plotting things over log meaning and care about border / edge cases.

(I know some people get around this by adding an arbitrary little constant to all the points, but I would like to avoid that as it's messy.)

+3


source to share


1 answer


If I understand that you want to plot x versus y in the log scale?

Here and an example using lattice

andlatticeExtra



# Some reproducible data
tm <- data.frame(x=seq(0,10,1),y=seq(0,10,1))
library(lattice) 
library(latticeExtra) 
xyplot(x ~ y , data=tm, 
       scales= list(x=list(log=2), 
                    y=list(log=2)), 
       xscale.components = xscale.components.logpower, ## to get pretty scales
        yscale.components = yscale.components.logpower 
) 

      

enter image description here

+1


source







All Articles