ggforce icon indicating copy to clipboard operation
ggforce copied to clipboard

`geom_mark_rect` does not work with histograms

Open joelgombin opened this issue 4 years ago • 1 comments

I'd like to use geom_mark_rect with a histogram, but this doesn't seem to work as it expects a y aesthetics. Here is a reprex:

library(ggplot2)
library(ggforce)
library(dplyr)
#> 
#> Attachement du package : 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

df <- cars %>% 
  mutate(category = if_else(speed < 15, "a", "b"))

ggplot(df, aes(x = speed)) +
  geom_histogram() +
  geom_mark_rect(aes(label = category))
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> Error: geom_mark_rect requires the following missing aesthetics: y

I'm using ggforce 0.3.1 and ggplot2 3.2.1.

joelgombin avatar Mar 29 '20 09:03 joelgombin

I would also like to know how to accomplish this

fkgruber avatar Sep 29 '21 20:09 fkgruber

Since I don't know exactly how you intent to use it I can give a specific answer, but setting stat = "bin" in geom_mark_rect() should allow you access to the same values as those used in the geom_histogram() layer

thomasp85 avatar Sep 02 '22 08:09 thomasp85