r通过悬停模式以图形方式显示x轴值 [英] R Plotly show x-axis value via hoovermode

查看:30
本文介绍了r通过悬停模式以图形方式显示x轴值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

紧接着question

我希望通过HOVERMODE&QOOT;X&QOOT;看到x值.所以我添加了一个额外的ADD_TRACE,它可以正常工作,但是它用绿色/红色表示,并且希望它用白色

t <- tibble("one" = c(1,2,3,4,5,6,7,8,9),
            "two" = c(-5,6,9,-8,7,-1,5,2,1),
            "three" = c(2,5,6,9,8,7,8,4,5))

# 'bar' objects don't have these attributes: 'mode', 'line'
Plot <- plot_ly(t, x = ~one, y = ~two, type = 'bar', mode = NULL, line = NULL, name = ~ifelse(two < 0, yes = "two < 0", no = "two > 0"),   
                color = ~two < 0, colors = c("#28a745", "#dc3545"),
                yaxis = 'y2') %>%
  
  add_trace(type = "bar", 
            x = ~one, y = 0, 
            hoverinfo='text', 
            text = ~paste("X:", one),
            yaxis = "y", opacity = 0,
            showlegend=F
  ) %>%
  
  add_trace(x = ~one, y = ~three, type = 'scatter',  mode = 'lines',
                name = "three", line = list(color = "#ffc107"), color = NULL, yaxis = "y") %>%
  
  layout(title = "test",
         xaxis = list(titel = "one"),
         yaxis = list(side = 'right', 
                      title = 'three', 
                      showgrid = F, zeroline = F, 
                      showline = T),
         yaxis2 = list(side = 'left', title = 'two', 
                       showgrid = F, zeroline = F,
                       showline = T, overlaying = "y"),
         hovermode = "x")

推荐答案

我们可以通过为新轨迹定义标记颜色marker = list(color = "#ffffff")

来实现这一点
library(plotly)
library(dplyr)

t <- tibble("one" = c(1,2,3,4,5,6,7,8,9),
            "two" = c(-5,6,9,-8,7,-1,5,2,1),
            "three" = c(2,5,6,9,8,7,8,4,5))

# 'bar' objects don't have these attributes: 'mode', 'line'
Plot <- plot_ly(t, x = ~one, y = ~two, type = 'bar', mode = NULL, line = NULL, name = ~ifelse(two < 0, yes = "two < 0", no = "two > 0"),   
                color = ~two < 0, colors = c("#28a745", "#dc3545"),
                yaxis = 'y2') %>%
  
  add_trace(type = "bar", 
            x = ~one, y = 0, 
            hoverinfo='text', 
            text = ~paste("X:", one),
            yaxis = "y", opacity = 0,
            showlegend = FALSE,
            marker = list(color = "#ffffff")
  ) %>%
  
  add_trace(x = ~one, y = ~three, type = 'scatter',  mode = 'lines',
            name = "three", line = list(color = "#ffc107"), color = NULL, yaxis = "y") %>%
  
  layout(title = "test",
         xaxis = list(titel = "one"),
         yaxis = list(side = 'right', 
                      title = 'three', 
                      showgrid = F, zeroline = F, 
                      showline = T),
         yaxis2 = list(side = 'left', title = 'two', 
                       showgrid = F, zeroline = F,
                       showline = T, overlaying = "y"),
         hovermode = "x")

Plot

这篇关于r通过悬停模式以图形方式显示x轴值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆