我建立了这个漂亮的geom_point图来标记特定点
我现在正在尝试使所有点(包括粉红色的点)随着ggplotly动态变化。我收到此警告消息:
在geom2trace.default(dots [[1L]] [[1L]],点[[2L]] [[1L]],点[[3L]] [[1L]]):geom_GeomTextRepel()尚未实现在情节上。如果您想查看此几何图形的实现,请打开您的示例代码存在问题https://github.com/ropensci/plotly/issues
交互式图看起来不错,但是有三个问题:1.字幕消失2.来自geom_text_repel的粉红色静态文本标签也消失了3. stat_cor的结果扭曲,它们应显示相关性中的r和p值
是否有解决方法,还是我必须在不使用静态图的静态图或不使用静态标签的交互式图之间进行选择?
我的代码:
p <- df %>%
ggplot(aes(x, y, size = z)) +
geom_point(data=subset(df, a %in% c("a", "b", "c")),
aes(x, y,
text = paste("", a, "\n", #the text label is here for the benefit of ggploty
"x: ", x, "\n", #It is not in the global aes because putting it there removes the entire stat_cor label
"y: ", y, "\n",
"z: ", z, "\n",
sep = "")),
color = "hotpink") +
geom_point(data=subset(df, !(a %in% c("a", "b", "c"))),
aes(x, y,
text = paste("", a, "\n", #the text label is here for the benefit of ggploty
"x: ", x, "\n", #It is not in the global aes because putting it there removes the entire stat_cor label
"y: ", y, "\n",
"z: ", z, "\n",
sep = "")),
color = "steelblue") +
geom_text_repel(data=subset(df, a %in% c("a", "b", "c")),
aes(label=a), color="hotpink", size=5, fontface ="bold", point.padding = TRUE) +
ggtitle("Nice Plot!",
subtitle = "Point Sizes Vary Based On z") +
xlab("x label") + ylab("y label") +
labs(size = "z label") +
theme_minimal() +
theme(plot.title = element_text(face = "bold", color = "darkgrey", hjust = .5, size = 15),
plot.subtitle = element_text(face = "italic", color = "darkgrey", hjust = .5, size = 13),
legend.position = "top",
legend.title = element_text(face = "bold", color = "steelblue", size = 12),
legend.text = element_text(face = "bold", color = "steelblue", size = 12),
axis.text = element_text(face = "bold", color = "darkgrey", hjust = .5, size = 12),
axis.title = element_text(face = "bold", color = "darkgrey", hjust = .5, size = 12)) +
stat_cor(method = "pearson", label.x = 10, label.y = 90, color = "steelblue", size =6)
p #shows subtitle, and labels pink poiints with geom_text_repel
# Three issues:
#1) subtitle dissappears
#2) pink static text labels from geom_text_repel are also gone
#3) the results from stat_cor are warped, they should show the r and pvalues from the correlation
ggplotly(p, tooltip = "text") %>%
config(displayModeBar = F)
[通常,ggrepel
不支持从ggpubr
和plotly
转换结果以及字幕,因此您必须找到替代方法来解决此问题。
这是一种使大部分ggplot对象保持相同,并且仅修改plotly
的一些内容的方法:
静态图:
invisible(lapply(c("ggplot2", "plotly", "dplyr", "ggrepel"),
require, character.only = TRUE))
# mock data
set.seed(1)
df <- data.frame(x=rnorm(26, mean=55, sd=15),
y=rnorm(26, mean = 50, sd=10),
z=rnorm(26, mean=40, sd=30),
a=c(letters[1:3], rep(NA_character_, 23)),
cl=c(rep("a", 3), rep("b", 23)))
p <- df %>%
ggplot(aes(x, y, size = z, colour=cl)) +
scale_colour_manual(values=c("hotpink", "steelblue"))+
# stat_cor(method = "pearson", label.x = 10, label.y = 90, color = "steelblue", size =6)+
geom_point()+
annotate("text", label=paste0("R = ", round(with(df, cor.test(x, y))$estimate, 2),
", p = ", round(with(df, cor.test(x, y))$p.value, 3)), x = min(df$x) + 10, y = max(df$y) + 10, color="steelblue", size=5)+
labs(title = "Nice Plot!",
subtitle = "Point Sizes Vary Based On z",
size = "z label",
x = "x label",
y = "y label") +
theme_minimal() +
theme(plot.title = element_text(face = "bold", color = "darkgrey", hjust = .5, size = 15),
plot.subtitle = element_text(face = "italic", color = "darkgrey", hjust = .5, size = 13),
legend.position = "top",
legend.title = element_text(face = "bold", color = "steelblue", size = 12),
legend.text = element_text(face = "bold", color = "steelblue", size = 12),
axis.text = element_text(face = "bold", color = "darkgrey", hjust = .5, size = 12),
axis.title = element_text(face = "bold", color = "darkgrey", hjust = .5, size = 12))+
guides(size = guide_legend(override.aes = list(colour = "steelblue")),
colour=FALSE)
p + geom_text_repel(aes(label=a), color="hotpink", size=5, fontface ="bold", point.padding = 1)
#> Warning: Removed 23 rows containing missing values (geom_text_repel).
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLmltZ3VyLmNvbS83SmNsRnlHLnBuZyJ9” alt =“”>
[这里,我没有使用ggpubr
统计信息,但建立了plotly
可以处理的注释。我还简化了定义美学的方式。
对于plotly
,您只需修改一些内容,例如标记突出显示的点并添加字幕:
p1 <- p + theme(legend.position="none")
ggplotly(p1, tooltip = c("x", "y", "z")) %>%
config(displayModeBar = F) %>%
layout(title = 'Nice Plot! <br><sub>Point Sizes Vary Based On z</sub>',
font=list(color="#a9a9ac")) %>%
add_annotations(x=subset(p$data, !is.na(a))$x,
y = subset(p$data, !is.na(a))$y,
text = subset(p$data, !is.na(a))$a,
xref = "x",
yref = "y",
showarrow = TRUE,
arrowhead = 4,
arrowsize = .5)
结果: