How to suppress zeroes when using geom_histogram with scale_y_log10

Joseph Kreke

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

shadow

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)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to suppress output when running npm scripts

From Dev

When using gulp. Is there any way to suppress the 'Started' and 'Finished' log entries for certain tasks

From Dev

How to suppress warning messages when loading a library?

From Dev

How to remove leading zeroes from day and month values in Oracle, when parsing to string using to_char function?

From Dev

How to suppress awk output when using FS?

From Dev

How can I suppress the date when using pandoc to convert md to pdf?

From Dev

geom_text does not appear using scale_y_log10

From Dev

geom_density doesn't fill correctly with scale_y_log10

From Dev

When using `scale_x_log10`, how can I map `geom_text` accurately to `geom_bin2d`?

From Dev

How to suppress warnings in maven output when using maven-checkstyle-plugin?

From Dev

How do I suppress row names when using DT::renderDataTable in R shiny?

From Dev

How to suppress the error message when dividing 0 by 0 using np.divide (alongside other floats)?

From Dev

How do I suppress unwanted dollar and quote symbols when using "printf %q"?

From Dev

Suppress bold when using "__"

From Dev

How to suppress text using JQuery?

From Dev

How can I suppress command output when using backticks?

From Dev

Suppress 'file truncated' messages when using tail

From Dev

How to create a sequence with leading zeroes using brace expansion

From Dev

cURL: how to suppress strange output when redirecting?

From Dev

How to stop Excel from creating "phantom" zeroes when copy/pasting

From Dev

geom_density doesn't fill correctly with scale_y_log10

From Dev

How can I suppress printing the full path to subdirectories when using CDPATH=".:/some/path"?

From Dev

How to change all decimal to Zeroes using XSL

From Dev

How to suppress infinite loop note when using "IF 0 THEN SET"

From Dev

How to retain trailing zeroes when converting String to Number in javascript

From Dev

How to suppress the warming message by R when using commandArgs()

From Dev

How to replace multiple empty fields into zeroes using awk

From Dev

How to smooth geom_freqpoly to exclude zeroes

From Dev

How to weight by ratio of two variables with geom_histogram?

Related Related

  1. 1

    How to suppress output when running npm scripts

  2. 2

    When using gulp. Is there any way to suppress the 'Started' and 'Finished' log entries for certain tasks

  3. 3

    How to suppress warning messages when loading a library?

  4. 4

    How to remove leading zeroes from day and month values in Oracle, when parsing to string using to_char function?

  5. 5

    How to suppress awk output when using FS?

  6. 6

    How can I suppress the date when using pandoc to convert md to pdf?

  7. 7

    geom_text does not appear using scale_y_log10

  8. 8

    geom_density doesn't fill correctly with scale_y_log10

  9. 9

    When using `scale_x_log10`, how can I map `geom_text` accurately to `geom_bin2d`?

  10. 10

    How to suppress warnings in maven output when using maven-checkstyle-plugin?

  11. 11

    How do I suppress row names when using DT::renderDataTable in R shiny?

  12. 12

    How to suppress the error message when dividing 0 by 0 using np.divide (alongside other floats)?

  13. 13

    How do I suppress unwanted dollar and quote symbols when using "printf %q"?

  14. 14

    Suppress bold when using "__"

  15. 15

    How to suppress text using JQuery?

  16. 16

    How can I suppress command output when using backticks?

  17. 17

    Suppress 'file truncated' messages when using tail

  18. 18

    How to create a sequence with leading zeroes using brace expansion

  19. 19

    cURL: how to suppress strange output when redirecting?

  20. 20

    How to stop Excel from creating "phantom" zeroes when copy/pasting

  21. 21

    geom_density doesn't fill correctly with scale_y_log10

  22. 22

    How can I suppress printing the full path to subdirectories when using CDPATH=".:/some/path"?

  23. 23

    How to change all decimal to Zeroes using XSL

  24. 24

    How to suppress infinite loop note when using "IF 0 THEN SET"

  25. 25

    How to retain trailing zeroes when converting String to Number in javascript

  26. 26

    How to suppress the warming message by R when using commandArgs()

  27. 27

    How to replace multiple empty fields into zeroes using awk

  28. 28

    How to smooth geom_freqpoly to exclude zeroes

  29. 29

    How to weight by ratio of two variables with geom_histogram?

HotTag

Archive