I have a quick and simple question which I couldn’t figure out myself.
Is there a way to modify how the legend displays the markers?
At the moment I am using scattergl with thousands of small points per trace…so I had to set the marker size small, which looks nice within the scatter plot but not in the legends where the markers colors are not distinguishable.
Similar thing: I wanted to color my points, using one color for the filling and one for the outline (the outline would indicate the point’s trace and is hence the only color I would like to display in the legend which displays the scatter traces. Does that work?
Hello,
I’m picking up that topic again, as one thing crossed my mind so I was playing around but no success yet.
Note that I am using the scattergl plot type.
I am wondering if there is a way to use the legendgroup behavior to accomplish different marker styles in my legend?
I tried creating my regular traces as scattergl plus additional “fake traces” in “box” type, which look nice in the legend, linked using the same legendgroup.
I thought by doing so, I could somehow use the box layout in the legend, while my scatter cointains the scattergl traces.
However, it always screws up my axes.
That’s another gl2d limitation. You can’t currently mixed gl2d traces with regular cartesian traces (e.g. box trace). So yeah, that probably messed up the axes.
By the way @silly_lily, you might want to subscribe to https://github.com/plotly/plotly.js/pull/1869 for the latest info on our gl2d rewrite. We’re hoping to bring scattergl and gl2d axes to par with cartesian at some point this fall.
Still no way to set a legend marker size in Plotly… imagine a scatter plot with 5000 points…
either giant markers in the plot, or microscopic legend dots… Is there still no simple solution for this? I simple legend(markersize = x) argument sounds so simple to add.
library(plotly)
library(shiny)
library(htmlwidgets)
library(colourpicker)
ui <- fluidPage(
fluidRow(
column(8,
plotlyOutput("plot1")
),
column(2,
colourpicker::colourInput(inputId = 'markercolor', label = 'X',
palette = "limited",
showColour = "background", returnName = TRUE),
selectInput(inputId = 'traceNo', label = 'Trace', choices = c(1:3), selected = 1),
br(),
h5('Switch'),
actionButton(inputId = 'Switch', label = icon('refresh'), style = "color: #f7ad6e; background-color: white; border-color: #f7ad6e;
height: 40px; width: 40px; border-radius: 6px; border-width: 2px; text-align: center; line-height: 50%; padding: 0px; display:block; margin: 2px")
)
)
)
server <- function(input, output, session) {
# values <- reactiveValues()
observeEvent(input$Switch, {
plotlyProxy("plot1", session) %>%
plotlyProxyInvoke("restyle", list(marker = list(color = input$markercolor)), list(as.numeric(input$traceNo)-1))
})
output$plot1 <- renderPlotly({
markersize <- 4
markerlegendsize <- 20
colors <- c('red', 'blue', 'black')
p1 <- plot_ly()
p1 <- add_trace(p1, data = mtcars, x = ~disp, y = ~mpg, type = 'scatter', mode = 'markers', color = ~as.factor(cyl), colors = colors)
p1 <- layout(p1, title = 'mtcars group by cyl with switching colors')
p1 <- plotly_build(p1)
## this is a bit of a hack to change the size of the legend markers to not be equal to the plot marker size.
## it makes a list of 1 size value for each marker in de trace in the plot, and another half of with sizes that are a lot bigger.
## the legend marker size is effectively the average size of all markers of a trace
for(i in seq(1, length(sort(unique(mtcars$cyl) )))) {
length.group <- nrow(mtcars[which(mtcars$cyl == sort(unique(mtcars$cyl))[i]), ])
p1$x$data[[i]]$marker$size <- c(rep(markersize,length.group), rep(c(-markersize+2*markerlegendsize), length.group))
}
p1
})
}
shinyApp(ui, server)