Plotting multiple dendrogram plots by ggplot and faceting them by 2 two methods by ggplot, R

huangapple go评论61阅读模式
英文:

Plotting multiple dendrogram plots by ggplot and faceting them by 2 two methods by ggplot, R

问题

I have translated the code portion for you:

我有一个类似下面的数据框。
```R
sakubun<-read.csv("https://www.iwanami.co.jp/files/moreinfo/0298960/sakubun259f.csv",
                  row.names=1, 
                  fileEncoding = 'cp932')
sakubunp<-sakubun/rowSums(sakubun)

我对这个数据框进行了聚类分析,并尝试绘制结果。聚类分析有两个不同的部分,每个部分都有多种方法。因此,结果可以是多种模式,就像这些方法组合在一起一样多。

我想在一张图片中显示多个结果,以及应用的组合。我成功地将结果放入了列表对象中。
我的代码如下所示。

d.method <- c("euclidean", "maximum", "manhattan", "canberra", "binary", "minkowski")
c.method <- c("ward.D", "ward.D2", "single", "complete", "average", "mcquitty", "median", "centroid")

result_list <- list()

for (i in 1:length(d.method)) {
  for (j in 1:length(c.method)) {
    cl_plot <- hclust(dist(sakubunp[, -32], method = d.method[i]), method = c.method[j]) %>% 
      stats::as.dendrogram() %>% 
      dendextend::as.ggdend() %>% 
      ggplot2::ggplot()
    
    result_list[[paste(d.method[i], c.method[j], sep = "_")]] <- cl_plot
  }
}

在我的数据中,"d.method" 可以有六种方法,"c.method" 可以有八种方法,所以会有 6x8=48 种图形模式,我想将它们排列成矩阵形式(列 = d.method,行 = c.method)。

但是,我无法找到如何将它们放入图中,使用 ggplot::facet_grid() 函数。我该如何制作这样的图形?


Please note that I've translated the code portion as requested, and you can continue working with the code to achieve your desired plot.

<details>
<summary>英文:</summary>

I have a dataframe like below.

sakubun<-read.csv("https://www.iwanami.co.jp/files/moreinfo/0298960/sakubun259f.csv",
row.names=1,
fileEncoding = 'cp932')
sakubunp<-sakubun/rowSums(sakubun)

I applied cluster analysis to this data frame and try to plot the result. Cluster analysis has two different part and each has multiple methods. So the result can be a multitude of patterns, as many as the combination of these methods applied together.

I want to show multiple results as well as which combination is applied in one picture.
I succeeded in putting the results into list object. 
My code is as following

d.method <- c("euclidean", "maximum", "manhattan", "canberra", "binary", "minkowski")
c.method <- c("ward.D", "ward.D2", "single", "complete", "average", "mcquitty", "median", "centroid")

result_list <- list()

for (i in 1:length(d.method)) {
for (j in 1:length(c.method)) {
cl_plot <- hclust(dist(sakubunp[, -32], method = d.method[i]), method = c.method[j]) %>%
stats::as.dendrogram() %>%
dendextend::as.ggdend() %>%
ggplot2::ggplot()

result_list[[paste(d.method[i], c.method[j], sep = &quot;_&quot;)]] &lt;- cl_plot

}
}


In my data, there can be six methods for &quot;d.method&quot; and eight for &quot;c.method&quot;, so there will be 6x8=48 patterns of plots and I want to arrange them into a matrix-form (column = d.method, row = c.method).

But i can&#39;t find how to put them into plots, with using `ggplot::facet_grid()` function. How can I make such a plot?

</details>


# 答案1
**得分**: 1

以下是翻译好的内容:

如果不需要使用ggplot的facet功能,您可以使用基本的R图形参数`mfrow`,并按照以下方式遍历您的数据框,每行一个图:
- 创建一个包含每种方法组合的数据框`d`:
```R
d.method <- c("euclidean", "maximum", "manhattan", "canberra", "binary", "minkowski")
c.method <- c("ward.D", "ward.D2", "single", "complete", "average", "mcquitty", "median", "centroid")

d <- 
  setNames(expand.grid(d.method, c.method),
           c('d.method', 'c.method')
           )
> d |> head(2)
   d.method c.method
1 euclidean   ward.D
2   maximum   ward.D
  • 使用mfrow准备一个多图面板(最好将绘图窗口调整得很大,根据需要调整绘图参数:?par):
par(mfrow = c(8, 6),
    mar = rep(1, 4) ## 减小外边距(facets之间的间隔)
    )
  • 为每一行创建一个hclust对象(记得将结果包装成一个列表),然后进行一次mutate绘图:
library(dplyr)

d |>
  rowwise() |>
  mutate(clust = 
           ## 您可以将任何内容(如聚类结果)打包到*列表*列中:
           list( 
             iris[1:4] |>
             dist(method = d.method) |>
             hclust(method = c.method)
           ),
         plot(clust,
              main = paste(c.method, d.method, sep = ', ') , sub = '', 
              labels = FALSE,
              xlab = '', ylab = '')
         )

Plotting multiple dendrogram plots by ggplot and faceting them by 2 two methods by ggplot, R

英文:

If it doesn't have to be ggplot's facetting facilities, you can use base R's graphic parameter mfrow and walk through your dataframe, casting one plot per row like this:

  • create dataframe d with one row per method combination:
d.method &lt;- c(&quot;euclidean&quot;, &quot;maximum&quot;, &quot;manhattan&quot;, &quot;canberra&quot;, &quot;binary&quot;, &quot;minkowski&quot;)
c.method &lt;- c(&quot;ward.D&quot;, &quot;ward.D2&quot;, &quot;single&quot;, &quot;complete&quot;, &quot;average&quot;, &quot;mcquitty&quot;, &quot;median&quot;, &quot;centroid&quot;)

d &lt;- 
  setNames(expand.grid(d.method, c.method),
           c(&#39;d.method&#39;, &#39;c.method&#39;)
           )
&gt; d |&gt; head(2)
   d.method c.method
1 euclidean   ward.D
2   maximum   ward.D
  • prepare a multifigure panel with mfrow (better make the plot window really large, adjust plot parameters as needed: ?par):
par(mfrow = c(8, 6),
    mar = rep(1, 4) ## decrease outer margin (= gap between facets)
    )
  • create one hclust object per row (remember to wrap the result into a list) and
  • plot it in one mutate-go:
library(dplyr)

d |&gt;
  rowwise() |&gt;
  mutate(clust = 
           ## you can pack anything (like a cluster result) in a *list* column:
           list( 
             iris[1:4] |&gt;
             dist(method = d.method) |&gt;
             hclust(method = c.method)
           ),
         plot(clust,
              main = paste(c.method, d.method, sep =&#39;, &#39;) , sub = &#39;&#39;, 
              labels = FALSE,
              xlab = &#39;&#39;, ylab = &#39;&#39;)
         )

Plotting multiple dendrogram plots by ggplot and faceting them by 2 two methods by ggplot, R

huangapple
  • 本文由 发表于 2023年5月21日 19:57:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76299796.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定