ggplot treatment of -Inf on Log scale Y axis inconsistent



I think ggplot is inconsistent in how it treats -inf in log scale. Below are two plots that display similar information. In the first case, I use a box plot to show the 25th, 50th, and 75th percentiles of the example dataset on a log scale. As you can see, it removes the 0 (or - inf values on the log scale) and creates the box plot from the remaining data points.

In the second example, I precalculate the 25th, 50th, and 75th percentiles and then use geom_point to make a similar plot with log scales. In this case, the zero value is simply plotted at the bottom of the axis instead of being removed. I think this behaviour is better. Is there a way to plot the box plot without removing the -inf values?

Thanks a lot.

# create dataset
Test_Data <- data.frame("Values" = seq(0,10000,1))
Test_Data$Values[Test_Data$Values%%3==0] <- 0

Test_Data$Groups[Test_Data$Values%%2==0] <- 'A'
Test_Data$Groups[Test_Data$Values%%2!=0] <- 'B'
Test_Data$Values <- Test_Data$Values/10000

# plot boxplot
ggplot(data = Test_Data, aes(x= Groups, y = Values)) + 
  scale_y_continuous(trans='log10', limits = c(1e-5, 1e1)) +
  annotation_logticks(sides = "l") 

# Create dataset with quantile measures
Test_Data_Processed <- Test_Data %>%
  select_all() %>%
  group_by(Groups) %>%
  summarise(Num = n(),
            Median = median(Values),
            Percnt_25 = quantile(Values,.25),
            Percnt_75 = quantile(Values, .75)) %>%
  gather(Measure, Value, Median:Percnt_75)

# plot with geom_point
ggplot(data = Test_Data_Processed, aes(x= Groups, y = Value, shape = Measure, color = Groups)) + 
  geom_point(size = 4) +
  scale_y_continuous(trans='log10', limits = c(1e-5, 1e1)) +
  annotation_logticks(sides = "l") 



I see that you've also posted this as an issue on GitHub:

If you cross-post issues here and on GitHub (or, as sometimes happens, it becomes apparent that something posted to the forum is an actual bug in the package, and an issue is filed as a result) we ask that you also post the link to the issue here, so that others can follow the progress.

Please see our FAQ on cross-posting below:



Hi Mara,

Thanks for pointing that out. The reason I cross posted was because I wasn't sure if this was something I didn't know how to do or whether it was actually not possible to do it. I have also cross posted it on stack exchange and didn't realize that it may be impolite till I read the FAQ. I also wasn't sure if the members of the forum overlapped considerably or not.

Thanks for sharing the FAQ and I will update all posts once an answer appears.


1 Like

closed #4

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.