Change step between ticks with plotly - r

I have the following code
library(plotly)
A <- matrix(seq(1, 20), nrow = 4, ncol = 5)
p <- plot_ly(z = t(A), type = "heatmap", colorscale = "Greys")
p
Is there anyway to change to steps between the ticks on the y-axis to display only the even numbers (0, 2, 4)?

You need to specify some of the layout.yaxis.tick* attributes. You can read more about it here to see further customisation options.
library(plotly)
A <- matrix(seq(1, 20), nrow = 4, ncol = 5)
p <- plot_ly(z = t(A), type = "heatmap", colorscale = "Greys")
p %>% layout(
yaxis = list(
tickmode = 'array',
tickvals = seq(0, 5, by = 2),
ticktext = seq(0, 5, by = 2)
)
)

Related

How to change colors and symbols according to categorical variable in R plotly:::add_markers()?

I am trying to change markers' colors and symbols according to a categorical variable in r-plotly. But it is not working. Also, I would like to draw "contour lines" around the "walls" of the 3d panel, similar to what I have in the attached figure. Any help is welcome.
# Packages
library(plotly)
library(misc3d)
# Fake data
norm_vec <- function(x) sqrt(sum(x ^ 2))
X3d_norm <- data.frame(T3 = runif(100), T6 = runif(100), P4 = runif(100))
norms <- apply(X3d_norm, 1, norm_vec)
X3d_norm <- X3d_norm / norms
X3d_norm$cluster <- factor(sample(1:2, 100, replace = T), ordered = T)
# Getting octant data
f <- function(x, y, z){x ^ 2 + y ^ 2 + z ^ 2}
R <- 1
x <- y <- z <- seq(0, R, length.out = 100)
g <- expand.grid(x = x, y = y, z = z)
voxel <- array(with(g, f(x, y, z)),dim = c(100, 100, 100))
cont <- computeContour3d(voxel, level = R ^ 2, x = x, y = y, z = z)
idx <- matrix(0:(nrow(cont) - 1), ncol = 3,byrow = TRUE)
# Colors and symbols
pal <- c("#E69F00", "#009E73")
pal <- setNames(pal, c(1, 2))
symb <- c("circle", "square")
symb <- setNames(symb, c(1, 2))
# Plot
plot_ly() %>%
add_mesh(
x = cont[, 1], y = cont[, 2], z = cont[, 3],
i = idx[, 1], j = idx[, 2], k = idx[, 3],
opacity = 0.15
) %>%
add_markers(
data = X3d_norm,
x = ~T3, y = ~P4, z = ~T6,
color = ~cluster, colors = pal,
symbol = ~cluster, symbols = symb
) %>%
layout(
legend = list(title = list(text = 'Clusters')),
scene = list(
xaxis = list(
title = "T3", nticks = 4, tickvals = c(0.20, 0.40, 0.60, 0.80),
tickfont = list(size = 16), titlefont = list(size = 24)),
yaxis = list(
title = "P4", nticks = 4, tickvals = c(0.20, 0.40, 0.60, 0.80),
tickfont = list(size = 16), titlefont = list(size = 24)),
zaxis = list(
title = "T6", nticks = 4, tickvals = c(0.20, 0.40, 0.60, 0.80),
tickfont = list(size = 16), titlefont = list(size = 24))
)
)

Add title to each y-axis with Plotly subplots

Let's consider the following plot:
p1 <- plot_ly(
x = c("giraffes", "orangutans", "monkeys"),
y = c(20, 14, 23),
name = "Size",
type = "bar")
p2 <- plot_ly(
x = c("giraffes", "orangutans", "monkeys"),
y = c(10, 2, 3),
name = "Weight",
type = "bar"
)
p <- subplot(p1, p2, nrows = 2, shareX = TRUE)
p %>% layout(yaxis = list(title = "Size"))
I would like to control the y-axis title of each subplot. It looks like the way I am doing it I only manage to control the top subplot. How can I also add a title for the y-axis of the bottom subplot?
titleY = TRUE will make the difference, such that:
p1 <- plot_ly(
x = c("giraffes", "orangutans", "monkeys"),
y = c(20, 14, 23),
name = "Size",
type = "bar")%>%layout(yaxis = list(title = c("Size1")))
p2 <- plot_ly(
x = c("giraffes", "orangutans", "monkeys"),
y = c(10, 2, 3),
name = "Weight",
type = "bar"
)%>%layout(yaxis = list(title = c("Size2")))
p <- subplot(p1, p2, nrows = 2, shareX = TRUE, titleY= TRUE)
p

R plotly overlay heatmaps

I have two matrices, A and B, I want to make two heatmaps with plotly, and overlay them
library(plotly)
A = matrix(c(1:8, rep(0, 8)), ncol = 4)
B = matrix(c(rep(0, 8), 1:8), ncol = 4)
PA <- plot_ly(z = A, type = "heatmap", colors = colorRamp(c("white", "green")))
PB <- plot_ly(z = B, type = "heatmap", colors = colorRamp(c("white", "red")))
When I try to overlay them, they are indeed overplayed, but the second heatmap totally masked the first one.
PA %>% add_trace(z = B, type = "heatmap")
I could change the opacity in order to 'see' both heatmaps
PA %>% add_trace(z = B, opacity = 0.5, type = "heatmap")
But it is really not beautiful, and I cannot set different colours for each heatmap.
Is there any elegant way to overlay them like the following example? thanks a lot.
p = plot_ly(x = rnorm(500), opacity = 0.6, type = "histogram") %>%
add_trace(x = rnorm(500)+1) %>%
layout(barmode="overlay")
I am not sure if it is possible, but maybe you could trick it. You could try:
ay <- list(
title = "",
zeroline = FALSE,
showline = FALSE,
showticklabels = FALSE,
showgrid = FALSE
)
PB <- PB %>% layout(yaxis = ay, xaxis = list(range = c(1.5, 3.5), dtick = 1))
PA <- PA %>% layout(yaxis = list(dtick = 1), xaxis = list(range = c(-0.5, 1.5), dtick = 1))
subplot(PA, PB, nrows = 1, shareX = TRUE, shareY = FALSE)

How to draw a level line in a filled contour plot and label it?

I have the following contour plot
x <- c(0,25,50,75,100)
y <- c(0,10,20)
z <- matrix(c(12,12,13,12,5,12,5,5,5,12,5,12,13,14,15), nrow = 5, ncol = 3, byrow = TRUE)
A <- matrix(seq(0, 100, by = 25), nrow = 3, ncol = 5, byrow = TRUE) #As x
B <- matrix(seq(0,20, by = 10), nrow = 3, ncol = 5) #As y
filled.contour(x,y,z, color=terrain.colors,#
plot.axes = { axis(1); axis(2); points(A,B)})
How can I draw a level line around the level with value 5 and label it and obtain something like:
You may use contour in plot.axes. It's not possible to add the line at exactly 5, so I used 5.01 instead and specified labels. This is at least the principle.
filled.contour(x, y, z, color = terrain.colors,
plot.axes = {axis(1); axis(2); points(A, B);
contour(x, y, z, levels = 5.01, labels = "5", col = "red", add = TRUE)})
library(fields)
library(emdbook)
x <- c(0,25,50,75,100)
y <- c(0,10,20)
z <- matrix(c(12,12,13,12,5,12,5,5,5,12,5,12,13,14,15), nrow = 5, ncol = 3, byrow = TRUE)
A <- matrix(seq(0, 100, by = 25), nrow = 3, ncol = 5, byrow = TRUE) #As x
B <- matrix(seq(0,20, by = 10), nrow = 3, ncol = 5) #As y
image.plot(x,y,z)
contour(x,y,z,
add=TRUE, lwd=2, cex=2)

R: Plot_ly 3d graph with trace line

I am using plotly 4.7.0. I am trying to add a 3d line to a 3d plot_ly surface plot. When I don't add the add_lines() function inside of the plot_ly call it looks fine. As soon as I do add the add_lines, the graph gets all messed up and doesn't add the 3d line graph.
library(plotly)
m_x = seq(-2, 2, .01)
m_y = seq(-2, 2, .01)
df = expand.grid(m_x, m_y)
df['matrix'] = exp(-(df$Var1^2+df$Var2^2))
m_z = matrix(df$matrix, nrow = length(m_x), ncol = length(m_y))
m_df = list(m_x, m_y, m_z)
x1 = seq(-2, 0, by=0.0202)
y1 = runif(100, min=-0.03,max=0.03)
z1 = exp(-(x1^2+y1^2))
df = data.frame(x1, y2, z2)
names(df) <- c("df_x", "df_y", "df_z")
colors = c( "Blue", "Cyan", "Green", "Yellow", "Orange", "Red")
p1 <- plot_ly(x = m_df[[1]], y = m_df[[2]], z = m_df[[3]],
colors = colors, color = m_df[[3]]) %>% add_surface() %>%
add_lines(x = df$df_x, y = df$df_y, z = df$df_z, data = df,
line = list(color = 'red', width = 1)) %>%
layout(title = "Hike_Example",
scene = list(aspectratio = list(x = 4, y = 4, z = 1)))
p1
Here is the code of the surface plot with the 3D line.
I plotted the 3D line using add_trace in place of add_lines.
library(plotly)
m_x = seq(-2, 2, .01)
m_y = seq(-2, 2, .01)
df = expand.grid(m_x, m_y)
df['matrix'] = exp(-(df$Var1^2+df$Var2^2))
m_z = matrix(df$matrix, nrow = length(m_x), ncol = length(m_y))
m_df = list(m_x, m_y, m_z)
x1 = seq(-2, 0, by=0.0202)
y1 = runif(100, min=-0.03,max=0.03)
z1 = exp(-(x1^2+y1^2))
df = data.frame(x1, y1, z1)
names(df) <- c("df_x", "df_y", "df_z")
colors = c( "Blue", "Cyan", "Green", "Yellow", "Orange", "Red")
p1 <- plot_ly(x = m_df[[1]], y = m_df[[2]], z = m_df[[3]],
colors = colors, color = m_df[[3]]) %>% add_surface() %>%
add_trace(x = ~df_x, y = ~df_y, z = ~df_z, data = df,
type="scatter3d", mode="lines",
line = list(color = "red", width = 4)) %>%
layout(title = "Hike_Example",
scene = list(aspectratio = list(x = 4, y = 4, z = 1)))
p1

Resources