R Plotly: Smaller markers in bubble plot
By : 김민우
Date : March 29 2020, 07:55 AM
this will help One way to do this sort of thing is to adjust the sizeref (and size) argument in marker: code :
plot_ly(test, x=Group, y=Numbers, mode="markers",
marker = list(size = Days, sizeref = 0.15))
plot_ly(test, x=Group, y=Numbers, mode="markers",
marker = list(size = Days/2, sizeref = 0.1))
plot_ly(test, x=Group, y=Numbers, size = Days, mode="markers",
marker = list(sizeref = 2.5)) # Days data in the hoverinfo with this method
plot_ly(test, x=Group, y=Numbers, mode="markers",
marker = list(size = Days, sizeref = 0.15),
hoverinfo = "text",
text = paste0("(", Group, ", ", Numbers, ")<br>", "Days (size): ", Days))
|
How to plot horizontal lines through markers in plotly?
By : Mohamed Al Arabi
Date : March 29 2020, 07:55 AM
Hope this helps To get this to work we had to replot the original scatter plot with an using the row index to prevent plotly from reordering cars. Then I added back to the axis labels. code :
library(plotly)
##I added the text argument(hover over text) so that I could make sure
##that the yaxis labels matched the points. feel free to delete.
p <- plot_ly(x = mtcars$mpg, y = seq_along(rownames(mtcars)), text=rownames(mtcars),
type = 'scatter', mode = 'markers')
##This sets some attributes for the yaxis. Note: in your origianl plot every other
##car name was shown on the y-axis so that is what I recreated but you could remove
##the "seq(1,32, by=2)" to show all car names.
ax <- list(
title = "",
ticktext = rownames(mtcars)[seq(1,32, by=2)],
tickvals = seq(1,32, by=2)
)
##This is taken from the plotly help page. y0 and y1 now are set to equal the row
##number and x0 and x1 are +/-1 from the car's mpg
line <- list(
type = "line",
line = list(color = "pink"),
xref = "x",
yref = "y"
)
lines <- list()
for (i in seq_along(rownames(mtcars))) {
line[["x0"]] <- mtcars$mpg[i] - 1
line[["x1"]] <- mtcars$mpg[i] + 1
line[c("y0", "y1")] <- i
lines <- c(lines, list(line))
}
p <- layout(p, title = 'Highlighting with Lines', shapes = lines, yaxis=ax)
p
ax2 <- list(
title = "", ticktext = paste0('<span style="text-decoration: underline;">',
rownames(mtcars)[1:32 %% 2 ==0],"</span>"),
tickvals = seq(1,32, by=2), style=list(textDecoration="underline"))
|
Add jitter to grouped box plot using markers in R plotly
By : Bjoroli
Date : March 29 2020, 07:55 AM
Hope this helps You could create a ggplot2 object and then make it interactive using ggplotly() function. code :
library(dplyr)
library(ggplot2)
library(plotly)
dat <- data.frame(xval = sample(100,1000,replace = TRUE),
group1 = as.factor(sample(c("a","b","c"),1000,replace = TRUE)),
group2 = as.factor(sample(c("g1","g2","g3","g4"),1000, replace = TRUE)))
p <- dat %>% ggplot(aes(x=group2, y=xval, fill=group1)) +
geom_boxplot() + geom_jitter() + facet_grid(~group2)
ggplotly(p) %>% layout(boxmode = 'group')
|
Change color of markers of a trace in plotly with plotly proxy without changing the marker size
By : Rod
Date : March 29 2020, 07:55 AM
should help you out You can inject custon javascript code using shinyJS. Here, i use some d3 to select the legend items and change their size. It is very hacky but unfortunatly, as far as i know, plotly does not provide an internal solution. code :
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)
|
How to change markers shape manually in plotly interactive plot
By : D.Scheffers
Date : March 29 2020, 07:55 AM
help you fix your problem I am trying to assign different markers for different colors in plotly interactive plot. By using the following script I can get the interactive plot, can change the size of the markers 'o', can annotate the text. Plotly plot I got is attached. I want to change the markers as in PCA plot attached. , You can set the shape of the markers with the symbol attribute, e.g.: code :
trace1 = Scatter(
x=x1, # x-coordinates of trace
y=y1, # y-coordinates of trace
mode='markers + text', # scatter mode (more in UG section 1)
text = label1, textposition='top center',
marker = dict(size = 15, color = color_3, symbol = 'cross'), # Add symbol here!
textfont=dict(
color='black',
size=18,
family='Times New Roman'
)
|