数据帧:

df2 = data.frame(value = c(10,10,10,10,10),
                 key = c('ar', 'or', 'br', 'gt', 'ko'))

情节代码:

df2 %>% 
  plot_ly(y = ~value,
          x = ~key, 
          type = 'bar',
          hoverinfo = 'text',
          hovertext = paste0('this is a pretty huge text',
                             '\nand I would like to set the correct',
                             '\nposition of the hoverlabel, so it',
                             "\nwon't cover the title.",
                             '\n\nAlso, I would like to position it in the',
                             '\nmiddle of the bar, if possible')) %>% 
  layout(title = list(text = "This is the Title I don't want to be covered",
                      y = 0.98))

基本上,我有一个相当于huge hoverinfo的悬停标签,但它是covering the title.我想把它放在middle of the yaxis,这样我就可以继续阅读标题和悬停信息.有什么建议吗?

enter image description here

推荐答案

这更像是一个技巧,而不是我所谓的"正确"方法...

我做的是添加transparent个标记,其y为零,但x与条形图相同.在最后两个步骤中,我设置了悬停模式x unified并隐藏了图例.我的悬停框现在在横栏的中间.

df2 = data.frame(value = c(10,10,10,10,10),
                 value2 = c(0, 0, 0, 0, 0), # <-  I added this
                 key = c('ar', 'or', 'br', 'gt', 'ko'))

df2 %>% 
  plot_ly(y = ~value,
          x = ~key, 
          type = 'bar',
          #hoverinfo = 'text',                                # not needed
          hovertext = paste0('this is a pretty huge text',
                             '\nand I would like to set the correct',
                             '\nposition of the hoverlabel, so it',
                             "\nwon't cover the title.",
                             '\n\nAlso, I would like to position it in the',
                             '\nmiddle of the bar, if possible'),
          hovertemplate = '%{hovertext}<extra></extra>') %>%  # extra = hide trace name
  add_markers(y = ~value2, x = ~key, color = I("transparent"), 
              hovertemplate = '<extra></extra>') %>%          # trace invisible
  layout(title = list(text = "This is the Title I don't want to be covered",
                      y = 0.98),
         # only need hoverlabel if you want original color scheme back
         hoverlabel = list(bgcolor = '#1f77b4',
                           font = list(color = "white")), 
         hovermode = "x unified",                     # move hover box to bar center             
         showlegend = F)                              # no need to see trace info

如果你对 colored颜色 感到好奇,下面是plot_ly种默认 colored颜色 列表.https://community.plotly.com/t/plotly-colours-list/11730/2

enter image description here

enter image description here

R相关问答推荐

根据固定值范围在tible中添加新行

在数据表中呈现数学符号

在通过最大似然估计将ODE模型与数据匹配时,为什么要匹配实际参数的转换值?

编码变量a、b、c以匹配来自另一个数据点的变量x

如何修复R码的置换部分?

R Highcharts与两个位置关联的注释

如何编辑ggplot的图例字使用自定义对象(gtable)?'

R Sapply函数产生的值似乎与for循环方法略有不同

使用data.table::fcase()而不是dplyr::case_When()时保持值

在ggplot2的框图中绘制所有级别的系数

仅 Select 超过9行的CSV文件

如何指定我的函数应该查找哪个引用表?

如何在PackageStatus()中列出&q;不可用的包&q;?

列名具有特殊字符时的循环回归

来自程序包AFEX和amp;的类/函数和NICE_TABLE&冲突

在gggraph中显示来自不同数据帧的单个值

GOGPLATE geom_boxploy色彩疯狂

策略表单连接两个非常大的箭头数据集,而不会 destruct 内存使用

向内存不足的数据帧添加唯一行

使用循环改进功能( struct 简单)