是否可以将轨迹值放置在悬停时显示在绘图角落中的Ploly/R(仅限y值)中,或放置在绘图上的固定位置(如红色突出显示的位置).

x <- c(1:100)
random_y <- rnorm(100, mean = 0)
data <- data.frame(x, random_y)

plot_ly(data, x = ~x, y = ~random_y, type = 'scatter', mode = 'lines',
               line = list(color = "#000"))

and then apply styling to that value, say larger font enter image description here

推荐答案

我们可以用一个小的shiny 的应用程序来实现这样的行为:

library(shiny)
library(plotly)

ui <- fluidPage(
  plotlyOutput("plot")
)

server <- function(input, output, session) {
  
  output$plot <- renderPlotly({
    x <- c(1:100)
    random_y <- rnorm(100, mean = 0)
    data <- data.frame(x, random_y)
    
    p <- plot_ly(data, x = ~x, y = ~random_y, type = 'scatter', mode = 'lines',
                 line = list(color = "#000")) %>%
      layout(annotations = list(
        x = 1, y = 2,
        text = "", # Initialize with empty text
        showarrow = FALSE,
        xanchor = 'left',
        yanchor = 'bottom',
        font = list(
          family = "Arial, sans-serif",
          size = 16,      
          color = "red" 
        )
      ))
    p
  })
  
  observeEvent(event_data("plotly_hover"), {
    hover_data <- event_data("plotly_hover")
    plotlyProxy("plot", session) %>%
      plotlyProxyInvoke("relayout", list(
        annotations = list(
          list(
            x = 1, y = 2,
            text = paste("Y-value:", hover_data$y),
            showarrow = FALSE,
            xanchor = 'left',
            yanchor = 'bottom',
            font = list(
              family = "Arial, sans-serif",
              size = 20,      
              color = "red"   
            )
          )
        )
      ))
  })
}

shinyApp(ui, server)

enter image description here

R相关问答推荐

修改dDeliverr中列表列的最后一个元素

使用map()内的公式()创建多个公式

具有多个依赖变量/LHS的逻辑模型

更改编号列表的 colored颜色

手动打印线型gplot

从R导出全局环境中的所有sf(numrames)对象

使用ggsankey调整Sankey图中单个 node 上的标签

在嵌套列表中查找元素路径的最佳方法

为了网络分析目的,将数据框转换为长格式列联表

绘制采样开始和采样结束之间的事件

安全地测试文件是否通过R打开

有没有可能用shiny 的书签恢复手风琴面板?

扩展R中包含列表的数据框

如何将一些单元格的内容随机 Select 到一个数据框中?

将数据集旋转到长格式,用于遵循特定名称模式的所有变量对

Geom_arcbar()中出错:找不到函数";geom_arcbar";

如何使用字符串从重复的模式中提取多个数字?

我是否可以使用多个变异项来构建顺序列(标记多个问题)

通过初始的shiny 应用更新部署的shiny 应用的数据和参数,其中部署的应用程序显示为URL

变异以按组从其他列创建具有最大和最小值的新列