Dipojjal Saha
Dipojjal Saha

Reputation: 95

How to colour the text of the median value in boxplot in ggplot?

How would I change the colour of the text label from median to any lighter colour such as white?

set.seed(1)
library(plyr)
DF <- data.frame(TYPE = sample(letters[1:3], 500, replace = TRUE), 
                 PROVIDER = letters[1:5], 
                 VALUE = rnorm(500))

Get the medians by type and provider (both columns will exist in the new data.frame):

meds <- ddply(DF, .(TYPE, PROVIDER), summarize, med = median(VALUE))

ggplot(DF, aes(x = PROVIDER, y =V ALUE)) + 
  geom_boxplot(fill = "#44546A") + facet_wrap(~TYPE) + 
  geom_text(data = meds, aes(y = med, label = round(med,2)), size = 3, vjust = -0.5)

Upvotes: 0

Views: 104

Answers (1)

Miff
Miff

Reputation: 7941

col is an acceptable argument to geom_text(), see the help file by typing ?geom_text (the Aesthetics section) so changing your plot to

ggplot(DF, aes(x=PROVIDER,y= VALUE)) + geom_boxplot(fill="#44546A") + facet_wrap(~TYPE) + 
    geom_text(data = meds, aes(y = med, label = round(med,2)),size = 3, vjust = -0.5, col="white")

changes the colour of the median text as required.

Upvotes: 1

Related Questions