R ggplot2 - Simple graph - cannot indicate log axis boundaries

I am trying to create a simple density graph in R in ggplot2. Here is my code that works great.

d <-  ggplot(result, aes(x=result$baseMeanA)) 
d + geom_density(colour="darkgreen", size=2, fill="darkgreen") + 
scale_x_log10() + scale_y_continuous(limits = c(0, 0.45))

The problem is that I cannot set the x axis, as I would like, to negative numbers.

scale_x_log10(limits= c(1, 10000))

works fine but

scale_x_log10(limits= c(-1, 10000))

not working at all! This gives me this error:

Error if (zero_range (range)) {: missing value requiring TRUE / FALSE

Please, help!

+5
source share
3 answers

If the range of limits should be partially below zero, you can log10-convert your variable and specify limits for continuous scale:

ggplot(result, aes(x=log10(baseMeanA))) +
   geom_density(colour="darkgreen", size=2, fill="darkgreen") + 
   scale_x_continuous(limits = c(-1, 10000) + 
   scale_y_continuous(limits = c(0, 0.45)) +
+3
source

, , , ? -, R

R> log(-1)
[1] NaN
Warning message:
In log(-1) : NaNs produced

R ?

+2

e ^ y cannot be negative. The exponential constant e is positive, and y is an exponent. and mathematical definition:

log (x) = y <==> x = e ^ y

That is why R cannot compute log (x) if x is negative. It just contradicts the definition of mathematics.

Hope this helps to understand why this plot is giving you trouble.

+1
source

All Articles