我有按年份和型号的销售数据,通过Sankey chart和plotly(Previous question)进行可视化.然而,我有一些关于hover的问题需要解决.更准确地说,hover每年只显示型号,不显示销售信息:

enter image description here

If we turn the hover to the second mode, it traces everything together but model: F is accidentally shown on the chart for no reason: enter image description here

代码:

df <- data.frame (model  = c("A","B","C","D","E","F","G","H","I","J","A","B","C","D","E","F","G","H","I","J","A","B","C","D","E","F","G","H","I","J","A","B","C","D","E","F","G","H","I","J","A","B","C","D","E","F","G","H","I","J","A","B","C","D","E","F","G","H","I","J"),
 Year = c(2015,2015,2015,2015,2015,2015,2015,2015,2015,2015,2016,2016,2016,2016,2016,2016,2016,2016,2016,2016,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018,2019,2019,2019,2019,2019,2019,2019,2019,2019,2019,2020,2020,2020,2020,2020,2020,2020,2020,2020,2020),
                  sales = c(450,678,456,344,984,456,234,244,655,789,234,567,234,567,232,900,1005,1900,450,345,567,235,456,345,144,333,555,777,111,444,222,223,445,776,331,788,980,1003,456,434,345,2222,3456,456,678,8911,4560,4567,4566,5555,6666,7777,8888,1233,1255,5677,3411,2344,6122,4533))

##################################################################################################

library(ggsankey)
library(tidyverse)
library(plotly)

# df from the question is unchanged

# visualize the original
(plot <- ggplot(df, 
              aes(Year, node = model, fill = model, value = sales)) + 
    geom_sankey_bump(space = 0, type = "alluvial", 
                     color = "transparent", smooth = 15) +
    scale_fill_viridis_d(option = "A", alpha = .8) +
    theme_sankey_bump(base_size = 16)) 

ggplotly(plot) -> plp 
plp

#-------- colors --------
# collect the 10 colors
cols <- map_dfr(1:10, function(k){
  nm <- plp$x$data[[k]]$name
  filler <- plp$x$data[[k]]$fillcolor
  c(nm = nm, filler = filler)
})

##################################################################################################

#--------------- collect values for hovertext positions ----------
x <- plp$x$data[[1]]$x
inds <- which(x %in% 2015:2020, arr.ind = T)
yrs <- x[inds]

tellMe <- invisible(
  map(1:length(plp$x$data),
      function(m) {
        y <- plp$x$data[[m]]$y
        y[inds]
      }) %>% setNames(sort(unique(df$model))) %>% # changed from LETTERS[1:10] 
    as.data.frame() %>% 
    mutate(yr = yrs %>% as.integer()) %>% 
    pivot_longer(names_to = "model", values_to = "sales", 
                 cols = sort(unique(df$model))) %>% 
    distinct() %>% 
    group_by(yr, model) %>% 
    summarise(val = mean(sales)) %>% 
    left_join(df, by = c("yr" = "Year", "model" = "model")) %>% 
    as.data.frame() # drop groups
)

#-------------- create data trace for hovertext --------------
plot_ly(tellMe, x = ~yr, y = ~val, split = ~model, 
        customdata = ~sales, text = ~model,
        line = list(width = .01, shape = "spline", smoothing = 1.3),
        hovertemplate = "Year: %{x}<br>Model: %{text}<br>Sales: %{customdata}<extra></extra>",
        type = "scatter", mode = "lines", showlegend = F) -> pp2
pp2
##################################################################################################

# change colors to match sankey
pp2 <- plotly_build(pp2)
invisible(
  map(1:10,
      function(z) {
        nm <- pp2$x$data[[z]]$name
        # collect and assign the color
        cr <- unlist(cols[cols$nm == nm, "filler"], use.names = F)
        pp2$x$data[[z]]$line$color <<- cr
      })
)

#################################################################################################################################
#-------------- consolidate the traces (subplot won't work) -----------
# collect data one more time!
dx <- plp$x$data
yx <- pp2$x$data
yx <- append(yx, dx) # put plt on top

# replace data 
plp$x$data <- yx

# lines are small, increase the distance searched for matches
plp %>% layout(hoverdistance = 40)

推荐答案

在调用绘图之前,可以使用它删除仅包含"模型…"的悬停数据

如果你这样做,然后进行一系列操作,你会得到一些奇怪的结果,比如有计划地重新添加这些信息.因此,这必须在接近尾声时完成.

invisible(map(
  1:length(plp$x$data),
  function(k){
    x <- length(plp$x$data[[k]]$x)
    if(x > 3000) {
      plp$x$data[[k]]$text <<- NULL
      plp$x$data[[k]]$hoverinfo <<- "none"
    }
  }
))

如果没有其他标签,您可以将悬停响应提高hoverdistance.

plp %>% layout(hoverdistance = 80)

enter image description here enter image description here enter image description here

R相关问答推荐

重复组的运行计数

分组时间连续值

强制相关图以显示相关矩阵图中的尾随零

检测(并替换)字符串中的数学符号

为什么st_join(ob1,ob2,left = True)返回具有比ob1更多功能的sf对象?

使用gcuminc,如何使用逗号格式化风险表?

ggplot的轴标签保存在officer中时被剪切

从外部文件读取多个值作为字符向量

在R函数中使用加号

如何根据R中其他变量的类别汇总值?

将箭头绘制在图形外部,而不是图形内部

随机森林的带Shap值的蜂群图

如何计算R glm probit中的线性预测因子?

我们如何在R中透视数据并在之后添加计算

R中Gamma回归模型均方误差的两种计算方法不一致

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

创建列并对大型数据集中的特定条件进行成对比较的更高效程序

在点图上绘制置信度或预测区间ggplot2

使用ggplot2中的sec_axis()调整次轴

按两个因素将观测值分组后计算单独的百分比