I have written the following where I have an image in the background (necessary) and some data points:
library(plotly)
library(ggplot2)
library(lorem)
df <- data.frame(
id = 1:50,
value = rnorm(50)
)
# background image (necessary)
image_destination <- "https://i.imgur.com/ojTcMtN.jpeg"
gg <- ggplot(df,
aes(x = id,
y = value,
text = ipsum_words(50, collapse = F)
)
) +
geom_point(color = "red", alpha = 0.5, size = 5) +
theme_bw()
# create plot
pp <- ggplotly(gg,
tooltip = c("text"))
# display plot
plotly::layout(pp,
# tooltip = c("text"),
images = list(
list(
source = image_destination,
xref = "x",
yref = "y",
x = 0,
y = 2,
sizex = 100,
sizey = 4,
sizing = "stretch",
opacity = 1,
layer = "below"
)
))
Is there a way to manipulate plotly so that the size of the geom_point increases on mouse over (for example, increase from the current size of 5 to size 10)?
You can achieve your desired result using two custom event handlers as outlined in the docs. Using the
plotly_hoverevent you can increase the size for the hovered points, usingplotly_unhoveryou can set it back to the defaults. In both cases this achieved usingPlotly.restyle. However, usingPlotly.restylewe can only set or change the marker style for a trace. Hence, to set the marker style for one point requires that each point is treated as a single trace. To this end I mapfactor(id)on thecoloraes inggplot(). As this will add a color legend I addedshowlegend = FALSEinlayoutas settingguide="none"is not accounted for byplotly. Finally, as the units used for sizes differs fromggplot2getting or setting the correct sizes in plotly requires a conversion of theggplot2sizes which can be achieved usingSize_in_mm * 96 / 25.4Note: As the image part is not relevant for the issue, I dropped it to make the example more minimal.