How can I do this so that my scaled points do not get cropped in ggplot2?

I am making a scatter plot where I want the log to transform x-scale. I have some points at 0 that ggplot handles fine, since it does not eliminate them, however it pinches them.

What can I do to prevent points at 0 from being cut off? It seems that xlim() and the scale transformation are not reproduced together, only the last of them takes effect.

Example:

 myData <- data.frame(x = c(rexp(5), 0), y = "category") myBreaks <- c(.1, 1, 5) ggplot(myData, aes(x = x, y = y)) + scale_x_continuous(trans = "log", breaks = myBreaks, labels = myBreaks) + geom_point(size = 5, legend = F) 

clipped

+4
source share
1 answer

Since log(0) is -Inf , I suspect that your 0th item will always be truncated if you leave it at zero. I tried to mess with expand=... , coord_trans and everything else I could think of.

Here is a workaround:

  • set null values ​​to an arbitrary small value (say 1e-6)
  • includes a gap at this value
  • optionally indicate break 0

Code:

 myData <- data.frame(x = c(rexp(5), 0), y = "category") myData <- within(myData, x[x==0] <- 1e-6) myBreaks <- c(1e-6, 0.1, 1, 5) myLabels <- c(0, myBreaks[-1]) ggplot(myData, aes(x = x, y = y)) + geom_point(size = 5, legend = F) + scale_x_continuous( trans = "log", breaks = myBreaks, labels = myLabels ) 

enter image description here

+2
source

Source: https://habr.com/ru/post/1382438/


All Articles