How to set X axis range on plotly graphs? - r

I am using R version of plotly. Here is what I want:
x = 1:100
y = 1:100
plot_ly(x, y)
I want the graph only show where x>20 and x<40, ignoring the other part. How to do this ?

Set the layout of xaxis.
y = 1:100
plot_ly(x=~x, y=~y) %>%
layout(
xaxis = list(
range=c(20,40)
)
)

Related

plotly 3D graph function cuts off off axis labels in R

I am creating a 3D line plot in plotly in R and I cannot stop the plot funciton from shortening the axes labels.
library(plotly)
my_data <- read_excel("filedir\\filename")
## Create Axis titles and margins
axx <- list(
title = "Year-Month"
)
axy <- list(
title = "Easting"
)
axz <- list(
title = "Elevaiton (ft)"
)
m <- list(
l = 50,
r = 50,
b = 50,
t = 50,
pad = 4
)
x <- 70*(runif(70, 0, 1))
y <- 55*(runif(70, 0, 1))
z <- 40*(runif(70, 0, 1))
##Plot Graph
fig <- plot_ly(my_data, x = ~Year, y = ~East, z = ~Elevation, type = 'scatter3d', mode = 'lines', color = ~Year)
fig <- fig %>% layout(scene = list(xaxis=axx,yaxis=axy,zaxis=axz))
fig <- fig %>% layout(autosize = T, margin=m)
fig
I have played around with the margins and tried to add ax.dist = 20 as well as standoff = 100 in the plot_ly() function and axes lists, but I just cannot seem to figure it out. I feel like it has something to do with the margins or image size.
The numbers are Easting values. An example of one is 1102031.3. It gets shortened to 1.10203M (See image). Thanks for any help.

One slider to control graphs in single subplot in R using plotly

I'm looking for certain fix with range selector in plotly using R.
I have two plots visualized via a single subplot using Plotly in R. Now, I need to add a Range Slider/Selector to the complete plot, so that changing it modifies both my plots.
Is it possible via Plotly? (using R only)
This functionality is similar to Dygraphs synchronize feature(https://rstudio.github.io/dygraphs/gallery-synchronization.html).
I'd recommend using subplots option shareX = TRUE:
Please check the following example:
library(plotly)
DF1 <- data.frame(x=1:100, y=runif(100)+ seq(0, 1, length.out = 100))
DF2 <- data.frame(x=1:100, y=runif(100)+ seq(0, 2, length.out = 100))
p1 <- plot_ly(DF1, x = ~x, y = ~y, type = "scatter", mode = "lines+markers")
p2 <- plot_ly(DF2, x = ~x, y = ~y, type = "scatter", mode = "lines+markers")
p <- subplot(p1, p2, nrows = 2, shareX = TRUE)
p

Show 3d axes in Plotly, R

Using plotly in R, how do I make a 3d plot that shows the axes, like this:
instead of a plot that shows a bounding box, like this:
?
R code for the second is
t <- seq(0,2*pi,by =1)
r <- seq(0,1,by = .1)
x <- as.vector(outer(t,r,function(t,r).5*r*cos(t)))
y <- as.vector(outer(t,r,function(t,r)r*sin(t)))
z <- 4*x^2+y^2
plot_ly(x = ~x, y = ~y, z = ~z, type = "mesh3d", opacity=.5)

Custom tick marks / labels appear on x & y axis in plotly surface plot?

I would like to control the tick marks for the surfaceplot
Using the standard volcano code here for a reproducible code. This is what I tried but it doesn't seem to be working.
p <- plot_ly(x = kd$x, y = kd$y, z = kd$z) %>% add_surface() %>% layout(title = 'Example plot',xaxis = list(autotick = F, dtick = 10))
I want the x-axis to be in an increment of 10 and y axis the digits need to be replaced by the words, but I am only looking for 3 ticks here like "Sixty" (instead of 60), "Eighty"(instead of 80) and "hundred" (instead of 100). I have no clue how to deal with the y axis.
In this answer too it says to try this, but it's not working for me.
Control which tick marks / labels appear on x-axis in plotly?
In this, it mentions about adding a suffix but not replacing.
Plotly in R: format axis - tick labels to percentage
For your first question (i.e., I want the x-axis to be in an increment of 10) hard to say since the x axis is from 1 to 4. But for your second question you could try this:
plot_ly(x = kd$x, y = kd$y, z = kd$z) %>% add_surface() %>% layout(scene = list(yaxis = list(
tickmode = "array",
nticks = 6,
tickvals = c(60, 80, 100),
ticktext = c("Sixty", "Eighty", "Hundred")
)))

common axis subplots with plot.ly for R

I am attempting to use subplots with the plot.ly R library for interactive online charting. I can successfully create a subplot, however am struggling to only have a single y-axis that is common to both charts.
The plot.ly website does provide an example for a common x-axis, however this is done slightly differently using and additional trace rather than the group option that is provided within the plot_ly() function.
example code:
library(data.table)
library(plotly)
dt <- data.table(x = c("A","B","C","D","A","B","C","D"),
y = c(12,4,3,9,5,10,3,7),
group = factor(c(rep("G1",4),rep("G2",4))))
dt$id <- as.integer(dt$group)
xx <- xaxis_standard
yy <- yaxis_standard
p <- plot_ly(dt, x=x, y=y, group = group, xaxis = paste0("x",id))
p <- layout(p, yaxis = list(range = c(0, max(y))))
p <- subplot(p, margin = 0.05)
p <- layout(p,showlegend = F, yaxis = list(anchor = 'x1'))
p
This image shows what results when I execute the code.
What I would like to have is the same chart, however without the y-axis on the right hand subplot.
Subplots are on separate axes labeled xaxis2, yaxis2, etc. Those axes are also arguments to layout().
p <- layout(p, showlegend = F, yaxis = list(anchor = 'x1'),
yaxis2 = list(showticklabels = F))
p

Resources