11

I'm trying to plot a histogram with a log y scale using ggplot, geom_histogram and scale_y_log10. Most regions (those with counts greater than 1) appear correct: the background is transparent and the histogram bars are filled with the default color black. But at counts of 1, the colors are inverted: black background and transparent fill of the histogram bars. This code (below) generates the example in the graph.

Can anyone explain the cause of this? I understand the problems that come with log scales but I can't seem to find a solution to this. I'm hoping there's a easy fix, or that I overlooked something.

set.seed(1)
df <- data.frame(E=sample(runif(100), 20, TRUE))
ggplot(df,aes(E)) + geom_histogram(binwidth=0.1) + scale_y_log10(limits=c(0.1,100)) + xlim(0,1)

Example of reversed color scheme below the count of 1

Joseph Kreke
  • 667
  • 1
  • 7
  • 18

1 Answers1

9

You can add drop=TRUE to the geom_histogram call to drop bins with zero counts (see ?stat_bin for details):

set.seed(1)
df <- data.frame(E=sample(runif(100), 20, TRUE))
ggplot(df,aes(E)) + 
  geom_histogram(binwidth=0.1, drop=TRUE) + 
  scale_y_log10(limits=c(0.1,100)) + 
  xlim(0,1)

EDIT: Since the scale starts at 1, it is impossible to display a bar of height 1. As mentioned in this answer, you can choose to start at different levels, but it may become misleading. Here's the code for this anyway:

require(scales)
mylog_trans <- 
function (base = exp(1), from = 0) 
{
  trans <- function(x) log(x, base) - from
  inv <- function(x) base^(x + from)
  trans_new("mylog", trans, inv, log_breaks(base = base), domain = c(base^from, Inf))
}

ggplot(df,aes(E)) + 
  geom_histogram(binwidth=0.1, drop=TRUE) + 
  scale_y_continuous(trans = mylog_trans(base=10, from=-1), limits=c(0.1,100)) +
  xlim(0,1)
Community
  • 1
  • 1
shadow
  • 21,823
  • 4
  • 63
  • 77
  • Thanks for the really quick answer. I was hoping for a simple solution like that but unfortunately it does not work properly. While the black areas disappear (where zeroes were) the places where 1s should appear do not. Everything below the level of 1 is removed. – Joseph Kreke Apr 15 '14 at 14:24
  • 2
    Currently, `geom_histogram` says: “`drop` is deprecated. Please use `pad` instead.” But `pad=TRUE` doesn’t do the same thing. – Ruud Aug 05 '16 at 10:17
  • 1
    @Ruud I have the same problem, i.e. ggplot2 tells me "Please use pad instead" but it does not do what drop used to do – Adrian Feb 09 '17 at 16:23