ggplotly:无法在ggplot2中添加pca得分图中的帧

发布于 2025-02-11 17:32:59 字数 1987 浏览 1 评论 0原文

我想使用ggplot2进行PCA分数图,然后使用plotly将图将图转换为交互式图。

我要做的是添加一个框架(使用stat_ellipse,我知道它有效)。

我的问题是,当我尝试将示例名称用作tooltipggplotly中时,帧将消失。我不知道该如何解决。

以下是我的代码,

library(ggplot2)
library(plotly)
library(dplyr)

## Demo data
dat <-  iris[1:4]
Group <- iris$Species

## Calculate PCA
df_pca <- prcomp(dat, center = T, scale. = FALSE)
df_pcs <- data.frame(df_pca$x, Group = Group)
percentage <-round(df_pca$sdev^2 / sum(df_pca$sdev^2) * 100, 2)
percentage <-paste(colnames(df_pcs),"(", paste(as.character(percentage), "%", ")", sep = ""))

## Visualization
Sample_Name <- rownames(df_pcs) 
p <- ggplot(df_pcs, aes(x = PC1, y = PC2, color = Group, label = Sample_Name)) +
  xlab(percentage[1]) +
  ylab(percentage[2]) +
  geom_point(size = 3)

ggplotly(p, tooltip = "label")

直到这里起作用!您可以看到示例名称可以在ggplotly绘图中正确显示。

接下来我尝试添加一个框架,

## add frame 
hull_group <- df_pcs %>%
  dplyr::mutate(Sample_Name = Sample_Name) %>%
  dplyr::group_by(Group) %>%
  dplyr::slice(chull(PC1, PC2))
p2 <- p +
  ggplot2::geom_polygon(data = hull_group, aes(fill = Group), alpha = 0.1)

您可以看到静态图仍然有效!正确添加框架。

但是,当我尝试将其转换为Plotly Interactive图时。框架消失了。

ggplotly(p2, tooltip = "label")

非常感谢您的帮助。

I would like to make a PCA score plot using ggplot2, and then convert the plot into interactive plot using plotly.

What I want to do is to add a frame (not ellipse using stat_ellipse, I know it worked).

My problem is that when I try to use sample name as tooltip in ggplotly, the frame will disappear. I don't know how to fix it.

Below is my code

library(ggplot2)
library(plotly)
library(dplyr)

## Demo data
dat <-  iris[1:4]
Group <- iris$Species

## Calculate PCA
df_pca <- prcomp(dat, center = T, scale. = FALSE)
df_pcs <- data.frame(df_pca$x, Group = Group)
percentage <-round(df_pca$sdev^2 / sum(df_pca$sdev^2) * 100, 2)
percentage <-paste(colnames(df_pcs),"(", paste(as.character(percentage), "%", ")", sep = ""))

## Visualization
Sample_Name <- rownames(df_pcs) 
p <- ggplot(df_pcs, aes(x = PC1, y = PC2, color = Group, label = Sample_Name)) +
  xlab(percentage[1]) +
  ylab(percentage[2]) +
  geom_point(size = 3)

ggplotly(p, tooltip = "label")

Until here it works! You can see that sample names can be properly shown in the ggplotly plot.

enter image description here

Next I tried to add a frame

## add frame 
hull_group <- df_pcs %>%
  dplyr::mutate(Sample_Name = Sample_Name) %>%
  dplyr::group_by(Group) %>%
  dplyr::slice(chull(PC1, PC2))
p2 <- p +
  ggplot2::geom_polygon(data = hull_group, aes(fill = Group), alpha = 0.1)

You can see that the static plot still worked! The frame is properly added.

enter image description here

However, when I tried to convert it to plotly interactive plot. The frame disappeared.

ggplotly(p2, tooltip = "label")

Thanks a lot for your help.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏有森光若流苏 2025-02-18 17:32:59

如果您将data映射ggplot()呼叫 call call geom_point(Geom_point()呼叫:

p2 <- ggplot() +
  geom_point(data = df_pcs, mapping = aes(x = PC1, y = PC2, color = Group, label = Sample_Name), size = 3) +
  ggplot2::geom_polygon(data = hull_group, aes(x = PC1, y = PC2, fill = Group, group = Group), alpha = 0.2)

ggplotly(p2, tooltip = "label")

您可能需要更改geom_pointgeom_polygon的顺序,以确保点在多边形的顶部(这也会影响工具提示位置)。

It works if you move the data and mapping from the ggplot() call to the geom_point() call:

p2 <- ggplot() +
  geom_point(data = df_pcs, mapping = aes(x = PC1, y = PC2, color = Group, label = Sample_Name), size = 3) +
  ggplot2::geom_polygon(data = hull_group, aes(x = PC1, y = PC2, fill = Group, group = Group), alpha = 0.2)

ggplotly(p2, tooltip = "label")

working

You might want to change the order of the geom_point and geom_polygon to make sure that the points are on top of the polygon (this also affects the tooltip location).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文