R plotly dynamic line width - r

I have multiple paths in my data, separated by NAs to build separated bar data, for which I'm trying to implement dynamic widths based on another value in that observation (column z below).
Reproducible Example:
index <- c(1,1,NA,2,2)
value <- c(50,51,NA,51,52)
width <- c(3,3,NA,5,5)
data <- data.frame(x = index,
y = value,
z = width)
p <- plot_ly(data, x = x, y = y, mode = "lines",
marker = list(color = '#ff2626'),
line = list(width = data$z))
Where I am trying to reference column z for the line = list(width = data$z) paramter.
Can this be done in the Plotly for R package, or does the line width need to be fixed?

index <- c(1,1,NA,2,2)
value <- c(50,51,NA,51,52)
width <- c(3,3,NA,5,5)
data <- data.frame(x = index, y = value, z = width)
# first, create an empty plotly object
p <- plot_ly(type = "scatter", mode = "lines+markers", marker = list(color = '#ff2626'))
# second, every third line a new lines definition begins. Loop through lines in steps of 3 and add lines
for (i in seq(1, nrow(data), by = 3)){
p <- p %>% add_trace(x=data[i,"x"], y=data[i:(i+1), "y"], line=list(width=data[i,"z"]))
}

Related

How to hide the (interpolated) fill in plotly when there are NA values in the data? (Using R)

I am trying to plot a line graph with fill and NA values. Plotly automatically fills the part with NA values where I want it to be empty. What is the best way to get the correct graph?
It is not an option to set the NA values to 0. I am also using a hoover and do not want to have a result of 0 while hovering over the line.
Example R data + code:
library(plotly)
set.seed(1)
A = data.frame(x = 1900:2000, value=cumsum(rnorm(101)))
A[40:70, 2:3] = NA
fig <- plot_ly(x = A$x, y = A$value, type = 'scatter', mode = 'lines', fill = 'tozeroy')
fig
Result:
Desired Result:
You could split the data into separate traces to achive this:
library(plotly)
library(data.table)
set.seed(1)
A = data.frame(x = 1900:2000, value = cumsum(rnorm(101)))
A[40:70, 2] = NA
setDT(A)
A[, id := rleid(is.na(value))]
fig <- plot_ly(
data = A,
x = ~ x,
y = ~ value,
type = 'scatter',
mode = 'lines',
fill = 'tozeroy',
split = ~ id,
color = I("#1f77b4"),
showlegend = FALSE
)
fig

plot_ly surface plot axes do not cover full range of values

I try to produce a 3D surface plot using plot_ly like this:
rm(list=ls())
set.seed(42)
x_val <- seq(-2,2,0.1)
y_val <- seq(0,1,0.1)
zz <- matrix(NA, nrow = length(x_val), ncol = length(y_val))
for(i in 1:length(x_val)){
for(j in 1:length(y_val)){
zz[i,j] <- rnorm(1, x_val[i], y_val[j]+0.01)
}
}
plot_ly(x = x_val, y = y_val, z = zz, type = "surface")
The resulting plots looks like this:
As you can see, the x_axis has a range between -2 and 2, but only values between -1 and -2 are plotted.
How can I plot the results for the full range of x-values?
As suggested in the comments, I tried to implement the solution from this question (plotly 3d surface - change cube to rectangular space).
However, using
plot_ly(x = x_val, y = y_val, z = zz, type = "surface") %>%
layout(
scene = list(
xaxis = list(range = c(-2,2)),
yaxis = list(range = c(0,1)),
zaxis = list(range = range(zz)),
aspectratio = list(x = 2, y = 1, z = 0.4))
)
leads to this image with the same problem:
The x-axis refers to the columns of the zz matrix.
The length of your x_val vector is 41 and the number of columns of zz is 11.
Thus, for a correct visualization, the zz matrix needs to be trasposed:
plot_ly(x = x_val, y = y_val, z = t(zz), type = "surface")

How to make X values with corresponding small Y values more visible in a histogram

I was wondering if there is a way to make X values with corresponding small Y values more visible in a plotly histogram.
library(plotly)
p <- plot_ly(x = ~rnorm(50000), type = "histogram")
p
For exampe in the image above there are oviously some x values near -4 and 4 that have very small y values in comparison with 1000 which seems to be the biggest y value. I know that this is relative but could the tiny values be displayed with bigger and more visible bars?
I don't think it's a good idea to change height of the bars. Instead you can try something like this:
library(plotly)
df <- data.frame(x = rnorm(5000), y = -1)
p <- plot_ly(data = df, x = x, type = "histogram")
add_markers(p,
data = df,
x = ~x,
y = ~y,
type = 'scatter',
mode = 'markers',
marker = list(symbol = 'star', size = ~abs(x), color = 'green', alpha = 0.5, line = list(color = 'transparent')))
Now it is possible to see that you have some observations close to 4 for example.

I want to increase the maximum magnification of the graph in R in plotly

For graphs drawn with R in the R package(Plotly), there is a limit to enlarging the graph. After zooming to a certain size, I can not zoom in anymore, but I want to zoom in. Do you have any code or arguments that circumvent these restrictions?
If you want to go to the bottom of the link, please check the limit of enlargement with the last example 'Map'.
https://plot.ly/r/trisurf/
I need help!
// additional information
The following code shows the graph and I want to zoom in on the helicopter more than the image! Is there a way?
(Zooming is done using the tools of plotly or using the mouse wheel.)
Click graph image (Maximum magnification)
library(plotly)
library(geomorph)
data <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/_3d-line-plot.csv')
plyFile <- 'http://people.sc.fsu.edu/~jburkardt/data/ply/chopper.ply'
dest <- basename(plyFile)
if (!file.exists(dest)) {
download.file(plyFile, dest)
}
mesh <- read.ply(dest)
# see getS3method("shade3d", "mesh3d") for details on how to plot
# plot point cloud
x <- mesh$vb["xpts",]
y <- mesh$vb["ypts",]
z <- mesh$vb["zpts",]
m <- matrix(c(x,y,z), ncol=3, dimnames=list(NULL,c("x","y","z")))
# now figure out the colormap
zmean <- apply(t(mesh$it),MARGIN=1,function(row){mean(m[row,3])})
library(scales)
facecolor = colour_ramp(
brewer_pal(palette="RdBu")(10)
)(rescale(x=zmean))
p <- plot_ly(data, x = ~x1, y = ~y1, z = ~z1, type = 'scatter3d', mode = 'lines',
line = list(color = '#1f77b4', width = 1)) %>%
add_trace(x = ~x2, y = ~y2, z = ~z2,
line = list(color = 'rgb(44, 160, 44)', width = 1)) %>%
add_trace(x = ~x3, y = ~y3, z = ~z3,
line = list(color = 'bcbd22', width = 1))
add_trace(p, x = x, y = y, z = z,
i = mesh$it[1,]-1, j = mesh$it[2,]-1, k = mesh$it[3,]-1,
facecolor = facecolor,
type = "mesh3d")

How to color plotly bubble chart by any category variable

I have data set as below and stored in data frame dt
dt = data.frame(category=c("A","B","C"),X=c(1,2,3),Y=c(3,4,5))
and am trying to plot bubble chart using plotly as below and color it by category
library(plotly)
plot_ly(dt, x =X, y = Y, size = X, mode = "markers",color = category)
But it is not showing the bubble chart properly.
I think its a bug and I believe the folks at Plotly are working on it. See here.
I think it has to do with the number of unique points being shown on the x-axis and the number of unique size values. See below for an illustration.
First example has 10 points but 10 unique sizes as well
Second example has 10 points but only 5 unique sizes
set.seed(123)
# This doesn't work
N<- 10
dt <- data.frame(category = sample(LETTERS, size = N, replace = T),
X = 1:N,
Y = 1:N)
plot_ly(dt, x = X, y = Y, color = category, mode = "markers", size = X)
# But this works
N<- 10
dt <- data.frame(category = sample(LETTERS, size = N/2, replace = T),
X = 1:N,
Y = 1:N)
plot_ly(dt, x = X, y = Y, color = category, mode = "markers", size = X)
For your example, you could try using ggplotly() instead:
dt = data.frame(category=c("A","B","C"),X=c(1,2,3),Y=c(3,4,5))
ggplot(dt, aes(X, Y, size = X, color = category)) + geom_point()
ggplotly()
Hope this helps.

Resources