在不更改标记大小的情况下,使用 plotly proxy 在 plotly 中更改跟踪标记的颜色
Change color of markers of a trace in plotly with plotly proxy without changing the marker size
我正在尝试使用 plotlyproxy
更改 trace
的 color
,效果很好,
但问题是,它也会改变我的标记的大小 / legendmarkers
.
很久以前,我发现(据我目前的研究表明)仍然没有办法将图例标记的大小单独设置为与情节标记不同。
如果你想在散点图中绘制 5000 个点,如果你问我最后得到的是小图例或大图标记,那将是一场灾难。
所以问题是 A 或 B 解决方案类型:
A:想办法在不改变我的legend
marker
size
的情况下使用plotlyproxy
要么
B:找到一种方法来单独调整 legend
的大小,当 plotlyproxy
触发
时不受影响
我欢迎了解此图例大小问题的人提供任何反馈。
注意:可能这可以通过 javascript 完成,但如果是那样的话,我可能需要提供有关我正在开发的实际应用程序的更多信息才能实现工作
这是展示它的虚拟应用程序:
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)
您可以使用 shinyJS 注入自定义 javascript 代码。在这里,我使用一些 d3 来 select 图例项并更改它们的大小。这是非常 hacky 但不幸的是,据我所知,plotly 没有提供内部解决方案。
library(plotly)
library(shiny)
library(htmlwidgets)
library(colourpicker)
library(shinyjs)
jsCode <- "shinyjs.changelegend = function(){
var paths = d3.select('#plot1').
select('.legend').
select('.scrollbox').
selectAll('.traces').
select('.scatterpts')
.attr('d','M8,0A8,8 0 1,1 0,-8A8,8 0 0,1 8,0Z');}"
ui <- fluidPage(
tags$script(src = "https://d3js.org/d3.v4.min.js"),
useShinyjs(),
extendShinyjs(text = jsCode),
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")
)
),
tags$div(id = "test")
)
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))
})
observeEvent(input$Switch,{
js$changelegend()
})
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))
}
return(p1)
})
}
shinyApp(ui, server)
自定义 javascript 代码在 jsCode
中定义,在 extendShinyjs()
中初始化。最后,只要单击按钮,就会在 js$changelegend()
中调用它。
如果您有多个地块并且想要相同的行为,您可以将地块 ID 作为参数传递给 js$changelegend()
并相应地更改 jsCode
来处理这个问题。
我正在尝试使用 plotlyproxy
更改 trace
的 color
,效果很好,
但问题是,它也会改变我的标记的大小 / legendmarkers
.
很久以前,我发现(据我目前的研究表明)仍然没有办法将图例标记的大小单独设置为与情节标记不同。
如果你想在散点图中绘制 5000 个点,如果你问我最后得到的是小图例或大图标记,那将是一场灾难。
所以问题是 A 或 B 解决方案类型:
A:想办法在不改变我的legend
marker
size
的情况下使用plotlyproxy
要么
B:找到一种方法来单独调整 legend
的大小,当 plotlyproxy
触发
我欢迎了解此图例大小问题的人提供任何反馈。
注意:可能这可以通过 javascript 完成,但如果是那样的话,我可能需要提供有关我正在开发的实际应用程序的更多信息才能实现工作
这是展示它的虚拟应用程序:
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)
您可以使用 shinyJS 注入自定义 javascript 代码。在这里,我使用一些 d3 来 select 图例项并更改它们的大小。这是非常 hacky 但不幸的是,据我所知,plotly 没有提供内部解决方案。
library(plotly)
library(shiny)
library(htmlwidgets)
library(colourpicker)
library(shinyjs)
jsCode <- "shinyjs.changelegend = function(){
var paths = d3.select('#plot1').
select('.legend').
select('.scrollbox').
selectAll('.traces').
select('.scatterpts')
.attr('d','M8,0A8,8 0 1,1 0,-8A8,8 0 0,1 8,0Z');}"
ui <- fluidPage(
tags$script(src = "https://d3js.org/d3.v4.min.js"),
useShinyjs(),
extendShinyjs(text = jsCode),
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")
)
),
tags$div(id = "test")
)
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))
})
observeEvent(input$Switch,{
js$changelegend()
})
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))
}
return(p1)
})
}
shinyApp(ui, server)
自定义 javascript 代码在 jsCode
中定义,在 extendShinyjs()
中初始化。最后,只要单击按钮,就会在 js$changelegend()
中调用它。
如果您有多个地块并且想要相同的行为,您可以将地块 ID 作为参数传递给 js$changelegend()
并相应地更改 jsCode
来处理这个问题。