2015-10-23 77 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

这里