The data set in question is diamonds(in dplyr). I am trying to break out histogram of price by cut using facet_wrap. Also I want that each plot has median line. This is how I have done -
by_cut <- group_by(diamonds, cut)
med <- by_cut %>%
summarise(medn = median(price))
diam <- diamonds %>%
mutate(med_cut = ifelse(cut == med$cut[1], med$medn[1],
ifelse(cut == med$cut[2], med$medn[2],
ifelse(cut == med$cut[3], med$medn[3],
ifelse(cut == med$cut[4], med$medn[4], med$medn[5])))))
diam %>%
ggplot(aes(price)) +
geom_histogram(binwidth = 100) +
facet_wrap(~ cut, scales = "free_y") +
geom_vline(aes(xintercept= med_cut), colour='red')
and got the required plot (as below, this is what I wanted..) -
However I am sure this is not the ideal way for achieving the outcome, Hence I would like to know what is the best way to achieve the resultant plot?
based on @yeedle comments
here is better than previous attempt:
diam <- diamonds %>%
group_by(cut) %>%
mutate(medn = median(as.numeric(price)))
diam %>%
ggplot(aes(price)) +
geom_histogram(binwidth = 100) +
facet_wrap(~ cut, scales = "free_y") +
geom_vline(aes(xintercept= medn, group= cut), colour='red')
Can we improve it further ?