突出小平面图中的单个值

时间:2016-04-19 16:20:18

标签: r ggplot2

我试图通过在其周围绘制一个方框或指向它的箭头来突出刻面平铺图中的最小BIC值。

set.seed(1000)
tseries<-data.frame("value"=rnorm(300, 50, 7.5))
BICs<- apply(expand.grid(0:2,0:2,0:2),1L,
               function(rw)BIC(arima(tseries$value,order=rw)))
ARIMA<-expand.grid(0:2,0:2,0:2)
names(ARIMA)<-c("p","d","q")
ARIMA$BICs<-BICs
ARIMA$d<-paste("d=",ARIMA$d,sep="")

ggplot(ARIMA, aes(x=q, y=p)) +
  geom_tile(aes(fill = BICs)) +
  geom_text(aes(label = round(BICs,2)),colour="yellow",fontface="bold",size=4) +
  scale_fill_gradient(low = "cyan", high = "blue4")+ 
  facet_wrap(~d,nrow=2)

smallest value is the ARIMA(p=1,d=1,q=1) specification

在堆栈周围进行搜索似乎是围绕单个值绘制框的最佳解决方案是使用正确的矩形美学来调用新的数据框

best<-data.frame("xmin"=ARIMA[which(ARIMA$BICs==min(ARIMA$BICs)),3]-.5,
                      "xmax"=ARIMA[which(ARIMA$BICs==min(ARIMA$BICs)),3]+.5,
                      "ymin"=ARIMA[which(ARIMA$BICs==min(ARIMA$BICs)),1]-.5,
                      "ymax"=ARIMA[which(ARIMA$BICs==min(ARIMA$BICs)),1]+.5,
                      "d"=ARIMA[which(ARIMA$BICs==min(ARIMA$BICs)),2],
                      "BIC"=ARIMA[which(ARIMA$BICs==min(ARIMA$BICs)),4])
best$d<-factor(best$d,levels=levels(as.factor(ARIMA$d)))  

但是当我尝试放置geom_rect审美时它会吐出错误

1 个答案:

答案 0 :(得分:1)

我应该在arima最佳数据框中制作p q向量

best<-data.frame("q"=ARIMA[which(ARIMA$BICs==min(ARIMA$BICs)),3],
                 "p"=ARIMA[which(ARIMA$BICs==min(ARIMA$BICs)),1],
                 "d"=ARIMA[which(ARIMA$BICs==min(ARIMA$BICs)),2],
                 "BIC"=ARIMA[which(ARIMA$BICs==min(ARIMA$BICs)),4])
best$d<-factor(best$d,levels=levels(as.factor(ARIMA$d)))  

然后geom_rect像往常一样工作

geom_rect(data=best,aes(xmin=q-.5,xmax=q+.5,ymin=p-.5,ymax=p+.5),colour="yellow",alpha=0)

enter image description here