ggplotly:无法在ggplot2中添加pca得分图中的帧
我想使用ggplot2
进行PCA分数图,然后使用plotly
将图将图转换为交互式图。
我要做的是添加一个框架(使用stat_ellipse
,我知道它有效)。
我的问题是,当我尝试将示例名称用作tooltip
在ggplotly
中时,帧将消失。我不知道该如何解决。
以下是我的代码,
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.
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.
However, when I tried to convert it to plotly interactive plot. The frame disappeared.
ggplotly(p2, tooltip = "label")
Thanks a lot for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您将
data
和映射
从ggplot()
呼叫 call call geom_point(Geom_point()呼叫:您可能需要更改
geom_point
和geom_polygon
的顺序,以确保点在多边形的顶部(这也会影响工具提示位置)。It works if you move the
data
andmapping
from theggplot()
call to thegeom_point()
call:You might want to change the order of the
geom_point
andgeom_polygon
to make sure that the points are on top of the polygon (this also affects the tooltip location).