Changing margins of a 3d scatter plot using plotly- R - 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))

Related

Adding scatterplot to existing scatterplot in plot_ly in R

I wish to add scatterplots to an existing 3D scatterplot in R using plot_ly.
I am generating data from Normal distribution of length 219. Then I want to plot and add scatter plots corresponding to the indices 80, 150 and 195 of the variables on the same graph with a different color (red in my case).
I used the following codes -
library(plotly)
index <- c(80, 150, 195)
set.seed(4991)
data1 <- rnorm(219,9,1.5)
data2 <- rnorm(219,2,1)
timeline <- 1:length(data1)
plot_ly(x=data1, y=data2, z=timeline, type="scatter3d", mode="markers")%>%
layout(scene = list(xaxis = list(title = 'cases per day'),
yaxis = list(title = 'deaths per day'),
zaxis = list(title = 'observation #')))%>%
add_trace(x=data1[index],y=data2[index],z=timeline[index], type="scatter3d", mode = "markers", marker = list(size = 5, color = "red", symbol = 104))
Snapshot of the output looks like this -
However if I use the color=timeline option in the plot_ly section,
plot_ly(x=data1, y=data2, z=timeline, type="scatter3d", mode="markers",color=timeline)%>%
layout(scene = list(xaxis = list(title = 'cases per day'),
yaxis = list(title = 'deaths per day'),
zaxis = list(title = 'observation #')))%>%
add_trace(x=data1[index],y=data2[index],z=timeline[index], type="scatter3d", mode = "markers", marker = list(size = 5, color = "red", symbol = 104))
I get the following error
Error:
! Tibble columns must have compatible sizes.
• Size 3: Columns `x`, `y`, and `z`.
• Size 219: Column `color`.
ℹ Only values of size one are recycled.
Run `rlang::last_error()` to see where the error occurred.
I want to plot the 3D scatterplot with color=timeline option and then add the scatterplot in red.
Any help is appreciated.
The variable timeline is all unique values, which doesn't align with your desire to have the three values colored. What you need is a grouping variable (i.e., yes or no, a or b, etc.)
I made a control.
timeline1 <- rep("A", length(data1))
timeline1[index] <- "B"
summary(timeline1 %>% as.factor())
# A B
# 216 3
Then I made my graph. One trace- with specific colors designated. I used Plotly's blue to keep it consistent with your question.
# '#1f77b4' is the Plotly blue (muted blue)
plot_ly(x = data1, y = data2, z = timeline, type = "scatter3d", mode = "markers",
color = timeline1, colors = setNames(c('#1f77b4', "red"), nm = c("A", "B"))) %>%
layout(scene = list(xaxis = list(title = 'cases per day'),
yaxis = list(title = 'deaths per day'),
zaxis = list(title = 'observation #')))

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")
)

R Plotly 3D: axis labels with TeX

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 *?

Plotly zerolines at different levels on double axis plot

I'm trying to overlay a line chart and bar chart in plotly (with a vertical line designating an important date) and I'm encountering this issue where the two zero lines are offset instead of on the same line. I've tried messing around with the overlaying = 'y' option within layout and tried changing the order of the three trace components but nothing seems to help. Any ideas how to fix? Below is my code with dummy data:
(Also, bonus points if you can fix my legend-overlapping-y2axis issue)
date <- seq(as.Date("2015/6/1"), by = "month", length.out = 19)
wires_mnth <- c(rep(0,8),100000,750000,1200000,2500000,3100000,5500000,7500000,8000000,9900000,11300000,11000000)
wires_cnt <- c(rep(0,8),100,200,250,325,475,600,750,800,1000,1150,1200)
data <- data.frame(date, wires_mnth)
plot_ly(data) %>%
add_trace(x = ~date, y = ~wires_cnt, type = 'bar', name = 'Wires Count',
yaxis = 'y2', opacity = .5) %>%
add_trace(x = ~date, y = ~wires_mnth, type = 'scatter', mode = 'lines', name
= 'Monthly Wires') %>%
add_trace(x = c(2016,2016), y = c(0, 12000000), type = 'scatter', mode =
"lines", name = 'Sanctions Removed') %>%
layout(title = 'Monthly Aggregate Wire Transfers to Iran',
xaxis = list(title = ''),
yaxis = list(side = 'left', title = 'Wire Amounts (USD)', showgrid =
FALSE, zeroline = FALSE),
yaxis2 = list(side = 'right', overlaying = 'y', title = 'Wires Count',
showgrid = FALSE, zeroline = FALSE)
)
You could add rangemode='nonnegative' to your layout or specify the range manually via range=list(0, max(wires_mnth).
For your bonus question, you can set the x-position of the legend, e.g.
legend = list(x = 1.2)

Resources