R Plotly 3D: axis labels with TeX - r

I'd like to use TeX to label the axes of a 3D-surface plot.
I somehow managed - with help of MathJax - to use TeX for the title. Not the axes though. Attached you find an example of what I tried to do.
x0 <- seq(1,100,by=0.1)
z0<-x0^2%*%t(x0)
library(plotly)
p <- plot_ly(
x = x0,
y = x0,
z = z0)%>%
add_surface() %>%
layout(
title = TeX("\\text{Graph of } f(x,y)=x^2*y"),
scene = list(
xaxis = list(title = "x"),
yaxis = list(title = "y" ),
zaxis = list(title = TeX("f(x,y)=x^2*y"))
))%>%
config(mathjax = 'cdn')
p
The result is the following
How can I use TeX also for the axis labels?
Moreover, anyway I can use $\cdot$ in the title instead of *?

Related

Plotly 3d scatterplot with more than 6 groups

I try to plot a 3d scatterplot with plotly in R with overall 12 groups and cannot find out how to use 12 different symbols and colours.
palette <- distinctColorPalette(12)
pie(rep(1, 12), col=palette)
library(plotly)
fig <- plot_ly(data, x = ~Length..mm., y = ~Width..mm., z = ~Height..mm.,
color = ~spec, colors = palette, symbol =~spec, symbols = ~spec)
fig <- fig %>% layout(scene = list(xaxis = list(title = 'Seed length (mm)'),
yaxis = list(title = 'Seed width (mm)'),
zaxis = list(title = 'Seed heigth (mm)'))
I really appreciate any help you can provide.
this is the output

How can I flip the x axis (180 degrees) using Plotly in R

I created a probability histogram using the following code:
set.seed(99)
x <- rnorm(1000)
fig1 <- plot_ly (y = ~x, type = 'histogram', histnorm = 'probability') %>%
layout(title = "Frequency Distribution",
xaxis = list(title = "Frequency", mirror = TRUE ))
fig1
The above code gives me the following:
as can be seen in the code I tried to flip the plot 180 degrees using mirror in the xaxis part. however, it does not work. I want to have such a plot:
Any ideas to fix it?
Use autorange = "reversed"
plot_ly (y = ~x, type = 'histogram', histnorm = 'probability') %>%
layout(
title = "Frequency Distribution",
xaxis = list(title = "Frequency", autorange = "reversed")
)

Changing margins of a 3d scatter plot using plotly- R

I'm using plotly to generate a 3D scatter plot, see example code below:
library(plotly)
mtcars$am[which(mtcars$am == 0)] <- 'Automatic'
mtcars$am[which(mtcars$am == 1)] <- 'Manual'
mtcars$am <- as.factor(mtcars$am)
p <- plot_ly(mtcars,
x = ~wt,
y = ~hp,
z = ~qsec,
color = ~am,
colors = c('#BF382A', '#0C4B8E')) %>%
add_markers() %>%
layout(scene = list(xaxis = list(title = 'Weight'),
yaxis = list(title = 'Gross horsepower'),
zaxis = list(title = '1/4 mile time')),
title = "Example plot")
When I download a static image of the plot, there's no space at the top of the image and the title looks nearly cut off. Is there a way to adjust the margins on the 3D plot to fix this? Attempting to adjust the margins by specifying layout yields the error:
'scatter3d' objects don't have these attributes: 'margin'
Perhaps you tried to add margin at a wrong place. The following does work:
layout(scene = list(xaxis = list(title = 'Weight'),
yaxis = list(title = 'Gross horsepower'),
zaxis = list(title = '1/4 mile time')),
title = "Example plot", margin = list(t = -1))

How to add a superscript or a subscript to an axis label to a 3D plot in plotly?

How to add a superscript or a subscript to an axis label to a 3D plot in plotly?
I tried to use bquote, but it did not work. Googling did not bring much on the matter either.
Scavenged code:
library(plotly)
set.seed(123)
n <- 100
theta <- runif(n, 0, 2*pi)
u <- runif(n, -1, 1)
base<-'B1'
compare<-'A1'
plot (1, 1, main = bquote('Annual mean' ~CO[2] ~'Flux Difference: \n' ~.(compare)~ 'minus'~.(base)))
p <- plot_ly(x = ~sqrt(1 - u^2) * cos(theta), y = ~sqrt(1 - u^2) * sin(theta), z = ~u) %>%
layout(
title = "Layout options in a 3d scatter plot",
scene = list(
xaxis = list(title = bquote('Annual mean' ~CO[2] ~'Flux Difference: \n' ~.(compare)~ 'minus'~.(base))),
yaxis = list(title = "Sin"),
zaxis = list(title = "Z")
))
p
Thank you for your time!
With HTML tags, for example:
yaxis = list(title = "Sin<sup>super</sup>")
More info here. Hope this helps!

Multiple Y-axis on graph not aligned

I created a graph through R's Plotly library.
The graph has 2 y-axis's and everything looks fine, except that the y-axis's are misaligned.
How do I "realign" it?
Python answer here
Had the same issue, add rangemode = "tozero" to your overlaying axis
plot_ly(data = dat,
x = x,
y = y,
type = "bar",
name = "Y") %>%
add_trace(data = par,
x = x,
y = Z,
name = "Z",
yaxis = "y2") %>%
layout(yaxis2 = list(overlaying = "y",
side = "right",
rangemode = "tozero"))
In the layout function, you can set axis ranges manually. You can use this to align them. Often, the scale of your two traces will be very different, though.
plot_ly(...) %>%
add_trace(..., yaxis = "y2") %>%
layout(
yaxis = list(
range = c(-2, 2)
),
yaxis2 = list(
range = c(-2, 2)
)
)
You can actually align primary and secondary y axes at any value (not just at 0) and for any plotting function by calculating new y axis limits. Here's a link to some code for a function that can handle any scenario: https://github.com/davidblakneymoore/A-Function-for-Aligning-Values-on-Primary-and-Secondary-Y-Axes-on-Plots-in-R.
This answer in python took JohnCoene's answer one step further:
https://community.plotly.com/t/align-multiple-y-axis-to-one-value-in-plotly/44500/2
The parameters scaleratio can be seen in further detail in the Plotly manual.
By constraining the left and right y-axes to one another you can adjust the scale at which they increase.
plot_ly(data = dat,
x = x,
y = y,
type = "bar",
name = "Y") %>%
add_trace(data = par,
x = x,
y = Z,
name = "Z",
yaxis = "y2") %>%
layout(yaxis = list(scaleanchor = 'y2',
scaleratio = 1, # Or perhaps 0.5 or 2, dep on your figure
constraintoward = 'bottom',
rangemode = "tozero"),
yaxis2 = list(scaleanchor = 'y',
scaleratio = 1, # Or perhaps 0.5 or 2, dep on your figure
overlaying = "y",
constraintoward = 'bottom'
side = "right",
rangemode = "tozero"))

Resources