英文:
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 = "_")]] <- cl_plot
}
}
In my data, there can be six methods for "d.method" and eight for "c.method", 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'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 = '')
)
英文:
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 <- 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
- 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 |>
rowwise() |>
mutate(clust =
## you can pack anything (like a cluster result) in a *list* column:
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 = '')
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论