R 从素食主义者到ggplot2
看到了一些关于如何导入素食者的ordiellipse输出以使用ggpot2绘图的问答,但是ordicluster呢 以经典沙丘为例:R 从素食主义者到ggplot2,r,plot,ggplot2,vegan,R,Plot,Ggplot2,Vegan,看到了一些关于如何导入素食者的ordiellipse输出以使用ggpot2绘图的问答,但是ordicluster呢 以经典沙丘为例: data(dune) data(dune.env) mod <- cca(dune ~ Management, dune.env) cl <- hclust(vegdist(dune)) plot(mod, type = "p", display="sites") ordicluster(mod, cl, prune=3, col = cutree(c
data(dune)
data(dune.env)
mod <- cca(dune ~ Management, dune.env)
cl <- hclust(vegdist(dune))
plot(mod, type = "p", display="sites")
ordicluster(mod, cl, prune=3, col = cutree(cl, 4))
数据(沙丘)
数据(沙丘环境)
mod我刚刚在GitHub上放了一个具有此功能的包。看
#
几天前我试图回答这个问题,但我想我被拒绝了,因为我只包含了一个指向我的包的链接。我写它是为了与Roxygen2和GitHub一起练习,打算包括来自vegan的ordiplot函数的所有功能。我自己在寻找答案时遇到了这个问题
我解决这个问题的方法是从vegan的ordicluster函数开始,编辑绘制线段的部分,将坐标收集到数据框中。这是我现在拥有的。除了纯素版本中提供的功能外,我还提供了将治疗组映射到符号形状的功能。我的职能是:
#' Add Dendrogram to Ordination Plot
#'
#' Modeled after the ordicluster function in vegan, this function overlays an ordination object with a cluster dendogram. Functionality has been added to include treatment groups.
#'
#' @param ord An ordination object.
#' @param cluster A cluster object from 'hclust' based on the same distance as 'ord.'
#' @param treatments A vector assigning treatments to samples.
#' @param choices Ordination axes to be plotted.
#' @param prune Number of upper level hierarchies removed from the dendrogram. If prune > 0, dendrogram will be disconnected.
#' @param col A vector of cluster group memberships. Used to assign colors to line segments for each cluster group.
#' @param plot A logical; defaults to TRUE.
#'
#' @details 'treatments' should be a vector of class factor and length equal to the number of samples included in the ordination and cluster; integers are not coerced into factors.
#'
#' @author Jari Oksanen, John Quensen
#'
#' @return Invisibly returns a list of the data frames used to make the plot (df_ord, df_segments) and the plot itself (plot).
#' @export
#' @import vegan
#' @import ggplot2
#' @importFrom grDevices col2rgb
#' @importFrom stats weighted.mean
#' @importFrom stats weights
#' @examples
#' data(dune)
#' data(dune.env)
#' dune.dist <- vegdist(dune, method="bray")
#' ord <- metaMDS(dune, k=3)
#' cl <- hclust(dune.bray, method="complete")
#' gg_ordicluster(ord, cluster=cl, treatments=dune.env$Management, prune=3, col=cutree(cl, 4))
#'
gg_ordicluster <- function (ord, cluster, treatments=NA, choices=c(1,2), prune = 0, col = 1, plot=TRUE)
{
if (is.numeric(treatments)) {
stop("'treatments' cannot be numeric")
}
n.trts <- nlevels(treatments)
if (n.trts==0) {
treatments <- "none"
n.trts <- 1
}
if (n.trts==1) {
show.legend <- FALSE
}
else {
show.legend <- TRUE
}
display <- "sites"
w <- stats::weights(ord, display)
weights.default <- function(object, ...) NULL
w <- eval(w)
mrg <- cluster$merge
ord.scores <- scores(ord, display = display, choices=choices)
if (nrow(mrg) != nrow(ord.scores) - 1)
stop("Dimensions do not match in 'ord' and 'cluster'")
if ((nrow(ord.scores) != length(treatments)) & (n.trts > 1))
stop("Dimensions of 'ord' and 'treatments' do not match")
if (length(w) == 1)
w <- rep(w, nrow(ord.scores))
n <- if (is.null(w))
rep(1, nrow(ord.scores))
else w
noden <- numeric(nrow(mrg) - prune)
go <- matrix(0, nrow(mrg) - prune, 2)
col <- rep(col, length = nrow(ord.scores))
col <- col2rgb(col)/255
nodecol <- matrix(NA, nrow(mrg) - prune, 3)
for (i in 1:(nrow(mrg) - prune)) {
a <- mrg[i, 1]
b <- mrg[i, 2]
one <- if (a < 0)
ord.scores[-a, ]
else go[a, ]
two <- if (b < 0)
ord.scores[-b, ]
else go[b, ]
n1 <- if (a < 0)
n[-a]
else noden[a]
n2 <- if (b < 0)
n[-b]
else noden[b]
xm <- weighted.mean(c(one[1], two[1]), w = c(n1, n2))
ym <- weighted.mean(c(one[2], two[2]), w = c(n1, n2))
go[i, ] <- c(xm, ym)
noden[i] <- n1 + n2
colone <- if (a < 0)
col[, -a]
else nodecol[a, ]
coltwo <- if (b < 0)
col[, -b]
else nodecol[b, ]
nodecol[i, ] <- (n1 * colone + n2 * coltwo)/noden[i]
# Rather than plotting the line segments, collect the coordinates and
# color into a data frame.
col.a = rgb(t(nodecol[i, ]))
temp <- c(one[1], one[2], two[1], two[2], col.a)
if (i==1){
temp2 <- temp
} else {
temp2 <- rbind(temp2, temp)
rownames(temp2) <- NULL # prevents duplicate row names
}
}
colnames(temp2) <- c("x", "y", "xend", "yend", "Group")
temp2 <- as.data.frame(temp2)
j <- sapply(temp2, is.factor)
temp2[j] <- lapply(temp2[j], as.character)
j <- c( rep(TRUE, 4), FALSE)
temp2[j] <- lapply(temp2[j], as.numeric)
df_segments <- temp2
df_ord <- as.data.frame(ord.scores)
df_ord$Treatment <- treatments
colnames(df_ord) <- c("x", "y", "Treatment")
xlab <- paste("Axis", choices[1], sep=" ")
ylab <- paste("Axis", choices[2], sep=" ")
plt <- ggplot() +
geom_segment(data=df_segments, aes(x=x, y=y, xend=xend, yend=yend),
color=df_segments$Group,
show.legend = FALSE) +
geom_point(data=df_ord, aes(x=x, y=y, shape=Treatment), size=3,
show.legend = show.legend) +
xlab(xlab) + ylab(ylab)
if (plot==TRUE) {print(plt)}
invisible(list(df_ord=df_ord, df_segments=df_segments, plot=plt))
}
将树状图添加到排序图
#'
#“模仿素食主义者的ordicluster函数,该函数将排序对象与聚类密度图叠加。已添加功能以包括治疗组。
#'
#“@param ord是一个排序对象。
#“@param根据与“ord”相同的距离从“hclust”聚集群集对象。”
#“@param treatments为样本指定处理的向量。
#“@param选择要绘制的坐标轴。
#“@param prune从树状图中删除的上层层次结构的数量。如果修剪>0,树状图将断开连接。
#“@param col是群集组成员身份的向量。用于为每个簇组的线段指定颜色。
#“@param绘制一个逻辑图;默认为TRUE。
#'
#“@details”treatments”应该是类因子和长度的向量,等于排序和聚类中包含的样本数量;整数不会强制转换为因子。
#'
#“@作者Jari Oksanen,John Quensen
#'
#“@return以不可见的方式返回用于生成绘图(df_ord、df_段)和绘图本身(plot)的数据帧列表。
#“@出口
#“@进口素食主义者
#“@import-ggplot2
#“@importfromGR2RGB
#“@importFrom stats weighted.mean
#“@importFrom stats weights”
#“@示例
#“数据(沙丘)
#'数据(dune.env)
#'dune.dist我刚刚在GitHub上放了一个具有此功能的包。看
#
几天前我试图回答这个问题,但我想我被拒绝了,因为我只包含了一个指向我的包的链接。我写它是为了与Roxygen2和GitHub一起练习,打算包括来自vegan的ordiplot函数的所有功能。我自己在寻找答案时遇到了这个问题
我解决这个问题的方法是从vegan的ordicluster函数开始,编辑绘制线段的部分,将坐标收集到数据框中。这是我现在拥有的。除了纯素版本中提供的功能外,我还提供了将治疗组映射到符号形状的功能。我的职能是:
#' Add Dendrogram to Ordination Plot
#'
#' Modeled after the ordicluster function in vegan, this function overlays an ordination object with a cluster dendogram. Functionality has been added to include treatment groups.
#'
#' @param ord An ordination object.
#' @param cluster A cluster object from 'hclust' based on the same distance as 'ord.'
#' @param treatments A vector assigning treatments to samples.
#' @param choices Ordination axes to be plotted.
#' @param prune Number of upper level hierarchies removed from the dendrogram. If prune > 0, dendrogram will be disconnected.
#' @param col A vector of cluster group memberships. Used to assign colors to line segments for each cluster group.
#' @param plot A logical; defaults to TRUE.
#'
#' @details 'treatments' should be a vector of class factor and length equal to the number of samples included in the ordination and cluster; integers are not coerced into factors.
#'
#' @author Jari Oksanen, John Quensen
#'
#' @return Invisibly returns a list of the data frames used to make the plot (df_ord, df_segments) and the plot itself (plot).
#' @export
#' @import vegan
#' @import ggplot2
#' @importFrom grDevices col2rgb
#' @importFrom stats weighted.mean
#' @importFrom stats weights
#' @examples
#' data(dune)
#' data(dune.env)
#' dune.dist <- vegdist(dune, method="bray")
#' ord <- metaMDS(dune, k=3)
#' cl <- hclust(dune.bray, method="complete")
#' gg_ordicluster(ord, cluster=cl, treatments=dune.env$Management, prune=3, col=cutree(cl, 4))
#'
gg_ordicluster <- function (ord, cluster, treatments=NA, choices=c(1,2), prune = 0, col = 1, plot=TRUE)
{
if (is.numeric(treatments)) {
stop("'treatments' cannot be numeric")
}
n.trts <- nlevels(treatments)
if (n.trts==0) {
treatments <- "none"
n.trts <- 1
}
if (n.trts==1) {
show.legend <- FALSE
}
else {
show.legend <- TRUE
}
display <- "sites"
w <- stats::weights(ord, display)
weights.default <- function(object, ...) NULL
w <- eval(w)
mrg <- cluster$merge
ord.scores <- scores(ord, display = display, choices=choices)
if (nrow(mrg) != nrow(ord.scores) - 1)
stop("Dimensions do not match in 'ord' and 'cluster'")
if ((nrow(ord.scores) != length(treatments)) & (n.trts > 1))
stop("Dimensions of 'ord' and 'treatments' do not match")
if (length(w) == 1)
w <- rep(w, nrow(ord.scores))
n <- if (is.null(w))
rep(1, nrow(ord.scores))
else w
noden <- numeric(nrow(mrg) - prune)
go <- matrix(0, nrow(mrg) - prune, 2)
col <- rep(col, length = nrow(ord.scores))
col <- col2rgb(col)/255
nodecol <- matrix(NA, nrow(mrg) - prune, 3)
for (i in 1:(nrow(mrg) - prune)) {
a <- mrg[i, 1]
b <- mrg[i, 2]
one <- if (a < 0)
ord.scores[-a, ]
else go[a, ]
two <- if (b < 0)
ord.scores[-b, ]
else go[b, ]
n1 <- if (a < 0)
n[-a]
else noden[a]
n2 <- if (b < 0)
n[-b]
else noden[b]
xm <- weighted.mean(c(one[1], two[1]), w = c(n1, n2))
ym <- weighted.mean(c(one[2], two[2]), w = c(n1, n2))
go[i, ] <- c(xm, ym)
noden[i] <- n1 + n2
colone <- if (a < 0)
col[, -a]
else nodecol[a, ]
coltwo <- if (b < 0)
col[, -b]
else nodecol[b, ]
nodecol[i, ] <- (n1 * colone + n2 * coltwo)/noden[i]
# Rather than plotting the line segments, collect the coordinates and
# color into a data frame.
col.a = rgb(t(nodecol[i, ]))
temp <- c(one[1], one[2], two[1], two[2], col.a)
if (i==1){
temp2 <- temp
} else {
temp2 <- rbind(temp2, temp)
rownames(temp2) <- NULL # prevents duplicate row names
}
}
colnames(temp2) <- c("x", "y", "xend", "yend", "Group")
temp2 <- as.data.frame(temp2)
j <- sapply(temp2, is.factor)
temp2[j] <- lapply(temp2[j], as.character)
j <- c( rep(TRUE, 4), FALSE)
temp2[j] <- lapply(temp2[j], as.numeric)
df_segments <- temp2
df_ord <- as.data.frame(ord.scores)
df_ord$Treatment <- treatments
colnames(df_ord) <- c("x", "y", "Treatment")
xlab <- paste("Axis", choices[1], sep=" ")
ylab <- paste("Axis", choices[2], sep=" ")
plt <- ggplot() +
geom_segment(data=df_segments, aes(x=x, y=y, xend=xend, yend=yend),
color=df_segments$Group,
show.legend = FALSE) +
geom_point(data=df_ord, aes(x=x, y=y, shape=Treatment), size=3,
show.legend = show.legend) +
xlab(xlab) + ylab(ylab)
if (plot==TRUE) {print(plt)}
invisible(list(df_ord=df_ord, df_segments=df_segments, plot=plt))
}
将树状图添加到排序图
#'
#“模仿素食主义者的ordicluster函数,该函数将排序对象与聚类密度图叠加。已添加功能以包括治疗组。
#'
#“@param ord是一个排序对象。
#“@param根据与“ord”相同的距离从“hclust”聚集群集对象。”
#“@param treatments为样本指定处理的向量。
#“@param选择要绘制的坐标轴。
#“@param prune从树状图中删除的上层层次结构的数量。如果修剪>0,树状图将断开连接。
#“@param col是群集组成员身份的向量。用于为每个簇组的线段指定颜色。
#“@param绘制一个逻辑图;默认为TRUE。
#'
#“@details”treatments”应该是类因子和长度的向量,等于排序和聚类中包含的样本数量;整数不会强制转换为因子。
#'
#“@作者Jari Oksanen,John Quensen
#'
#“@return以不可见的方式返回用于生成绘图(df_ord、df_段)和绘图本身(plot)的数据帧列表。
#“@出口
#“@进口素食主义者
#“@import-ggplot2
#“@importfromGR2RGB
#“@importFrom stats weighted.mean
#“@importFrom stats weights”
#“@示例
#“数据(沙丘)
#'数据(dune.env)
#'dune.dist@Wietze314看不出这是怎么回事helps@AndréSoares,也许他是在暗示,ggvegan
可以绘制一个聚类,或者它可能有一个fortify
方法。在链接后的文章中,解释了如何使用ggplot2绘制CCA的结果,使用fortify
或autoplot
这并不简单;我目前正在努力让ordicluster
返回一个对象,而不打印该对象,该对象将包含绘制线段所需的信息。然后我可以了解如何使用geom_段
重新创建绘图。如果我能正常工作,我会在以后更新。@Wietze314看不出这是怎么回事helps@AndréSoares,也许他是在暗示,ggvegan
可以绘制一个聚类,或者它可能有一个fortify
方法。在链接后的文章中,解释了如何使用ggplot2绘制CCA的结果,使用fortify
或autoplot
这并不简单;我目前正在努力让ordicluster
返回一个对象,而不打印该对象,该对象将包含绘制线段所需的信息。然后我可以了解如何使用geom_段
重新创建绘图。如果我能正常工作,我会在以后更新。