Vertical line appearing when manually adjust x-axis range using plotly - r

I am creating a chart using plotly and would like to extend the x-axis to 0. However, for some reason, when I do this, plotly renders a vertical reference line at 0, as shown below. Is there a way to omit this line and have the vertical line at 0 simply appear gray line the other grid lines?
library(dplyr)
library(plotly)
mtcars %>%
mutate(cyl = factor(cyl)) %>%
plot_ly(
x = ~mpg,
y = ~cyl,
type = "scatter",
mode = "markers"
) %>%
layout(xaxis = list(
range = c(0, 35)
))

By default plotly will automatically draw a zeroline. That could be toggled on and off via layout argument zeroline. If you want a zeroline you could set the color to be equal to the color of the gridlines via zerolinecolor="#eee", where #eee is the default grid line color. See the docs.
library(dplyr)
library(plotly)
mtcars %>%
mutate(cyl = factor(cyl)) %>%
plot_ly(
x = ~mpg,
y = ~cyl,
type = "scatter",
mode = "markers"
) %>%
layout(xaxis = list(
range = c(0, 35),
zeroline = FALSE
#zerolinecolor = "#eee"
))

Related

R plot_ly: How to add horizontal space in plot area?

I am using plot_ly in R to create a line chart. By default, the line runs right up to the left and right edges of the plot area. I would like to add some space here, as is done by default in ggplot. Does anyone know how to do this with plot_ly?
Reproducible example:
df <- data.frame(date = seq(as.Date('2021-01-01'), length=50, by='day'),
value = rnorm(50))
plot_ly(df, x=~date, y=~value) %>%
add_lines(color=I('black')) %>%
layout(title = 'plot_ly', plot_bgcolor = 'E9E9E9')
ggplot(df, aes(x=date, y=value)) +
geom_line() +
ggtitle('ggplot')
I would like to add the space shown below with red arrows:
You can set the range in xaxis :
library(plotly)
plot_ly(df, x=~date, y=~value) %>%
add_lines(color=I('black')) %>%
layout(title = 'plot_ly', plot_bgcolor = 'E9E9E9',
xaxis = list(range = c(min(df$date) - 3, max(df$date) + 3)))

Colors and text annotations in Plotly R - interactive mode

Problem: The following code produces a plotly plot which groups data based on color and annotates text on the respective y-data-points. When interacting with the plot (in the viewer pane), the selection of e.g. only model a4 (by clicking on the line) does not work correctly as the lines disappear for all other models but the according numbers won't. Any ideas how to fix this?
library(plotly)
library(data.table)
dt <- as.data.table(mpg)
plot_ly(dt[model %in% c("a4", "passat")],
x = ~year,
y = ~displ,
color = ~model,
colors = "Set1") %>%
add_lines() %>%
add_text(y = ~displ,
text = ~displ,
textposition = "top right",
showlegend = FALSE) %>%
layout(xaxis = list(title = ""),
yaxis = list(title = "Anzahl"))
Below you can find a figure describing my problem. Once I select only a4 in the plotly chart, the passat line disappears however the numbers associated to this line remain.
Aim: How to modify the code such that not only the line disappears for a4/passat but also the associated numbers?
Appreciate your suggestions / inputs.
The add_text statement has the option showlegend as FALSE, which effectively hides a potential second legend that would show/hide the text/numbers.
One strategy could be to use legendgroup to group the two legends together for lines and text, while still hiding the text legend. The group should be assigned to model in this case.
library(plotly)
library(data.table)
dt <- as.data.table(mpg)
plot_ly(dt[model %in% c("a4", "passat")],
x = ~year,
y = ~displ,
color = ~model,
colors = "Set1") %>%
add_lines(legendgroup = ~model) %>%
add_text(y = ~displ,
text = ~displ,
textposition = "top right",
showlegend = FALSE,
legendgroup = ~model) %>%
layout(xaxis = list(title = ""),
yaxis = list(title = "Anzahl"))
Plot

Plotly R: Interaction between subplot legend and highlight select

I am trying to get another plot to react to both legend (select/deselect levels of a factor) and drag select. In the following toy example drag select works as intended: the violin and boxplot are re-rendered to depict the points that are selected using the mouse. The legend works for the scatterplot, but not for the boxplot and violin that don't seem to share the legend. I read many posts about shared legend, but nothing worked for subplots of different types.
Even more surprisingly, when deplying to plotly cloud, the plot also loses it's drag select feature (altough I don't need the plotly cloud, I thought I should mention this).
Just to be clear, I'd like the boxplot and violin to be re-rendered based both on selecting on legend and drag-select on scatterplot. As these features theoretically work, the legend should provide the full data or subsets based on levels of the factor used for legend from which the drag selection could be carried out afterwards.
library(plotly)
d <- mtcars
d$cyl <- as.factor(d$cyl)
d <- highlight_key(mtcars)
sp <- plot_ly(d, x = ~mpg, y = ~disp,
color = ~factor(cyl), colors = c("red", "green", "blue"),
legendgroup = ~factor(cyl), showlegend = T) %>%
add_markers()
box <-
plot_ly(d, y = ~disp,
color = I("black"),
legendgroup = ~factor(cyl), showlegend = F) %>%
add_boxplot(name = " ")
violin <-
plot_ly(d, y = ~disp,
color = I("black"),
legendgroup = ~factor(cyl), showlegend = F) %>%
add_trace(type = "violin", name = " ")
p <-
subplot(sp, box, violin, shareY = TRUE, titleX = TRUE, titleY = TRUE) %>%
layout(
dragmode = "select",
barmode = "overlay",
title = "Click and drag scatterplot",
showlegend = TRUE
) %>%
highlight(on = "plotly_selected", off = "plotly_deselect")
p
Any help is greatly appreciated.

Remove continuous legend from plotly

I have a basic scatterplot that I've made in plotly (in R). I'm using a continuous input to color the data points which plotly converts into a gradient. However, the removelegend option doesn't remove a continuous legend the way it removes a discrete legend. Consider the example below.
data = mtcars
data$vs = as.character(data$vs)
plotly::plot_ly(
data = data,
x = ~disp,
y = ~mpg,
color = ~vs,
mode = "markers",
type = "scatter"
) %>%
layout(showlegend = FALSE)
plotly::plot_ly(
data = data,
x = ~disp,
y = ~mpg,
color = ~hp,
mode = "markers",
type = "scatter"
) %>%
layout(showlegend = FALSE)
Is there a way to remove the continuous legend?
The issue arises because in the continuous case plotly doesn't call it a legend, it's a color bar. The easiest way to remove it is to pipe in hide_colorbar()
plotly::plot_ly(
data = data,
x = ~disp,
y = ~mpg,
color = ~hp,
mode = "markers",
type = "scatter"
) %>%
hide_colorbar()

How to control color of boxplot points in plotly?

I would like to reproduce this ggplot graph by using native plotly functionality
ggplot(mtcars, aes(x="mpg", y=mpg))+
geom_boxplot()+geom_jitter(aes(color = mpg),width = 0.3,size=4)+
scale_color_gradient(low="blue", high="yellow", guide = 'none')
ggplotly()
I am trying to do something like this
plot_ly(data=mtcars, y = ~mpg,
type = "box", boxpoints = "all", jitter = 0.3,
marker=list(color=~mpg, size=10),pointpos = 0)
But I can't find the way to control the color of the points (color=~mpg does not do anything)
The reason that I need it to be implemented using native plotly rather then with the "plotlyfied" ggplot as mentioned above is that I need style consistency with other plots that were implemented in native plotly.
This is a little bit of a hack, but it works:
use a separate trace to draw the boxplot (without points)
add a 2nd trace of type scatter to draw the points
unfortunately, scatter traces don't accept jitter as an argument
this means we need to use a continuous scale on the x-axis and add jitter using rnorm
we don't really want the x axis to look like a continuous numeric scale, so we set the tick labels manually to look like categories
Like this:
plot_ly(data=mtcars) %>%
add_trace(y = ~mpg, x=1, type = "box", boxpoints = "none") %>%
add_trace(type="scatter", mode="markers",
y=~mpg, x=rnorm(nrow(mtcars),1,0.05), marker=list(color = ~mpg, size=10))%>%
layout(xaxis = list(tickmode="array", tickvals=c(1), ticktext=c("mtcars") ))
You can extend this approach to have more than one category like this
plot_ly(data=mtcars) %>%
add_trace(y = ~mpg, x=~gear, type = "box", boxpoints = "none") %>%
add_trace(type="scatter", mode="markers", y=~mpg,
x = ~gear+rnorm(nrow(mtcars),0,0.1),
marker=list(color = ~mpg, size=10))%>%
layout(xaxis = list(tickmode="array", tickvals=c(3,4,5), ticktext=c("3","4","5") ))
To control the marker colors, you can use the colorscale argument, together with autocolorscale = FALSE
The colorscale must be an array containing arrays mapping a normalized
value to an rgb, rgba, hex, hsl, hsv, or named color string. At
minimum, a mapping for the lowest (0) and highest (1) values are
required.
For example,
cs = list(list(0, rgb(0,0,1)), list(1, rgb(1,1,0)))
plot_ly(data=mtcars) %>%
add_trace(y = ~mpg, x=1, type = "box", boxpoints = "none") %>%
add_trace(type="scatter", mode="markers",
y=~mpg, x=rnorm(nrow(mtcars),1,0.05),
marker=list(color = ~mpg, autocolorscale=F,
colorscale = cs, size = 10)) %>%
layout(xaxis = list(tickmode="array", tickvals=c(1), ticktext=c("mtcars") ))

Resources