R ggplot2 - Simple plot- cannot specify log axis limits

user1678000 picture user1678000 · Sep 17, 2012 · Viewed 12.4k times · Source

I'm trying to create a simple densityplot in R in ggplot2. Here's my code which 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 adjust the x-axis as I would like, into negative numbers.

scale_x_log10(limits= c(1, 10000))

works great, but

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

does not work at all! It gives me this error:

Error in if (zero_range(range)) { : missing value where TRUE/FALSE needed

Please help!

Answer

Sven Hohenstein picture Sven Hohenstein · Sep 17, 2012

If the range of the limits should be partly below zero, you could log10-transform your variable and specify the limits for a 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)) +