R plotly show xcoordinate on xaxis - r

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.

Related

Adding polygon to plotly scatterplot

I created a plotly scatterplot in R on which I would like to add a polygon.
current plot
This is the code I used to make the graph:
fig <- plot_ly(data = data, x= ~xbeak, y = ~ybeak, color = ~coordsbeak, text = ~paste(coordsbeak), type = 'scatter')
Now I want to add a polygon to this plot, which I tried with add_polygons. The polygon is a different dataframe, consisting two columns with 42 x and y coordinates.
fig <- fig %>% add_polygons(x = xym$x, y=xym$y)
However when I try to run this I get this error which I don't understand. Any idea what I'm doing wrong?
Error:
! Tibble columns must have compatible sizes.
• Size 42: Columns x and y.
• Size 11149: Columns text and color.
ℹ Only values of size one are recycled.
Run rlang::last_error() to see where the error occurred.
Two solutions below. First, adding inherit = FALSE to add_plygons().
library(tidyverse)
library(plotly)
xym<-data.frame(y=c(3,4,4,3),
x=c(5,5,6,6))
fig <- plot_ly(data = iris, x= ~Sepal.Length, y = ~Sepal.Width, color = ~Species, text = ~paste(Species), type = 'scatter', mode="markers")
fig <- fig %>% add_polygons(x = xym$x, y=xym$y, inherit = FALSE, showlegend = FALSE)
fig
Or, Switch the order of operations - make the polygons first, then the scatterplot.
Here is an example with iris data:
xym<-data.frame(x=c(5,5,6,6),
y=c(3,4,4,3))
# make an empty plot_ly object
fig <- plot_ly()
# add the polygons
fig<-fig %>% add_polygons(x = xym$x, y=xym$y)
# add the scatterplot
fig<-fig %>% add_trace(data = iris, x= ~Sepal.Length, y = ~Sepal.Width, color = ~Species, text = ~paste(Species), type ="scatter", mode="markers")
fig
referenced - Adding a polygon to a scatter plotly while retaining the hover info
and Adding a polygon to a scatter plotly

Plotly: shareX between side-by-side plots

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

plotting 3D surface with plotly (matrix transformation with akima::interp)

In order to plot a surface in plotly with x, y, z, we can use the function interp to create the data (as input for the add_surface function in plotly)
This article give the solution.
I follow the different steps.
With the follow code, we can plotly a surface with markers.
library(akima)
library(plotly)
x=rep(seq(-1,5,0.2),time=31,each=1)
y=rep(seq(-1,5,0.2),time=1,each=31)
df=data.frame(x=x,y=y,
z=2*x+y-1.4)
fig <- plot_ly()%>% add_markers(data=df,x = ~x, y = ~y, z = ~z,
marker = list(color = "blue",
showscale = TRUE))
fig
We can see the following plot
Then I use interp to create the data for the surface, and I plot the surface together with the markers.
s = interp(x = df$x, y = df$y, z = df$z)
fig <- plot_ly()%>%
add_surface(x = s$x, y = s$y, z = s$z)%>%
add_markers(data=df,x = ~x, y = ~y, z = ~z,
marker = list(color = "blue",
showscale = TRUE))
fig
I have the following image.
We can see that the result is different. And I can't see why.
When I try to change the function to generate z, sometimes, the two surfaces are the same. For example, for this data.frame
df=data.frame(x=x,y=y,
z=x+y+1)
We have the following image. And we can see that this time, we get the same surfaces.
It appears the meanings of x and y are swapped in the add_surface versus the meanings in interp. The example that worked had x and y appear symmetrically. So swap them back by transposing the z matrix:
fig <- plot_ly()%>%
add_surface(x = s$x, y = s$y, z = t(s$z))%>%
add_markers(data=df,x = ~x, y = ~y, z = ~z,
marker = list(color = "blue",
showscale = TRUE))
fig
This is just a guess because the documentation in plotly is so weak. akima::interp clearly documents that rows of z correspond to different x values.

Viewing hover info for overlapping scatter points in Plotly R

I was surpised I was not able to google this solution, so I thought i'd put up a post. Surely others have the same issue...
The issue I have is that when two or more scatter points overlap (i.e. same x and y), the hover information only shows the info of the top point.
Example:
df <- data.frame(ID=1:6, x=c(5:9, 7), y=c(1:5, 3)+10, info=paste('Hover information: ',c(LETTERS[c(1:6)])))
df
plot_ly(df) %>%
add_trace(x = ~x,
y = ~y,
type = 'scatter',
mode = 'markers',
marker = list(color = 1:6,
symbol = 1:6,
size = 25),
hoverinfo = "text",
text = df$info)
It is possible to make BOTH hoverinfo for the middle point to show up? Possibly as:
Hover information: C
Hover information: F
You can try to use add_markers() and jitter, e.g:
plot_ly(df) %>%
add_markers(x = ~jitter(x, 1),
y = ~jitter(y, 1),
type = 'scatter',
mode = 'markers',
marker = list(color = 1:6,
symbol = 1:6,
size = 25),
hoverinfo = "text",
text = ~info)
But to get multiple information as you designed, maybe you need to modify your dataframe (but you will lose the colour code):
df$info <- as.character(df$info)
df$combined_info[1] <- df$info[1]
for(i in 2:nrow(df)){
df$combined_info[i] <- df$info[i]
for(j in 2:i-1){
if((df$x[j] == df$x[i]) && (df$y[j] == df$y[i])){
df$combined_info[i] <- paste0(df$combined_info[j], "<br>",
df$info[i])
}
}
}
And then you can use the original plotly code, while changing the "text" argument:
plot_ly(df) %>%
add_trace(x = ~x,
...
text = ~combined_info)

plotly bar and line chart

I want to plot a bar together with a line chart in R with plotly.
My first attempt was
p <- plot_ly(
x = c(1,2,3,4,5),
y = c(1,2,1.5,3,2),
type='scatter',
mode='lines',
line = list(color = 'black')
)
add_trace(
p,
x = c(1,2,3,4,5),
y = c(0.5,0.7,0.6,0.9,0.8),
type='bar',
marker = list(color = 'red')
)
The result is right, but I get the following warning:
Warning message: The following attributes don't exist: 'mode', 'line'
I guess cause the bar plot in add_trace() cannot handle the line and mode parameter from the plot_ly() function. So I changed the order:
p <- plot_ly(
x = c(1,2,3,4,5),
y = c(0.5,0.7,0.6,0.9,0.8),
type='bar',
marker = list(color = 'red')
)
add_trace(
p,
x = c(1,2,3,4,5),
y = c(1,2,1.5,3,2),
type='scatter',
mode='lines',
line = list(color = 'black')
)
This time I get the following message and red markers are displayed on the black line chart.
A marker object has been specified, but markers is not in the mode
Adding markers to the mode...
How can I fix this? (I'm using the R package plotly 4.1.0)
I'm running plotly 4.0.1, but if I add mode='lines+markers' instead of just mode='lines' the error message goes away for me.
--edit to add full code--
For the lazy (like me), here's the full code that worked on my end:
p <- plot_ly(x = c(1,2,3,4,5),
y = c(0.5,0.7,0.6,0.9,0.8),
type='bar',
marker = list(color = 'red', opacity=0)
)
add_trace(p,
x = c(1,2,3,4,5),
y = c(1,2,1.5,3,2),
type='scatter',
mode='lines+markers',
line = list(color = 'black')
)

Resources