Hoverinfo in an R plotly barchart - r

I'm plotting a horizontal bar chart using R's plotly package, and including hoverinfo parameter:
df <- data.frame(n = 17:10, name = paste0("n",1:8), hv_text = paste0("t",1:8), stringsAsFactors = F)
df$name <- factor(df$name, levels = rev(df$name))
library(plotly)
plot_ly(marker = list(line = list(width = 1, color = "gray")), x = df$n, y = df$name, type = 'bar', text = df$hv_text, hoverinfo = "xxx", showlegend = F)
Which gives:
So the hv_text gets plotted in addition to appearing when hovered over.
My question is how to get the hoverinfo text not plotted?
I tried using various different values for the hoverinfo parameter (text,x,y) but in all cases the hv_text gets plotted.

There are a lot of ways to do this. One way would be to use hovertext instead of text. For bar charts, if you use text without using hovertext, it will be plotted.
plot_ly(df, marker = list(line = list(width = 1, color = "gray")),
x = ~n, y = ~name, type = 'bar',
hovertext = ~hv_text, hoverinfo = "text", showlegend = F)
Another option would be to use hovertext and hovertemplate.
plot_ly(df, marker = list(line = list(width = 1, color = "gray")),
x = ~n, y = ~name, type = 'bar', hovertext = ~hv_text,
hovertemplate = "Name: %{y}<br>Text: %{hovertext}<extra></extra>",
showlegend = F)

Related

Set dynamic hoverlabel background color in R plotly scattergl

The dynamic hoverlabel background color in R plotly does not seem to work when using scattergl instead of scatter as depicted in the example below.
Works as intended with type = "scatter":
library(plotly)
X <- data.frame(x = 1:6,
y = 1:6,
z = 1:6)
plot_ly(data = X, x = ~x, y = ~y,
type = "scatter", mode = "markers",
marker = list(color = ~x,
colorscale = list(c(0, .5, 1), c('#0d71db', "#dbc00d", "#db220d"))))
The hoverlabel background color becomes black for all data points with type = "scattergl":
plot_ly(data = X, x = ~x, y = ~y,
type = "scattergl", mode = "markers",
marker = list(color = ~x,
colorscale = list(c(0, .5, 1), c('#0d71db', "#dbc00d", "#db220d"))))
I guess a solution could be to pass the same color array used in colorscale to the bgcolor argument via hoverlabel = list(bgcolor = ???). However I have no idea how to do so.
Edit
Tried this based on #Quinten's answer, without success. As can be seen the colors are the default plot_ly colors and do not correspond to what is specified in cols.
library(plotly)
n <- 5000
X <- data.frame(x = sample(1:100, n, replace = TRUE),
y = sample(1:100, n, replace = TRUE),
z = sample(1:500, n, replace = TRUE))
length_unique_vals <- length(unique(X$z))
cols <- colorRampPalette(c('#0d71db', "#dbc00d", "#db220d"))(length_unique_vals)
cols <- cols[factor(X$z)]
plot_ly(data = X, x = ~x, y = ~y,
type = "scattergl", mode = "markers",
marker = list(color = ~z,
colorscale = cols,
colorbar = list(title = "Colorbar")),
hoverlabel = list(bgcolor = cols)) %>%
toWebGL()
You can create a vector of 6 different colors using RColorBrewer. These colors you assign to the color of your marker and to the bgcolor of your hoverlabel which will show the right color. You can use the following code:
library(plotly)
X <- data.frame(x = 1:6,
y = 1:6,
z = 1:6)
library(RColorBrewer)
cols <- brewer.pal(n = 6, name = "Set3")
plot_ly(data = X, x = ~x, y = ~y,
type = "scattergl", mode = "markers",
marker = list(color = cols),
hoverlabel = list(bgcolor = cols))
Output:
As you can see from the plot, the label is the same color as the marker.

Make values to be seen only when hovered

Can we make these values seen only when hovered . Example "Toyota Corolla" to be seen only when hovered. Now all the values are appearing
library(plotly)
data <- mtcars[which(mtcars$am == 1 & mtcars$gear == 4),]
fig <- plot_ly(data, x = ~wt, y = ~mpg, type = 'scatter', mode = 'markers',
marker = list(size = 10))
fig <- fig %>% add_annotations(x = data$wt,
y = data$mpg,
text = rownames(data),
xref = "x",
yref = "y",
showarrow = TRUE,
arrowhead = 4,
arrowsize = .5,
ax = 20,
ay = -40,
# Styling annotations' text:
font = list(color = '#264E86',
family = 'sans serif',
size = 14))
fig
To add the row names/car types so that they appear when you hover over the point, you just need to add text and use it in hoverinfo
In your example, you can use rownames() to get the car types and add them to text which then can be used by hoverinfo
fig <- plot_ly(data, x = ~wt, y = ~mpg, type = 'scatter', mode = 'markers',
marker = list(size = 10), text = rownames(data), hoverinfo = "text")
However, if you didn't have the car names as row names and just as a column in a dataframe, you can just apply them directly to text which then can be used by hoverinfo
#Not required. Just used to create a column with car names#
data$cars <- rownames(data)
You can then use the new column as text
fig <- plot_ly(data, x = ~wt, y = ~mpg, type = 'scatter', mode = 'markers',
marker = list(size = 10), text = data$cars, hoverinfo = "text")
Both methods will give you the following result

Overlaying scatter area in plot_ly

I'd like to do plotly chart and plot filled area shape and bars on one plot. However area shape overlaying bars. I couldn't change order of elements. Is it possible to bring bars in fron?
data <- data.frame(years = c(2005,2006,2007), values1 = c(1,2,3), values2 = c(3,3,2))
plot_ly(data, x = data$years, y=data$values1, type = 'bar') %>%
add_trace(x=data$years, y=data$values2, type = 'scatter', mode = 'lines', fill = 'tozeroy')
This is adapted from the answer by #Maximilian Peters. This code
data <- data.frame(years = c(2005,2006,2007), values1 = c(1,2,3), values2 = c(3,3,2))
plot_ly(data) %>%
add_trace(x=~years, y=~values1, type = 'bar') %>%
add_trace( x = ~years, y=~values2, type = 'scatter', mode = 'lines', fill = 'tozeroy', yaxis='y2'
) %>%
layout(title = 'Trace order Plotly R',
xaxis = list(title = ""),
yaxis = list(side = 'left', title = 'Y - Axis', overlaying = "y2"),
yaxis2 = list(side = 'right', title = "" )
)
generates this output:

Add jitter to box plot using markers in plotly

I made a boxplot:
dat %>%
plot_ly(y = ~xval, color = ~get(col), type = "box",
boxpoints = "all", jitter = 0.7,
pointpos = 0, marker = list(size = 3),
source = shiny_source, key = shiny_key,
hoverinfo = 'text', text = txt)
but problem is that jittered points are not interactive and cannot be marked separately, so I came with an idea to add those points using add_markers:
dat %>%
plot_ly(y = ~xval, color = ~get(col), type = "box",
boxpoints = FALSE, jitter = 0.7,
pointpos = 0, marker = list(size = 3),
source = shiny_source, key = shiny_key,
hoverinfo = 'col', text = txt
) %>%
add_markers(x = ~get(col), y = ~varval, size = I(6))
but now points are in straight line and I'd like to add some jitter (for example by using beeswarm package). But I don't know how to get coordinates of qualitative variable IC0 on X axis. Any ideas?
I find myself in the same potential case often with plotly and ggplot2-- 3 lines of code to get 90% of what I want, and 30 lines of code to get the aesthetics just right.
One potential solution/workaround: Take advantage of R's "factors are coded with integers" paradigm, plot everything on a numeric scale, and then cover your tracks by hiding x labels and x hover values.
dat <- data.frame(xval = sample(100,1000,replace = TRUE),
group = as.factor(sample(c("a","b","c"),1000,replace = TRUE)))
dat %>%
plot_ly() %>%
add_trace(x = ~as.numeric(group),y = ~xval, color = ~group, type = "box",
hoverinfo = 'name+y') %>%
add_markers(x = ~jitter(as.numeric(group)), y = ~xval, color = ~group,
marker = list(size = 6),
hoverinfo = "text",
text = ~paste0("Group: ",group,
"<br>xval: ",xval),
showlegend = FALSE) %>%
layout(legend = list(orientation = "h",
x =0.5, xanchor = "center",
y = 1, yanchor = "bottom"
),
xaxis = list(title = "Group",
showticklabels = FALSE))
Yields the following

Colorbar in legend when using plotly

Here is my data:
set.seed(42)
mydata = data.frame(A = rnorm(20), B = rnorm(20), Index = sample(190:400,20))
I am trying to divide the data into 20 different intervals based on the Index value and then color the scatter points according to their interval value. Below is my code. It is not working perfectly.
cols = colorRampPalette(c("red", "black"), space = "rgb")(20)
mydata$interval = cut(mydata$Index,breaks = 20)
mydata$cols = cols[mydata$interval]
require(plotly)
x = list(title = "A")
y = list(title = "B")
plot_ly(mydata, x = ~A, y = ~B, color = ~cols, type = "scatter",
mode = 'markers', hoverinfo = 'text',
text = ~paste(interval)) %>%
layout(xaxis = x, yaxis = y)
How do I get a colorbar in the legend where the colors are based on Index value.
Are you looking for this:
plot_ly(mydata, x = ~A, y = ~B, type = "scatter",
mode = 'markers', hoverinfo = 'text', colors = colorRampPalette(c("red", "black"), space = "rgb")(20), color = ~Index, text = ~paste(interval), marker = list(size=14)) %>%
layout(xaxis = x, yaxis = y) %>%
colorbar(title = "My Legend")

Resources