0

在 R 中,我有一个包含一个自变量和 9 个因变量的数据集,我想查看散点图、直方图以及相关值,如 inchart.Correlation()但我不想看到因变量之间的相关性......这是不必要的。

即在下面的模拟示例中,我只关心/想要查看顶行和最左侧的列,以及所有直方图、所有红线和重要性星等,但我不关心/不关心想要所有其他散点图和相关值。这是否可能/是否有一种巧妙的方法可以在一个可视化中查看所有这些...即自变量与所有因变量...?

模拟示例:

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/多个%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)
4

1 回答 1

0

所以这就是我到目前为止使用ggplot的方法plyr......我仍然缺少自变量的直方图......哦,multiplot来自这里: http ://wiki.stdout.org/rcookbook/Graphs/多个%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)
于 2012-10-04T07:00:11.693 回答