plotly / plotly.R

An interactive graphing library for R
https://plotly-r.com
Other
2.53k stars 623 forks source link

replicate ggplot2 text legend #2122

Open ThierryO opened 2 years ago

ThierryO commented 2 years ago

I'd like to create a legend having text over a point as in the ggplot2 example below. How can I replicate this with plotly. Doesn't need to be a nice and clean ggplotly() solution.

library(ggplot2)
library(plotly)
ds <- data.frame(text = c("+", "-"))
p <- ggplot(ds, aes(x = text, y = text, label = text)) +
  geom_point(aes(colour = text), size = 6) +
  geom_text(show.legend = TRUE) +
  guides(colour = guide_legend(override.aes = list(label = c("-", "+")))) +
  scale_colour_discrete(labels = c("minus", "plus"))
p
ggplotly(p)

ggplot2 version

ggplot2 version

ggplotly version

ggplotly version

danttis commented 2 years ago

a solution would be to put create a name vector and put names <-c("minus", "plus"), and pass in the geom_point(aes(colour = text, shape=names) argument, but I'm not sure if that's what you want.