r - 在折线图上显示 x 和 y 坐标
问题描述
我是 R 脚本和编码的新手,只是继承了一些 R 代码,我绘制得非常好并给出了正确的结果。当用户将鼠标光标悬停在线时,我希望能够显示 x 和 y 值。
我找到了一篇文章的链接,该文章描述了如何执行必要的步骤,但完全没有运气......
[url=" https://www.r-graph-gallery.com/124-change-hover-text-in-plotly/ "]
library(dplyr)
library(ggplot2)
library(survival)
library(survminer)
library(grid)
library(gridExtra)
library(plotly)
pc <- dataset
fstat <- pc %>% mutate(fstatus = case_when(OUTCOMETYPE=="Revised" ~ 1,TRUE ~ 0))
pmpa <- fstat %>% select(PRIMARYPROCEDUREID,PRIMARYTOOUTCOMEYEARS,fstatus,OUTCOMETYPE)
if(nrow(pmpa) < 4){
d <- pmpa %>% select(PRIMARYPROCEDUREID,PRIMARYTOOUTCOMEYEARS,OUTCOMETYPE) %>% mutate(INSUFFICIENTDATA = "Summary Results")
h = head(d[,2:4])
grid.table(h)
}else{
fit <- survfit(Surv(PRIMARYTOOUTCOMEYEARS,fstatus)~1,data = pmpa)
ggsurv <- ggsurvplot(fit,
ylab="Patient Analysis",
xlab="Time (Years)",
break.time.by = 1,
xlim = c(0,max(fit$time)),
surv.scale = "percent",
legend.title = "Kaplan-Meier",
legend.labs = "",
risk.table = TRUE,
fontsize = 3,
font.tickslab = c(10, "plain"),
risk.table.y.text = FALSE,
fun = "event"
)
ggsurv$plot <- ggsurv$plot + theme(plot.title = element_text(hjust = 0.5, size=18), panel.grid.major = element_line(colour ="grey90"))
ggsurv$table <- ggsurv$table + theme(plot.title = element_text(hjust = 0.5, size = 10))
ggsurv
只能在折线图上查看胡佛的 x 和 y 坐标的能力。
解决方案
您可以仅在 ggsurv (这是一个 ggplot 对象)的生存“情节”上调用 ggplotly:
ggplotly(ggsurv$plot)
另请注意,您将收到警告:
geom_GeomConfint() 尚未在情节中实现。
See github issue for more details about the lack of confidence intervals: https://github.com/ropensci/plotly/issues/1404
Also - to show both the plot and table together (with the KM curve on top of the table), you can use 'subplot' with plotly. Please let me know if this is helpful.
推荐阅读
- javascript - javascript(ES6):比“for循环”更有效的方法吗?
- ios - 反应原生运行iOS构建失败错误:多个命令produceEntry,“:CFBundleIdentifier”,不存在
- python - 打印到一定数量
- php - Laravel + MySQL - 存储 Eloquent 命名空间
- java - 将新字段添加到 Avro Schema 中出现兼容性异常
- sapui5 - UI5 中的日期格式问题
- python - 使用 ConfigParser 匹配选择器中的文本
- java - 如何在我的 Main.java 文件中对齐 Java FX GridPane 中的矩形?
- javascript - Object.assign 的意外输出
- r - 将变量传递给使用 `enquo()` 的函数