R语言 绘图悬停文字垂直对齐

1zmg4dgp  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(155)

我正在使用plotlyr。我正在使用hovertext参数。悬停文本出现在我的条形图的顶部,并且与顶部的plotly选项栏冲突,使文本难以阅读。

my_data <-
  structure(
    list(
      flood_freq = "none",
      count = 24L,
      acres = 67276L,
      coiids = "627736, 627737, 627739, 627740, 1153760, 1047375, 1153763, 1153766, 1153765, 1153767, 1122909, 1172713, 1153763, 1153766, 1153765, 1153767, 1122909, 629955, 629956, 629977, 629980, 629982, 629985, 629988"
    ),
    class = c("tbl_df",
              "tbl", "data.frame"),
    row.names = c(NA,-1L)
  )

plot_ly(
  x = my_data$flood_freq,
  y = my_data$count,
  name = "Flood freq. (# components)",
  type = "bar",
  hoverinfo = "text",
  hovertext = paste("coiids: ", stringr::str_wrap(my_data$coiids, 40))
)

我知道如何更改水平对齐方式。R plotly hover label text alignment
如何更改垂直对齐方式或提出其他解决方案来防止冲突?

4ioopgfo

4ioopgfo1#

垂直对齐工具提示

没有什么简单的或内置到Plotly,将帮助您与垂直对齐。有一些创造性的方法,您可以围绕它工作,虽然。
例如,您可以添加另一个跟踪。一个本质上不可见并包含工具提示的跟踪。
例如:

plot_ly(
  x = my_data$flood_freq,
  y = my_data$count,
  name = "Flood freq. (# components)",
  type = "bar",
  hoverinfo = "text",
  hovertext = paste("coiids: ", stringr::str_wrap(my_data$coiids, 40))
) %>% add_markers(x = my_data$flood_freq, y = my_data$count/2,
                  hovertext = paste("coiids: ", stringr::str_wrap(my_data$coiids, 40)),
                  showlegend = F,
                  marker = list(opacity = 0)) %>%  # <--- hides the marker
  layout(hoverdistance = -1)

正如评论中提到的@Stéphane Laurent,您可以修改边距:

plot_ly(
  x = my_data$flood_freq,
  y = my_data$count,
  name = "Flood freq. (# components)",
  type = "bar", 
  hoverinfo = "text",
  hovertext = paste("coiids: ", stringr::str_wrap(my_data$coiids, 40))) %>% 
    layout(margin = list(t = 100))

工具提示中文本水平对齐

工具提示 * 内的文本 * 的水平对齐方式(不是工具提示本身)可以用layout,(layout.hoverlabel.align)来调整。有三个选项:左对齐、右对齐和自动对齐。
以下是它的工作原理:

plot_ly(
  x = my_data$flood_freq,
  y = my_data$count,
  name = "Flood freq. (# components)",
  type = "bar", 
  hoverinfo = "text",
  hovertext = paste("coiids: ", stringr::str_wrap(my_data$coiids, 40))) %>% 
  layout(hoverlabel = list(align = "right"),
         margin = list(t = 100))

相关问题