I would like to have two side by side plots sharing the same X-axis and the same toolbar. This means that, by zooming in the first plot, the second plot should automatically resize to the same zoomed region.
A way to do that could be to stack the plots one above the other, using shareX=TRUE, but I need them to be side by side.
In python there seems to be a way to do that, using fig.update_xaxes(matches='x'). Is there a similar option in R?
Here is a sample code:
library(plotly)
n = 10
x = 1:n
y = rnorm(n)
fig1 <- plot_ly(x = x, y = y, type = 'scatter', mode = 'lines+markers')
fig2 <- plot_ly(x = x, y = y, type = 'scatter', mode = 'lines+markers')
fig <- subplot(fig1, fig2, shareX = TRUE) # shareX actually not working here
fig
Thank you in advance!
We can use matches in R just as we can in python.
Run schema() and navigate:
object ► layout ► layoutAttributes ► xaxis ► matches
for more info.
This keeps all (x&y) axes synced:
library(plotly)
n = 10
x = 1:n
y = rnorm(n)
fig1 <- plot_ly(x = x, y = y, type = 'scatter', mode = 'lines+markers')
fig2 <- plot_ly(x = x, y = y, type = 'scatter', mode = 'lines+markers', xaxis = "x") %>% layout(xaxis = list(matches = "x"))
fig <- subplot(fig1, fig2, shareX = TRUE, shareY = TRUE)
fig
Related
Plotly offers the option to display a moveable horizontal or vertical "spikeline" by adding layout(hovermode = "x unified") or layout(hovermode = "y unified") (see documentation). For example:
library(plotly)
x <- seq(from = 0, to = 2*pi, length = 100)
y <- sin(x)
fig <- plot_ly(x = x, y = y, type = "scatter", mode = "markers")
fig <- fig %>% layout(hovermode = "x unified")
fig
creates this plot with a vertical line that follows the cursor:
I would like to have both the vertical and the horizontal spikeline displayed. Like this:
I tried to modify the layout like this:
fig <- fig %>% layout(hovermode = "x unified") %>% layout(hovermode = "y unified")
# or
fig <- fig %>% layout(hovermode = c("x unified", "y unified"))
# or
fig <- fig %>% layout(hovermode = "x unified|y unified")
None of this worked. Would anybody have a suggestion?
You could use spikemode with the option across in your layout for both xaxis and yaxis. Here is a reproducible example:
library(plotly)
x <- seq(from = 0, to = 2*pi, length = 100)
y <- sin(x)
fig <- plot_ly(x = x, y = y, type = "scatter", mode = "markers")
fig %>%
layout(xaxis = list(spikemode = 'across'),
yaxis = list(spikemode = 'across'))
Created on 2022-10-29 with reprex v2.0.2
As you can see both vertical and horizontal spike lines are shown.
If you only want to have the spike lines to your axis, you could use toaxis in your spikemode like this:
fig %>%
layout(xaxis = list(spikemode = 'toaxis'),
yaxis = list(spikemode = 'toaxis'))
Created on 2022-10-29 with reprex v2.0.2
As you can see this only shows the spike lines to your axis.
Check out this resource on 3d plots - I had the same issue and applied the xaxis = list() and yaxis = list() arguments to layout() and it worked well for me.
https://plotly.com/r/3d-hover/
I'm trying to plot a 3D scatter using Plotly and R. Other than x, y and z I also would like to set the color of each point depending on a fourth variable.
I manage to set the plot correctly (the use of name = ~res is to show the value of res while hovering), but I am not able to change the name of the colorbar.
This is a mock code of what I've done:
library(tidyverse)
library(plotly)
a = seq(1,10,1)
b = seq(100,1000,100)
c = seq(1,4.9,0.4)
data = tibble(a,b,c)
data <- data %>% mutate(res = a+b+c)
layout_details <- list(xaxis = list(title = 'a [-]'),
yaxis = list(title = 'b [-]'),
zaxis = list(title = 'c [-]'),
coloraxis=list(colorbar=list(title=list(text='Here are the results'))))
p = plot_ly(data, x = ~a, y = ~b, z = ~c, color = ~res, type = 'scatter3d',
mode = 'markers', name = ~res, showlegend = FALSE, scene = 'scene1')
p <- p %>% layout(scene1 = layout_details)
p
I've noticed that a quite similar question was asked (R plotly to legend title value ignored for continuous color scatter plot), but without any answers.
Does anyone know how to solve this?
Thanks
You can define your colorbar inside the marker argument.
The name argument is interfering with the colorbar therefore I moved res from the name argument to the hovertemplate and the customdata.
Code
p = plot_ly(data, x = ~a, y = ~b, z = ~c,
name = "",
scene = 'scene1',
type = 'scatter3d',
mode = 'markers',
customdata = as.list(data$res),
hovertemplate = paste('x: %{x}',
'y: %{y}',
'z: %{z}',
'name: %{customdata}',
sep = "\n"),
marker = list(color = ~res,
colorbar = list(title = "Here are the results"),
colorscale='Viridis',
showscale = TRUE))
p <- p %>% layout(scene1 = layout_details)
p
Plot
I created a scatter plot with plotly in R. Now I want to plot a boxplot with different data next to the scatter plot. I want to use plotly for this.
The result should look like this. Can someone help me please, I have no idea how to do that.
My code so far is
plot_ly(ds, x = ~x, y = ~y , mode = "markers", name = "Clusters", opacity = point.opacity,
text = ds$id,
hoverinfo = "text",
marker = list(symbol = point.symbol, color = ~color, size = point.size,
line = list(color = "#262626", width = point.linewidth, opacity = point.lineopacity)),
showlegend = F)
Here is an example on how to make a scatter with marginal box plots with plotly:
library(plotly)
data(iris)
create, three plots for the data: one for the scatter, two for the appropriate box plots, and one additional empty plot. Use the subplot function to arrange them:
subplot(
plot_ly(data = iris, x = ~Petal.Length, type = 'box'),
plotly_empty(),
plot_ly(data = iris, x = ~Petal.Length, y = ~Petal.Width, type = 'scatter',
mode = 'markers'),
plot_ly(data = iris, y = ~Petal.Width, type = 'box'),
nrows = 2, heights = c(.2, .8), widths = c(.8,.2), margin = 0,
shareX = TRUE, shareY = TRUE) %>%
layout(showlegend = F)
I'm trying to create a graph with a similar x-axis format to this (from https://plot.ly/r/line-charts/):
code given:
library(plotly)
x <- c(1:100)
random_y <- rnorm(100, mean = 0)
data <- data.frame(x, random_y)
p <- plot_ly(data, x = ~x, y = ~random_y, type = 'scatter', mode = 'lines')
However, running the code on my machine produces this graph:
Notice that the x-coordinate hover is not there.
R version: 3.4.1
Plotly version: 4.7.1
Changing the hoverinfo and text tags in plot_ly just changes the hoverinfo over the graph. How do I show the same hovering x-coordinate in the first graph?
Update: setting hoverinfo = "text+x" and layout(hovermode = "x") shows the x-coordinate on the x-axis and the point tooltips.
Try setting hovermode to 'x'
p <- plot_ly(data, x = ~x, y = ~random_y, type = 'scatter', mode = 'lines') %>% layout(hovermode = 'x')
and it should work.
I have a plotly graph in R with a reversed x-axis. I want to be able to play with its ranges. I know it is normal because by design, setting "range" turns "autorange" to "FALSE". https://plot.ly/r/reference/#layout-xaxis-range
But I still need to play with ranges of this reversed axes. Any workarounds, anyone?
Greetings
library(plotly)
s <- seq(1, 8)
plot_ly(x = s, y = s) %>%
add_trace(y = rev(s)) %>%
layout(
xaxis = list(range = c(3,5), autorange="reversed"),
yaxis = list(range = c(2, 5)))
Just retyping my comment above. Using plotly_3.4.3 (development version) and ggplot2_2.1.0. Remove autorange="reversed" from your code and try:
s <- seq(1, 8)
plot_ly(x = s, y = s) %>%
add_trace(y = rev(s)) %>%
layout( xaxis = list(range = c(5,3)), yaxis = list(range = c(2, 5)))