alily
alily

Reputation: 299

label median values of boxplot in R

I have plotted a boxplot for timeseries data with freq=12 in R. I want R to display median values in boxplot for every month. How would I do that?

R code:

st_ts = ts(stocks[,2],start = c(2000,4),end = c(2015,10),frequency = 12)
boxplot(st_ts ~ cycle(st_ts))

enter image description here

Upvotes: 3

Views: 1183

Answers (1)

mdag02
mdag02

Reputation: 1165

Here with ggplot

library(ggplot2)

data <- data.frame(abs = as.factor(rep(1:12, 10)), ord = rnorm(120, 5, 1))

calcmed <- function(x) {
   return(c(y = 8, label = round(median(x), 1)))
   # modify 8 to suit your needs
}

ggplot(data, aes(abs, ord)) +
     geom_boxplot() +
     stat_summary(fun.data = calcmed, geom = "text") +
     #annotate("text", x = 1.4, y = 8.3, label = "median :") +
     xlab("Abs") +
     ylab("Ord") +
     ggtitle("Boxplot")

sample

Upvotes: 3

Related Questions