gpt4 book ai didi

r - ggplot - 带有多个箭头的 geom_segment()

转载 作者:行者123 更新时间:2023-12-01 18:44:42 26 4
gpt4 key购买 nike

我正在研究主成分分析 (PCA)。我发现ggfortify效果很好,但想做一些手动调整。

这里尝试绘制 PCA 结果如下:

evec <- read.table(textConnection("
PC1 PC2 PC3
-0.5708394 -0.6158420 -0.5430295
-0.6210178 -0.1087985 0.7762086
-0.5371026 0.7803214 -0.3203424"
), header = TRUE, row.names = c("M1", "M2", "M3"))

res.ct <- read.table(textConnection("
PC1 PC2 PC3
-1.762697 -1.3404825 -0.3098503
-2.349978 -0.0531175 0.6890453
-1.074205 1.5606429 -0.6406848
2.887080 -0.7272039 -0.3687029
2.299799 0.5601610 0.6301927"
), header = TRUE, row.names = c("A", "B", "C", "D", "E"))

require(ggplot2)
require(dplyr)
gpobj <-
res.ct %>%
ggplot(mapping = aes(x=PC1, y=PC2)) +
geom_point(color="grey30") +
annotate(geom="text", x=res.ct$PC1*1.07, y=res.ct$PC2*1.07,
label=rownames(res.ct))

for (i in 1:nrow(evec))
{
PCx <- evec[i,1]
PCy <- evec[i,2]
axisname <- rownames(evec)[[i]]
gpobj <- gpobj +
geom_segment(
data = evec[i,],
aes(
x = 0, y = 0,
xend = PC1, yend = PC2
# xend = PCx, yend = PCy #not work as intended
),
arrow = arrow(length = unit(4, "mm")),
color = "red"
) +
annotate(
geom = "text",
x = PCx * 1.15, y = PCy * 1.15,
label = axisname,
color = "red"
)
}
gpobj

代码运行良好,但是当我尝试使用注释行 xend = PCx, yend = PCy 时而不是xend = PC1, yend = PC2 ,它没有按照我的预期工作,它没有显示所有箭头。

xend = PC1, yend = PC2效果很好:

<code>xend = PC1, yend = PC2</code> works well

xend = PCx, yend = PCy不:

<code>xend = PCx, yend = PCy</code> does not

问题:为什么不geom_segment()当起点和终点由环境变量指定而不是由 data = 中的变量名称引用时,保持前一个箭头?

最佳答案

在您使用的代码中,当在美学映射aes(...)(而不是将它们硬编码为 aes(...) 之外的固定美学值,就像对 annotate 层所做的那样),仅在绘制/打印 ggplot 时才评估实际值对象gpobj

这意味着 PCx/PCy 的值是在 for 循环外部计算的。此时,它们对应于它们所采用的最后一个值,即 i = 3,这就是为什么只有一个箭头段(实际上是三个箭头彼此重叠)可见。将 xend = PCx, Yend = PCy 移到 aes(...) 之外应该可以实现您想要的外观。

不过,我确实想知道为什么您首先选择使用 for 循环。像下面这样的东西难道不会达到同样的目的吗?

# convert row names to explicit columns
res.ct <- tibble::rownames_to_column(res.ct)
evec <- tibble::rownames_to_column(evec)

# plot
res.ct %>%
ggplot(mapping = aes(x=PC1, y=PC2)) +
geom_point(color="grey30") +
geom_text(aes(x = PC1 * 1.07, y = PC2 * 1.07,
label = rowname)) +
geom_segment(data = evec,
aes(x = 0, y = 0, xend = PC1, yend = PC2, group = rowname),
arrow = arrow(length = unit(4, "mm")),
color = "red") +
geom_text(data = evec,
aes(x = PC1 * 1.15, y = PC2 * 1.15, label = rowname),
colour = "red")

plot

关于r - ggplot - 带有多个箭头的 geom_segment(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59331292/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com