Using log transformation but need to preserve 0

I have a distribution of travel time. I am looking to do a log with base 2 transformations to get better graphs. Since, log is undefined at 0, the function leaves out all these values.
Is there any way to preserve all values that have 0 travel time and just use log approximation on other values such that
if x >0 then log (x);
else 0

MWE:
t = tibble(x = c(0, 0, 0, 1, 1, 1, 10, 10, 10, 100, 100, 100, 1000, 1000, 1000))

p = t %>%
ggplot(aes(x = x)) +
geom_histogram() +
scale_x_continuous(
trans=scales::pseudo_log_trans(base = 10),
breaks = scales::trans_breaks("log10", identity))

newx = ifelse(x==0,0,log2(x))

(Posting form same working team as OP)

It's not sufficient to simply transform the data to log2. We would like to only modify the scales and x-ticks. But the underlying numbers are the same.

This example from the documentation seems most relevant. You can still read the graph and say "Lots of animals have a body weight around 10".

I think this likely solves the problem

t = tibble(x = c(0, 0, 0, 1, 1, 1, 10, 10, 10, 100, 100, 100, 1000, 1000, 1000)); 
p = t %>% 
        ggplot(aes(x = x)) + 
        geom_histogram() + 
        scale_x_continuous( 
         trans = scales::pseudo_log_trans(base = 10), 
         breaks = scales::trans_breaks("log10", function(x) 10^x), 
         labels = scales::trans_format("log10", scales::math_format(10^.x))
     )   

But there are a few warnings about NaNs produced to track. down.

Additionally, this only creates one tick, at 10^2. The tick at 10^3 does not show up, nor the ticks at 10^1 or 10^0. This is odd.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.