0

I've created a (complicated/tricked) graph using plot_ly for R and I've mostly got it to look the way I wanted (see previous question here).

However, one last thing that's bugging me is that in order to force space between some of the categories I've had to trick the data adding observations equal to zero, and when you hover over them obviously it doesn't look very nice (see below).

So, my question is, is there a way to omit hoverinfo for those specific data points or values (not whole traces)?

Or perhaps a more elegant way to create additional space between specific categories that does not involve "tricking" the data like I mentioned?

Thank you very much in advance.

enter image description here

EDIT:

Here's the data and code

structure(list(Codigo = c("0", "8.1", "8.0", "8.4", "8.5", "8.6999999999999993", 
"8.8000000000000007", "8.1999999999999993", "8.6", "8.3000000000000007", 
"8.9", "0", "7.0", "7.2", "7.1", "7.3", "0", "6.8", "6.4", "6.5", 
"6.3", "6.6", "6.13", "6.7", "6.2", "6.11", "6.1", "6.9", "6.0", 
"6.12", "0", "5.7", "5.6", "5.5", "5.0", "5.2", "5.0999999999999996", 
"0", "4.7", "4.0", "4.3", "4.9000000000000004", "4.2", "4.0999999999999996", 
"4.5", "4.8", "4.5999999999999996", "4.4000000000000004", "0", 
"3.3", "3.2", "3.4", "3.6", "3.5", "3.0", "3.1", "0", "2.5", 
"2.6", "2.7", "2.8", "2.2000000000000002", "2.1", "2.9", "2.2999999999999998", 
"2.4", "2.0", "0", "1.1000000000000001", "1.1100000000000001", 
"1.9", "1.1200000000000001", "1.6", "1.8", "1.2", "1.5", "1.4", 
"1.3", "1.1399999999999999", "1.1499999999999999", "1.0", "1.1299999999999999", 
"1.7"), nombre_comp = c("        ", "G8 Operar maquinaria móvil", 
"G8 Trabajar con maquinaria y equipo especializado (otros)", 
"G8 Operar maquinaria para la extracción y la transformación de materias primas", 
"G8 Operar maquinaria para la fabricación de productos", "G8 Instalar, mantener y reparar equipos mecánicos", 
"G8 Instalar, mantener y reparar equipos eléctricos, electrónicos y de precisión", 
"G8 Conducir vehículos", "G8 Utilizar instrumentos y equipos de precisión", 
"G8 Operar vehículos acuáticos", "G8 Operar aeronaves", "       ", 
"G7 Construir (otros)", "G7 Instalar infraestructura interior o exterior", 
"G7 Construir y repara estructuras", "G7 Rematar estructuras interiores o exteriores", 
"      ", "G6 Lavar y mantener textiles y prendas de vestir", 
"G6 Utilizar herramientas manuales", "G6 Colocar materiales, herramientas o equipos", 
"G6 Elaborar moldes, formas, modelos y patrones", "G6 Manipular animales", 
"G6 Ocuparse de plantas y cultivos", "G6 Limpiar", "G6 Ensamblar y fabricar productos", 
"G6 Mover y elevar", "G6 Clasificar y envasar, embalar y empaquetar mercancías y materiales", 
"G6 Manipular y eliminar residuos y materiales peligrosos", "G6 Manipular y mover (otros)", 
"G6 Transformar y mezclar materiales", "     ", "G5 Utilizar herramientas digitales para controlar maquinaria", 
"G5 Utilizar herramientas digitales para la colaboración, creación y solución.", 
"G5 Acceder a y analizar datos digitales", "G5 Trabajar con ordenadores (otros)", 
"G5 Configurar y proteger sistemas informáticos", "G5 Programar sistemas informáticos", 
"    ", "G4 Reclutar y contratar", "G4 Capacidades de gestión (otros)", 
"G4 Asignar y controlar recursos", "G4 Tomar decisiones", "G4 Organizar, planificar y programar trabajo y actividades", 
"G4 Desarrollar objetivos y estrategias", "G4 Guiar y motivar", 
"G4 Supervisar a personas", "G4 Crear y desarrollar equipos", 
"G4 Realizar tareas administrativas", "   ", "G3 Proteger y velar por el cumplimiento", 
"G3 Prestar atención sanitaria o administrar tratamientos médicos", 
"G3 Facilitar información y apoyo al público y a los clientes", 
"G3 Prestar cuidados personales en general", "G3 Preparar y servir alimentos y bebidas", 
"G3 Prestar asistencia y cuidados (otros)", "G3 Ofrecer orientación", 
"  ", "G2 Medir propiedades físicas", "G2 Calcular y estimar", 
"G2 Analizar y evaluar información y datos", "G2 Realizar seguimiento, inspección y ensayos", 
"G2 Documentar y registrar información", "G2 Realizar estudios, investigaciones y análisis", 
"G2 Realizar seguimiento de avances en el ámbito de especialización", 
"G2 Gestionar información", "G2 Procesar información", "G2 Competencias en materia de información (otros)", 
" ", "G1 Negociar", "G1 Diseñar sistemas y productos", "G1 Solucionar problemas", 
"G1 Crear materiales artísticos, visuales o docentes", "G1 Promocionar, vender y comprar", 
"G1 Trabajar con otros", "G1 Actuar como enlace y crear redes de contactos", 
"G1 Prestar asesoramiento y consultoría", "G1 Presentar información", 
"G1 Enseñar y formar", "G1 Actuar y entretener", "G1 Utilizar más de una lengua", 
"G1 Comunicación, colaboración y creatividad (otros)", "G1 Escribir y redactar", 
"G1 Obtener información verbal"), T2019 = c(0, 26.47371, 36.66604, 
29.20799, 43.04965, 45.09934, 44.24722, 18.18302, 59.93101, 9.35563, 
3.07369, 0, 5.2988, 17.45539, 23.65072, 24.15063, 0, 27.3173, 
8.35577, 37.09522, 48.45148, 20.19712, 50.7987, 50.24694, 61.98523, 
68.43102, 62.87703, 25.58438, 3.66292, 12.68603, 0, 30.61711, 
46.98768, 65.6458, 39.96351, 27.7681, 11.36268, 0, 23.83861, 
48.11857, 73.9123, 52.43245, 86.94399, 83.90047, 64.5736, 59.47582, 
21.47687, 58.98102, 0, 98.53384, 27.17974, 73.41871, 29.5992, 
21.76646, 0.43687, 18.54575, 0, 46.81006, 62.52046, 83.9231, 
91.76589, 89.21012, 77.35894, 50.25313, 37.16182, 60.41529, 13.56145, 
0, 64.98231, 33.31231, 66.09042, 57.54572, 67.9786, 88.31099, 
84.01538, 82.99108, 74.18625, 59.37381, 28.41254, 19.80823, 33.81715, 
53.65351, 58.51055), T1997 = c(0, 0.3520542, 0.4552646, 0.3472373, 
0.5010785, 0.52242, 0.4662731, 0.1855482, 0.5874527, 0.0836924, 
0.0262412, 0, 0.0816599, 0.2313844, 0.3010284, 0.2803679, 0, 
0.3638317, 0.1091822, 0.4281436, 0.5551934, 0.2291073, 0.5689277, 
0.5519484, 0.6738303, 0.7256235, 0.6522561, 0.257638, 0.034869, 
0.1189976, 0, 0.3190127, 0.452448, 0.595845, 0.3605095, 0.2175164, 
0.0829162, 0, 0.2591114, 0.5031723, 0.7554225, 0.524622, 0.8575505, 
0.8238186, 0.6268805, 0.5557029, 0.191295, 0.4984334, 0, 0.9897875, 
0.2685441, 0.6753148, 0.2629773, 0.1923543, 0.0036891, 0.1359106, 
0, 0.5287208, 0.6372898, 0.8346352, 0.9047318, 0.8791534, 0.7353494, 
0.4525746, 0.3215138, 0.506457, 0.0937403, 0, 0.6530643, 0.3329005, 
0.6553841, 0.5670767, 0.6519465, 0.8422538, 0.8012689, 0.7850766, 
0.6790522, 0.5420047, 0.2526883, 0.175611, 0.2938071, 0.4636324, 
0.4772799), CambioRel = c(0, -24.8021753468642, -19.4621325708171, 
-15.8846414253308, -14.0860164624904, -13.6722560392022, -5.10449777179941, 
-2.00379200660529, 2.01844335722687, 11.7858969273196, 17.1322195631297, 
0, -35.1113582064146, -24.5610767190874, -21.4335923122204, -13.8609305844214, 
0, -24.9177573037204, -23.4694849526754, -13.3579948409833, -12.7304467236102, 
-11.8442755861555, -10.7115016547797, -8.9644249353744, -8.01062225904652, 
-5.69348980566368, -3.60070223950378, -0.696403480852967, 5.04803693825462, 
6.60744418374825, 0, -4.0254196776492, 3.85211118183746, 10.1726120047999, 
10.852862407232, 27.6597994450074, 37.0381180034782, 0, -7.99860600498473, 
-4.36959665704969, -2.1576667361642, -0.0567074960638444, 1.38643730019399, 
1.84337911282898, 3.00783004097273, 7.02809000996756, 12.2709427847043, 
18.3328003299939, 0, -0.449500524102405, 1.21145837871695, 8.71775651888571, 
12.5542014462845, 13.1581669866491, 18.4218373044916, 36.4555082532194, 
0, -11.4654464133055, -1.89634291965759, 0.550635774767219, 1.42883227935617, 
1.47275776900824, 5.20024902447734, 11.0383348955067, 15.5839033969926, 
19.2900680610595, 44.6704352343656, 0, -0.496306412706996, 0.0668668265743119, 
0.842269441690762, 1.47784241532054, 4.27021235638201, 4.85080625341197, 
4.8529151699261, 5.71080579907743, 9.24970127480626, 9.54482498029999, 
12.4410588064426, 12.7960663056414, 15.0998393163406, 15.7242461915949, 
22.5916909553493), codigo_comp_1d = c("0", "8", "8", "8", "8", 
"8", "8", "8", "8", "8", "8", "0", "7", "7", "7", "7", "0", "6", 
"6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "6", "0", 
"5", "5", "5", "5", "5", "5", "0", "4", "4", "4", "4", "4", "4", 
"4", "4", "4", "4", "0", "3", "3", "3", "3", "3", "3", "3", "0", 
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "0", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1"
), color = c("white", "#a5e1ad", "#a5e1ad", "#a5e1ad", "#a5e1ad", 
"#a5e1ad", "#a5e1ad", "#a5e1ad", "#a5e1ad", "#a5e1ad", "#a5e1ad", 
"white", "#b1cd88", "#b1cd88", "#b1cd88", "#b1cd88", "white", 
"#bfb66a", "#bfb66a", "#bfb66a", "#bfb66a", "#bfb66a", "#bfb66a", 
"#bfb66a", "#bfb66a", "#bfb66a", "#bfb66a", "#bfb66a", "#bfb66a", 
"#bfb66a", "white", "#ca9d58", "#ca9d58", "#ca9d58", "#ca9d58", 
"#ca9d58", "#ca9d58", "white", "#d28354", "#d28354", "#d28354", 
"#d28354", "#d28354", "#d28354", "#d28354", "#d28354", "#d28354", 
"#d28354", "white", "#d2685c", "#d2685c", "#d2685c", "#d2685c", 
"#d2685c", "#d2685c", "#d2685c", "white", "#c8506c", "#c8506c", 
"#c8506c", "#c8506c", "#c8506c", "#c8506c", "#c8506c", "#c8506c", 
"#c8506c", "#c8506c", "white", "#b24080", "#b24080", "#b24080", 
"#b24080", "#b24080", "#b24080", "#b24080", "#b24080", "#b24080", 
"#b24080", "#b24080", "#b24080", "#b24080", "#b24080", "#b24080"
), CambioRelT = c(0, -54.7085239814223, -42.9294822566757, -35.0382687889461, 
-31.0708701419732, -30.1581993087315, -11.2594776408156, -4.41995516578783, 
4.45227304741035, 25.9972770804701, 37.7901708909166, 0, -77.448471982471, 
-54.1767097403265, -47.2781190610118, -30.574376750325, 0, -54.9634741287401, 
-51.768881656883, -29.465003407213, -28.0807606644432, -26.126048456862, 
-23.6273809438917, -19.7736871745243, -17.6697936304434, -12.5586735524195, 
-7.94241239185333, -1.53612358594213, 11.134936594605, 14.5746699040334, 
0, -8.87925215792832, 8.49696907724284, 22.4387006395656, 23.9391938396322, 
61.0118580364701, 81.6985098557586, 0, -17.6432882326582, -9.63843615159223, 
-4.75937133451002, -0.125085133257373, 3.05819700190093, 4.06611714459182, 
6.63465763092445, 15.5025285273018, 27.0671889954415, 40.4384064088518, 
0, -0.991506182768259, 2.67222930398767, 19.2295871191782, 27.69199964464, 
29.0242240478179, 40.6348037565206, 80.4133919558936, 0, -25.2904288147185, 
-4.18294446540168, 1.21458985220384, 3.15171165139232, 3.24860229386291, 
11.4706853123251, 24.3483081987868, 34.3749022331757, 42.5499431546906, 
98.53384, 0, -1.09475039596264, 0.147494537638, 1.85787404955929, 
3.25981798324593, 9.41921471958417, 10.699886059695, 10.7045378980146, 
12.5968690908225, 20.4029484082214, 21.0539309165574, 27.4424301315409, 
28.225504035821, 33.3071559167928, 34.6844697221859, 49.8326477063582
), Espacio = c(0, 91.766898, 81.574568, 89.032618, 75.190958, 
73.141268, 73.993388, 100.057588, 58.309598, 108.884978, 115.166918, 
0, 112.941808, 100.785218, 94.589888, 94.089978, 0, 90.923308, 
109.884838, 81.145388, 69.789128, 98.043488, 67.441908, 67.993668, 
56.255378, 49.809588, 55.363578, 92.656228, 114.577688, 105.554578, 
0, 87.623498, 71.252928, 52.594808, 78.277098, 90.472508, 106.877928, 
0, 94.401998, 70.122038, 44.328308, 65.808158, 31.296618, 34.340138, 
53.667008, 58.764788, 96.763738, 59.259588, 0, 19.706768, 91.060868, 
44.821898, 88.641408, 96.474148, 117.803738, 99.694858, 0, 71.430548, 
55.720148, 34.317508, 26.474718, 29.030488, 40.881668, 67.987478, 
81.078788, 57.825318, 104.679158, 0, 53.258298, 84.928298, 52.150188, 
60.694888, 50.262008, 29.929618, 34.225228, 35.249528, 44.054358, 
58.866798, 89.828068, 98.432378, 84.423458, 64.587098, 59.730058
), color_lab = c("white", "black", "black", "black", "black", 
"black", "black", "black", "black", "black", "black", "white", 
"black", "black", "black", "black", "white", "black", "black", 
"black", "black", "black", "black", "black", "black", "black", 
"black", "black", "black", "black", "white", "black", "black", 
"black", "black", "black", "black", "white", "black", "black", 
"black", "black", "black", "black", "black", "black", "black", 
"black", "white", "black", "black", "black", "black", "black", 
"black", "black", "white", "black", "black", "black", "black", 
"black", "black", "black", "black", "black", "black", "white", 
"black", "black", "black", "black", "black", "black", "black", 
"black", "black", "black", "black", "black", "black", "black", 
"black"), EjeX = 1:83, custom_hover_t = c("skip", "%{x:.2f}%", 
"%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%", 
"%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%", "skip", "%{x:.2f}%", 
"%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%", "skip", "%{x:.2f}%", "%{x:.2f}%", 
"%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%", 
"%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%", 
"%{x:.2f}%", "skip", "%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%", 
"%{x:.2f}%", "%{x:.2f}%", "skip", "%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%", 
"%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%", 
"%{x:.2f}%", "%{x:.2f}%", "skip", "%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%", 
"%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%", "skip", "%{x:.2f}%", 
"%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%", 
"%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%", "skip", "%{x:.2f}%", 
"%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%", 
"%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%", 
"%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%", "%{x:.2f}%")), row.names = c(NA, 
-83L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), groups = structure(list(
    codigo_comp_1d = c("0", "1", "2", "3", "4", "5", "6", "7", 
    "8"), .rows = structure(list(c(1L, 12L, 17L, 31L, 38L, 49L, 
    57L, 68L), 69:83, 58:67, 50:56, 39:48, 32:37, 18:30, 13:16, 
        2:11), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -9L), .drop = TRUE))


custom_hover_t <- "%{x:.2f}%"
custom_hover_c <- "%{text:.2f}%"

plot_ly(height = 800)  %>%
  add_trace(type = 'bar',
            orientation = 'h',
            y = datos$nombre_comp,
            x = datos$T2019,
            name = 'Tamaño',
            marker = list(color = ~datos$color, 
                          width = 0.04),
            hovertemplate = custom_hover_t
  ) %>%
  add_trace(type = 'bar',
            orientation = 'h',
            y = datos$nombre_comp,
            x = datos$Espacio,
            name = '',
            marker = list(color = 'white'),
            hoverinfo = "skip"
  ) %>%
  add_trace(type = 'bar',
            orientation = 'h',
            y = datos$nombre_comp,
            x = datos$CambioRelT,
            name = 'Cambio',
            marker = list(color = datos$color, 
                          width = 0.04),
            hovertemplate = custom_hover_c,
            text = datos$CambioRel
  ) %>%
  add_annotations(x = 0, y = nrow(datos)+2, xanchor = "left", "Tamaño en 2019", showarrow = F,
                  font = list(family = "Raleway", size = 18))  %>%
  add_annotations(x = max(datos$T2019)*6/5, y = nrow(datos)+2, xanchor = "left", "Cambio desde 1997", showarrow = F,
                  font = list(family = "Raleway", size = 18)) %>%
  plotly::layout(
    barmode = "stack",
    title = "",
    xaxis = list(title     = NULL,
                 autorange = T,
                 zeroline  = F,
                 showline  = F,
                 autotick  = FALSE,
                 tickmode  = "array",
                 showgrid  = FALSE,
                 showticklabels = F,
                 titlefont = list(color="transparent")
    ),
    yaxis = list(title     = NULL,
                 visible   = FALSE,
                 autorange = TRUE,
                 visible   = FALSE,
                 zeroline  = FALSE,
                 showline  = F,
                 showgrid  = FALSE,
                 ticklen = 0,
                 type = "category",
                 categoryorder = "trace",
                 titlefont = list(color="transparent")
    ), #para mostrar solo 2 decimales al hacer hover en un punto
    showlegend = F
  ) %>%
  layout(hovermode = "y unified",
         hoverlabel = list(bgcolor='rgba(241,241,241,0.75)', font=list(color='black',family = "Raleway", size = 13), bordercolor = "white")) %>%
  config(displayModeBar = F)

Ainhoa
  • 191
  • 7
  • I think that this [Q&A](https://stackoverflow.com/questions/68587628/r-plotly-hovertemplate-with-conditional-content) may help! – Kat Dec 03 '21 at 16:02
  • I think there is a way however I would need to be able to see the code you use for your plot and the data you're working with. You can share the data using `dput()` and copy and paste the output into your question – neuron Dec 03 '21 at 18:24
  • @Kat I tried and got it to work but I'm not sure it's meant to do what I want it to do, I'd like to have a hover template for some data points and hoverinfo="skip" for some others, basically. – Ainhoa Dec 03 '21 at 20:35
  • @Ainhoa the `hoverinfo` attribute is defined per trace. Accordingly the only thing you can do is to split the traces. – ismirsehregal Dec 17 '21 at 09:13

0 Answers0