showing medians for subgroups on a ggplot histogram

Hello,
is there a possibility to calculate subgroup medians within a ggplot histogram (eg. using stat or after_stat functions)?
I have a ggplot like the one below, but I am at a loss as to how to generate separate median lines for every subgroup (created with fill= ) from within ggplot.

library(tidyverse)
attach(starwars)
ggplot()+
         aes(x=height, fill = gender)+
         geom_histogram(col="white", alpha=0.5, position = 'identity')+  
         labs(x="height", y=NULL)+
         geom_vline(xintercept=median(height, na.rm = TRUE), linetype="dotted")

Why not just calculate the median for each group and add two different geom_vline()? Replace the xintercept argument with the numeric values of the medians.

Well, there are many methods that allow to calculate statistics inside ggplot, I thought there is one for such problem too and I would gladly learn that.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.