在下面的代码中,我试图为数据框的每个特征显示它与其他特征的关系。
因此,使用Peter Haschke的多图方法,我已经能够展示所有的组合。
suppressMessages(require(ggplot2))
suppressMessages(require(grid))
multiplot <- function(..., plotlist = NULL, file, cols = 1, layout = NULL) {
plots <- c(list(...), plotlist)
numPlots = length(plots)
if (is.null(layout)) {
layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
ncol = cols, nrow = ceiling(numPlots/cols))
}
if (numPlots == 1) {
print(plots[[1]])
} else {
grid.newpage()
pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
for (i in 1:numPlots) {
matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
layout.pos.col = matchidx$col))
}
}
}
df <- data.frame(matrix(rnorm(400), nrow=100))
colnames(df) <- c("F1","F2","F3","F4")
pdf("example.pdf")
plots <- list()
count <- 1
for(i_feature in colnames(df)) {
for(j_feature in colnames(df)) {
if(i_feature == j_feature) {
empty_df <- data.frame()
blank_plot <- ggplot(empty_df) +
geom_point() +
xlim(0, 100) +
ylim(0, 100) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.ticks=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank()
)
blank_plot <- blank_plot + geom_text(aes(x=45,y=50,label=j_feature), size=6,fontface="bold")
plots[[count]] <- blank_plot
} else {
plots[[count]] <- ggplot(df,aes_string(x=i_feature,y=j_feature)) +
geom_point(size = 1) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.ticks=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank()
) +
theme(legend.position="none")
}
count <- count + 1
}
}
multiplot(plotlist = plots, cols = ncol(df))
garbage <- dev.off()
如您所见,我没有将一列与自身绘制,而是显示一个带有列名称的标签。这个实现的问题是这一行:
blank_plot <- blank_plot + geom_text(aes(x=45,y=50,label=j_feature), size=6,fontface="bold")
没有正确刷新标签名称,因此要处理的最后一个特征是始终显示的特征,如屏幕截图所示。
有人可以帮我解决这个问题吗?