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
Related
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 #')))
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")
)
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))
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 *?
I have a plotly graph in R with a reversed x-axis. I want to be able to play with its ranges. I know it is normal because by design, setting "range" turns "autorange" to "FALSE". https://plot.ly/r/reference/#layout-xaxis-range
But I still need to play with ranges of this reversed axes. Any workarounds, anyone?
Greetings
library(plotly)
s <- seq(1, 8)
plot_ly(x = s, y = s) %>%
add_trace(y = rev(s)) %>%
layout(
xaxis = list(range = c(3,5), autorange="reversed"),
yaxis = list(range = c(2, 5)))
Just retyping my comment above. Using plotly_3.4.3 (development version) and ggplot2_2.1.0. Remove autorange="reversed" from your code and try:
s <- seq(1, 8)
plot_ly(x = s, y = s) %>%
add_trace(y = rev(s)) %>%
layout( xaxis = list(range = c(5,3)), yaxis = list(range = c(2, 5)))