> library(reshape2) > library(ggplot2) > mymatrix<-matrix(runif(30,min=-0.1,max=0.1),nrow=5,ncol=6) > colnames(mymatrix)<-c("房地產業","國防軍工","建築業","信息科技業","化工行業","電氣工業") > mymatrix 房地產業 國防軍工 建築業 信息科技業 化工行業 電氣工業 [1,] -0.085724727 -0.05439762 -0.02239555 -0.034122123 -0.06915880 0.02343567 [2,] 0.024749188 -0.08855248 0.02641241 -0.045408533 -0.08644377 0.02015972 [3,] 0.058020560 0.06182446 0.07663317 -0.024530879 0.07593718 0.01692426 [4,] -0.006305735 -0.08288435 0.02140884 -0.039372059 -0.06864554 -0.04277815 [5,] -0.060101370 0.02608878 0.01004945 0.001181824 0.03706699 0.02555530 > fixdata<-melt(mymatrix) > head(fixdata,10) Var1 Var2 value 1 1 房地產業 -0.085724727 2 2 房地產業 0.024749188 3 3 房地產業 0.058020560 4 4 房地產業 -0.006305735 5 5 房地產業 -0.060101370 6 1 國防軍工 -0.054397615 7 2 國防軍工 -0.088552482 8 3 國防軍工 0.061824457 9 4 國防軍工 -0.082884352 10 5 國防軍工 0.026088781 > p<-ggplot(fixdata,aes(x=Var2,y=Var1,fill=value))+xlab("行業")+ylab("星期") > p1<-p+geom_tile() > print(p1)
> p2<-p1+geom_tile(color="white",size=0.1) > print(p2)
> p3<-p2+scale_fill_gradient(low='green',high='red') > print(p3)
> p4<-p3+guides(fill=FALSE) > print(p4)
> p5<-p4+geom_text(aes(label=round(value,3)),angle=45) > print(p5)