在R中,我有一個數據集有一個獨立變量和9個因變量,我想看到散點圖,直方圖加相關值,如chart.Correlation()
,但我不想要看到因變量之間的相關性......因爲它是不必要的。chart.Correlation independent vs dependent variables visualization
即在下面的模擬示例中,我只關心/希望看到最上一行,最左邊的列,所有的直方圖,所有的紅線和重要明星等,但我不在乎/不要求其他所有其他散點圖和相關值。這是否可能/是否有一種在一個可視化中看到所有這些的簡潔方式......即,獨立變量vs所有因變量...?
模擬例如:
d <- xts(matrix(rnorm(10000),ncol=10), Sys.Date()-1000:1)
library(PerformanceAnalytics)
chart.Correlation(d)
在一個側面說明...我得到的是一些從chart.Correlation
產生的相關值的字體大小有點懊惱......任何方式設定最低和最大的字體大小,使字體大小不會變得不可讀...
也請隨意使用任何其他軟件包(如ggplot2等),你認爲可能是有用的,以幫助找到問題的解決方案。
在此先感謝
編輯:
原來這就是我想出迄今使用ggplot
和plyr
......我仍然失蹤的自變量的直方圖...哦和multiplot
來自這裏:http://wiki.stdout.org/rcookbook/Graphs/Multiple%20graphs%20on%20one%20page%20(ggplot2)/
,現在已經包括它的答案...但任何其他建議/改進將受到歡迎....
require(plyr)
require(ggplot2)
indep.dep.cor <- function(xts.obj, title=""){
# First column always assumed to be independent
df <- data.frame(coredata(xts.obj))
assign('df',df,envir=.GlobalEnv)
df.l <- melt(df, id.vars=colnames(df)[1], measure.vars=colnames(df)[2:ncol(df)])
assign('df.l',df.l, envir=.GlobalEnv)
cor.vals <- ddply(df.l, c("variable"), summarise, round(cor(df[,1],value),3))
stars <- ddply(df.l, c("variable"), summarise, symnum(cor.test(df[,1],value)$p.value, corr = FALSE, na = FALSE, cutpoints = c(0, 0.001, 0.01, 0.05, 0.1, 1), symbols = c("***", "**", "*", ".", " ")))
cor.vals$stars <- stars[,2]
assign('cor.vals',cor.vals,envir=.GlobalEnv)
bin.w <- min((ddply(df.l,c("variable"),summarise,diff(range(value))/30))[,2])
m1 <- ggplot(df.l,aes_string(x="value"))+
facet_grid(.~variable)+
stat_density(aes(y=..density..),fill=NA, colour="red", size=1.2)+
geom_histogram(aes(y=..density..),fill="white", colour="black", binwidth=bin.w)+
opts(title=title)
m2 <- ggplot(df.l,aes_string(x=colnames(df.l)[1], y="value"))+
facet_grid(.~variable)+geom_point(aes(alpha=0.2))+
opts(legend.position="none")+
geom_text(data=cor.vals,aes(label=paste(cor.vals[,2],cor.vals[,3]),size=abs(cor.vals[,2])*2,colour=cor.vals[,2]),x=Inf,y=Inf,vjust=1,hjust=1,show_guide=FALSE)+
scale_colour_gradient(low = "red", high="blue")+
geom_smooth(method="loess")
multiplot(m1,m2,cols=1)
}
indep.dep.cor(d)
oups忘了添加它是它的一部分的包...即PerformanceAnalytics ...現已編輯將其包含在問題 –
刪除之前的評論。現在。請包括圖書館(plyr)...和沒有陰謀。 'multiplot'應該做什麼? –
多槽只在相同的輸出 –