r - 使用 R 中的 ggplotly() 绘制图中点旁边的标签

标签 r ggplot2 ggplotly

我想知道是否有一种方法可以在 ggplotly() 图中的点旁边放置标签。如果我在ggplot()中使用geom_textgeom_text_repel,结果就可以了。但如果我调用 ggplotly(),我就看不到我创建的标签了。

例如:

require(ggplot)
require(plotly)
x <- c("01/01/2007","04/03/2008","28/11/2008","13/06/2009")
y <- c(25, 50, 75, 100)
x <- as.Date(x, "%d/%m/%Y")
labels <- c("observer1", "observer2", "observer3", "observer4")
x_lab <- "date"
y_lab <- "score"
mydata <- as.data.frame(cbind(x,y))
ggplot(mydata, aes(x=x, y=y)) +
  geom_point() +
  geom_line(col="blue")
ggplotly()

我希望“标签”出现在点旁边,但我找不到方法来做到这一点。

预先感谢您的任何建议。

最佳答案

我知道你知道这个错误不会发生在ggplot2中,它发生在plotly中,因为plotly的功能与 ggplot2 有很大不同。并非 ggplot2 包中的所有函数都可以传递给plotly。 但是,如果您仍然因为 plotly 的附加功能而对其着迷,我强烈建议您使用 plot_ly() 函数来创建绘图,而不是 ggplot() .

我在下面为您提供了一个良好的开端:

require(ggplot)
require(plotly)
x <- c("01/01/2007","04/03/2008","28/11/2008","13/06/2009")
y <- c(25, 50, 75, 100)
x <- as.Date(x, "%d/%m/%Y")
labels <- c("observer1", "observer2", "observer3", "observer4")
x_lab <- "date"
y_lab <- "score"
mydata <- as.data.frame(cbind(x,y))
plot_ly(mydata, x = ~x, y = ~y, type = 'scatter', mode = 'markers',
             marker = list(size = 10)) %>%
  add_annotations(x = mydata$x,
                  y = mydata$y,
                  text = labels)

此图的输出位于此链接中 Graph Output from plot_ly function

关于r - 使用 R 中的 ggplotly() 绘制图中点旁边的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50381374/

相关文章:

r - ggplot错误: cannot coerce class “c(” gg“,”ggplot“)” to a data.frame

r - 如何在 dplyr 工作流中为 ggplot2 的比例编写函数?

r - ggplotly - 仅在某些几何对象上返回工具提示悬停文本

r - 使用极慢的循环在 R 中生成交互式部分依赖图

r - 多个列表上多个变量的统计检验(tibble)

使用 TTR 和 data.table 滚动平均值和 sd 不包括实际值

r - 将气泡颜色指定为变量限制了 ggplot 中的选择

r - ggplotly() 不将日期放在 x 轴上

R DOLS(动态普通最小二乘法)包

r - 使用 scale_x_datetime 时没有前导零