R中箱线图的标签中值

label median values of boxplot in R

我在 R 中为频率为 12 的时间序列数据绘制了一个 boxplot。我希望 R 每个月在 boxplot 中显示中值。我该怎么做?

R代码:

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

这里用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")