rggplot2histogram

How do I plot a histogram in ggplot2 actually using layer and not geom_histogram()?


I was trying to use the layer( ) command to create a simple histogram instead of geom_histogram() and have been running into problems. Eventually, R could not find a geom called histogram! What am I missing here?

df <-data.frame(
sex <- rep(c("M","F"), each =50),
height <- round(c(rnorm(50, mean = 5.5, sd = .5), rnorm(50, mean = 4.8, sd = .8)),1)
)
df

library(ggplot2)
p <- ggplot()
p+ layer(data = df, geom = "histogram", mapping = aes(x=height, fill  = sex))
p+ layer(data = df, geom = "histogram", mapping = aes(x=height, fill  = sex), stat = "count")
p+ layer(data = df, geom = "histogram", mapping = aes(x=height, fill  = sex), stat = "count", position = "identity")


> >> p+ layer(data = df, geom = "histogram", mapping = aes(x=height, fill  = sex))
Error:
> ! Can't create layer without a stat.
> Run `rlang::last_trace()` to see where the error occurred.


> > p+ layer(data = df, geom = "histogram", mapping = aes(x=height, fill  = sex), stat = "count")
> Error:
> ! Can't create layer without a position.
> Run `rlang::last_trace()` to see where the error occurred.


> > p+ layer(data = df, geom = "histogram", mapping = aes(x=height, fill  = sex), stat = "count", > position = "identity")
> Error:
> ! Can't find geom called "histogram"
> Run `rlang::last_trace()` to see where the error occurred.


Solution

  • IMHO the docs could be improved by making clear that for the geom= only short names of geoms with a corresponding Geom proto subclass could be used, i.e. as a geom_histogram is basically a shorthand for a geom_bar with stat="bin" the corresponding Geom proto subclass is GeomBar. Hence, to draw a "histogram" using layer() use geom="bar" or geom=GeomBar.

    set.seed(123)
    
    df <- data.frame(
      sex <- rep(c("M", "F"), each = 50),
      height <- round(c(rnorm(50, mean = 5.5, sd = .5), rnorm(50, mean = 4.8, sd = .8)), 1)
    )
    
    library(ggplot2)
    
    p <- ggplot()
    
    p +
      layer(
        data = df, geom = "bar", mapping = aes(x = height, fill = sex),
        stat = "count", position = "identity"
      )
    

    Also note, that a geom_histogram uses stat="bin":

    
    p +
      layer(
        data = df, geom = "bar", mapping = aes(x = height, fill = sex),
        stat = "bin", position = "identity"
      )
    #> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.