Search code examples
rggplot2geom-col

ggplot2 and log scale don't show values = 1


I want to plot a diagram with a log scales y axis:

data <- data.frame(x = seq_len(5), y = c(2,1,100,500,30))
ggplot(data) + aes(x,y) + geom_col() + scale_y_log10()

But because the horizontal axis always crosses the vertical axis at y = 1, the bar for y = 1 is never drawn.

enter image description here

I know that log(1) = 0, but can I set the plot base to 0.1 to let the bar start below 1?


Solution

  • Can't you just multiply the values of y by 10 but divide the labels by the same amount?

    ggplot(data) + aes(x, y * 10) +  geom_col() + 
      scale_y_log10(labels = function(x) x/10, name = "y")
    

    enter image description here

    You could do the same with a histogram:

    set.seed(2)
    ggplot(data.frame(x = rnorm(1000)), aes(x)) + 
      geom_histogram(aes(y = ..count.. * 10), color = "black", fill = "gold") +
      scale_y_log10(labels = function(x) x/10, name = "count")
    

    enter image description here