Black Lives Matter. Please consider donating to Black Girls Code today.

Hiding Color Scale / Color Legend

How do you hide the color scale / color legend / color bar that appears to the right on a plot?

I thought one of these would do it, but neither worked:
plot_ly(z = volcano, type = “surface”) %>% layout(showlegend = FALSE)
plot_ly(z = volcano, type = “surface”) %>% layout(showscale = FALSE)

showscale is a trace attribute.

plot_ly(z = volcano, type = “surface”) %>% add_trace(showscale = FALSE)
plot_ly(z = volcano, type = “surface”) %>% add_trace(showlegend = FALSE)

Tried that too without luck.

So close :slight_smile: , this worked for me.

library( plotly )
plot_ly( z=volcano, type="surface", showscale=FALSE )

Lol interestingly enough that works on the minimum example above but not on my actual plots. Here is another example where hiding the legend and scale doesn’t actually hide it. I traced the issue down to having the color parameter. If you add color to a plot with mode=markers it seems plotly doesn’t want to hide the legend/scale/bar.

f1 <- list(
family = “Arial, sans-serif”,
size = 18,
color = “lightgrey”
)
f2 <- list(
family = “Old Standard TT, serif”,
size = 14,
color = “black”
)
a <- list(
title = “AXIS TITLE”,
titlefont = f1,
showticklabels = TRUE,
tickangle = 45,
tickfont = f2,
exponentformat = “e”,
showexponent = “All”
)

s <- seq(0, 8)
plot_ly(x = s, y = s, mode = “markers”, color = s, showlegend = FALSE, showscale = FALSE) %>%
add_trace(y = rev(s)) %>%
layout(xaxis = a, yaxis = a, showlegend = FALSE, showscale = FALSE)

If anyone comes across this issue and wants a work around I came up with a janky solution that works.

  1. Manually create a vector of colors that corresponds with the data. This function will do that:

spectral_color <- function(vector) {
vector <- df$variable
df <- cbind.data.frame(vector, seq=seq(1:length(vector)))
df <- arrange(df, vector)
df$pallete <- colorRampPalette(brewer.pal(11,“Spectral”))(length(vector))
df <- arrange(df, seq)
return(df$pallete)
}

  1. Define the color in the “marker parameter” so it looks like something below:

bubble <- plot_ly(df, x=var1, y=var2, mode = “markers”, marker = list(size = (abs(var3)*40)+8, color=spectral_color(var3)),
hoverinfo = “text”, text=paste(gsub(’(.{1,90})(\s|$)’, ‘\1
’, df$var4)), showlegend=FALSE, showscale=FALSE)

With this you can color the plot without a legend / color scale appearing.

1 Like

In case anybody else comes across this problem (I was having issues turning off colorbars for a surface and a 3D scatter trace at the same time), add hide_colorbar():

plotly() %>%
add_surface(…) %>%
add_trace(…) %>%
hide_colorbar(…) %>%
layout(…)

3 Likes