在矩阵布局中绘制直方图以分析群集

时间:2018-01-24 09:34:16

标签: r layout ggplot2

我正在尝试编写一个大函数来执行集群分析。

目前,我正在努力完成最后一项任务: 在矩阵布局中绘制直方图,其中行代表群集,列代表变量。

考虑以下样本:

df <- data.frame(x = rnorm(50,5), y = rnorm(50, 10))

目标是使用以下图表并将它们合并为矩阵布局:

library(ggplot2)

plot11 <- ggplot() +
        geom_histogram(data = df,
                       aes(x = x, y = (..count..)/sum(..count..)),
                       position = "identity",
                       fill = "lightblue", color = "black") +
        xlab("1,1")

plot12 <- ggplot() +
        geom_histogram(data = df,
                       aes(x = x, y = (..count..)/sum(..count..)),
                       position = "identity",
                       fill = "lightblue", color = "black") +
        xlab("1,2")

plot13 <- ggplot() +
        geom_histogram(data = df,
                       aes(x = x, y = (..count..)/sum(..count..)),
                       position = "identity",
                       fill = "lightblue", color = "black") +
        xlab("1,3")

plot21 <- ggplot() +
        geom_histogram(data = df,
                       aes(x = x, y = (..count..)/sum(..count..)),
                       position = "identity",
                       fill = "lightblue", color = "black") +
        xlab("2,1")

plot22 <- ggplot() +
        geom_histogram(data = df,
                       aes(x = x, y = (..count..)/sum(..count..)),
                       position = "identity",
                       fill = "lightblue", color = "black") +
        xlab("2,2")

plot23 <- ggplot() +
        geom_histogram(data = df,
                       aes(x = x, y = (..count..)/sum(..count..)),
                       position = "identity",
                       fill = "lightblue", color = "black") +
        xlab("2,3")


plot31 <- ggplot() +
        geom_histogram(data = df,
                       aes(x = x, y = (..count..)/sum(..count..)),
                       position = "identity",
                       fill = "lightblue", color = "black") +
        xlab("3,1")


plot32 <- ggplot() +
        geom_histogram(data = df,
                       aes(x = x, y = (..count..)/sum(..count..)),
                       position = "identity",
                       fill = "lightblue", color = "black") +
        xlab("3,2")

对于每个绘图,第一个索引表示行,第二个索引表示列。因此,plot32应显示row = 3 and col = 2

解决方案需要使用索引才能引用绘图对象

例如,

for(cluster in 1:k) {
    for(variable in 1:3) {
        #add plot paste0("plot",cluster,variable) to the matrix layout in row = cluster and col = variable
    }
}

以下是如何安排情节: enter image description here

显然,最终的可视化比这更清晰,因为

  • 根据该群集的相对重要性对图表进行排序
  • 对于每个群集,只有该群集的文本描述的绘图
  • 在分类变量的情况下,将使用饼图

然而,主要问题是要了解如何创建矩阵布局,我可以插入所有图。

更新

enter image description here 这是我根据你的建议得到的。

2 个答案:

答案 0 :(得分:1)

牛皮画怎么样

library("cowplot")
plot_grid(plot11,plot12,plot13,plot21,ncol = 2, nrow = 2)

或者如果您可以使用您的函数填充它,您可以传递一个图表列表:

plotList<-list(plot11,plot12,plot13,plot21)
plot_grid(plotlist=plotList,ncol = 2, nrow = 2)

enter image description here

答案 1 :(得分:1)

我所采用的是你处理一个coninant表单来创建一个字符串并使用它(当然,多时隙函数的代码不是我的):

 multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  require(grid)

  # Make a list from the ... arguments and plotlist
  plots <- c(list(...), plotlist)

  numPlots = length(plots)

  # If layout is NULL, then use 'cols' to determine layout
  if (is.null(layout)) {
    # Make the panel
# ncol: Number of columns of plots
# nrow: Number of rows needed, calculated from # of cols
layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
                 ncol = cols, nrow = ceiling(numPlots/cols))
  }

  if (numPlots==1) {
print(plots[[1]])

  } else {
# Set up the page
grid.newpage()
pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))

# Make each plot, in the correct location
for (i in 1:numPlots) {
  # Get the i,j matrix positions of the regions that contain this subplot
  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))
    }
  }
}

string_plots <- "multiplot(plotlist = list(plot11,plot12, plot13, plot21, plot22, plot23, plot31, plot32), cols = 3)"

eval(parse(text=string_plots))