2015-10-23 131 views
1

我在R中爲freq = 12的時間序列數據繪製了boxplot。我希望R在每月的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)) 

enter image description here

回答

1
與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

這裏