0
votes

I want to highlight the metric mean and median in Histogram plot using vertical lines. red to denote mean and blue to denote median. I am able to draw tow different kines however unable to order the color codes as per metrics.

I have created a dataframe d which I pass to my ggplot geom_vline. The dataframe contains metric calculation of mean and median and the desired color codes.

    d = data.frame(metric = c(
      mean(titanic_merge_clean$Age, na.rm = TRUE),
      median(titanic_merge_clean$Age, na.rm = TRUE) ),
      colr = c("red", "blue"))

    titanic_merge_clean %>%
     ggplot(aes(x = Age)) +
     geom_histogram() +
     geom_vline(data = d, aes(xintercept = metric,
     color = colr))

dataframe d which is passed to ggplot geom_vline

Following is what d looks like:

enter image description here

[1]: https://i.stack.imgur.com/OlOPG.jpg

In the resulting ggplot histogram above ordering of lines have reversed. The red is indicating median and blue is indicating mean.:

2

2 Answers

2
votes

Since you specify a column of color names and map this to ggplot's color aesthetic, we can use scale_color_identity(). Your example is not reproducible, so here's a simple one using the mtcars data set.

library(ggplot2)

my.data <- mtcars
my.data$color <- sample(c('red', 'blue'), nrow(my.data), replace = T)

g <- ggplot(data = my.data, aes(x = cyl, y = mpg, color = color)) +
  geom_point() +
  scale_color_identity(guide = 'legend')
print(g)

enter image description here

2
votes

You need to specify colors outside the aes statement

d = data.frame(metric = c(
  mean(titanic_merge_clean$Age, na.rm = TRUE),
  median(titanic_merge_clean$Age, na.rm = TRUE) ),
  colr = c("red", "blue"))

titanic_merge_clean %>%
 ggplot(aes(x = Age)) +
 geom_histogram() +
 geom_vline(data = d, aes(xintercept = metric), colour=d$colr)