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

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

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

问题

I have translated the code portion for you:

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

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

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

  1. d.method <- c("euclidean", "maximum", "manhattan", "canberra", "binary", "minkowski")
  2. c.method <- c("ward.D", "ward.D2", "single", "complete", "average", "mcquitty", "median", "centroid")
  3. result_list <- list()
  4. for (i in 1:length(d.method)) {
  5. for (j in 1:length(c.method)) {
  6. cl_plot <- hclust(dist(sakubunp[, -32], method = d.method[i]), method = c.method[j]) %>%
  7. stats::as.dendrogram() %>%
  8. dendextend::as.ggdend() %>%
  9. ggplot2::ggplot()
  10. result_list[[paste(d.method[i], c.method[j], sep = "_")]] <- cl_plot
  11. }
  12. }

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

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

  1. Please note that I've translated the code portion as requested, and you can continue working with the code to achieve your desired plot.
  2. <details>
  3. <summary>英文:</summary>
  4. 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)

  1. 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.
  2. I want to show multiple results as well as which combination is applied in one picture.
  3. I succeeded in putting the results into list object.
  4. 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()

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

}
}

  1. 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).
  2. 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?
  3. </details>
  4. # 答案1
  5. **得分**: 1
  6. 以下是翻译好的内容:
  7. 如果不需要使用ggplotfacet功能,您可以使用基本的R图形参数`mfrow`,并按照以下方式遍历您的数据框,每行一个图:
  8. - 创建一个包含每种方法组合的数据框`d`
  9. ```R
  10. d.method <- c("euclidean", "maximum", "manhattan", "canberra", "binary", "minkowski")
  11. c.method <- c("ward.D", "ward.D2", "single", "complete", "average", "mcquitty", "median", "centroid")
  12. d <-
  13. setNames(expand.grid(d.method, c.method),
  14. c('d.method', 'c.method')
  15. )
  1. > d |> head(2)
  2. d.method c.method
  3. 1 euclidean ward.D
  4. 2 maximum ward.D
  • 使用mfrow准备一个多图面板(最好将绘图窗口调整得很大,根据需要调整绘图参数:?par):
  1. par(mfrow = c(8, 6),
  2. mar = rep(1, 4) ## 减小外边距(facets之间的间隔)
  3. )
  • 为每一行创建一个hclust对象(记得将结果包装成一个列表),然后进行一次mutate绘图:
  1. library(dplyr)
  2. d |>
  3. rowwise() |>
  4. mutate(clust =
  5. ## 您可以将任何内容(如聚类结果)打包到*列表*列中:
  6. list(
  7. iris[1:4] |>
  8. dist(method = d.method) |>
  9. hclust(method = c.method)
  10. ),
  11. plot(clust,
  12. main = paste(c.method, d.method, sep = ', ') , sub = '',
  13. labels = FALSE,
  14. xlab = '', ylab = '')
  15. )

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:
  1. d.method &lt;- c(&quot;euclidean&quot;, &quot;maximum&quot;, &quot;manhattan&quot;, &quot;canberra&quot;, &quot;binary&quot;, &quot;minkowski&quot;)
  2. 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;)
  3. d &lt;-
  4. setNames(expand.grid(d.method, c.method),
  5. c(&#39;d.method&#39;, &#39;c.method&#39;)
  6. )
  1. &gt; d |&gt; head(2)
  2. d.method c.method
  3. 1 euclidean ward.D
  4. 2 maximum ward.D
  • prepare a multifigure panel with mfrow (better make the plot window really large, adjust plot parameters as needed: ?par):
  1. par(mfrow = c(8, 6),
  2. mar = rep(1, 4) ## decrease outer margin (= gap between facets)
  3. )
  • create one hclust object per row (remember to wrap the result into a list) and
  • plot it in one mutate-go:
  1. library(dplyr)
  2. d |&gt;
  3. rowwise() |&gt;
  4. mutate(clust =
  5. ## you can pack anything (like a cluster result) in a *list* column:
  6. list(
  7. iris[1:4] |&gt;
  8. dist(method = d.method) |&gt;
  9. hclust(method = c.method)
  10. ),
  11. plot(clust,
  12. main = paste(c.method, d.method, sep =&#39;, &#39;) , sub = &#39;&#39;,
  13. labels = FALSE,
  14. xlab = &#39;&#39;, ylab = &#39;&#39;)
  15. )

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:

确定