I'm using Plotly in R and I want to create a plot like this:
But when trying with fill = "tozeroy" the plot looks like this:
Ignoring the value of y0
So, I want to fill the area from the lines to y0 value, instead to absolute zero.
A code example:
library(plotly)
library(magrittr)
x <- seq.Date(as.Date("2017/01/01"), as.Date("2017/12/31"), by = 1)
y1 <- rnorm(365, 100, 10)
y2 <- rnorm(365, 100, 10)
dat <- data.frame(x, y1, y2)
plot_ly(dat, x = ~x, y = ~y1, mode = "lines", type = "scatter", fill = "tozeroy", y0 = 80, name = "y1") %>%
add_trace(dat, x = ~x, y = ~y2, fill = 'tozeroy', y0 = 80, name = "y2")%>%
layout(xaxis = list(title = ""), yaxis = list(title = ""))
produces:
Ignoring the y0 = 80 parameter.
If I understood correctly you want the range to begin in y0.
To that end, you have to change the yaxis' range in the layout.
This should suffice:
library(plotly)
library(magrittr)
set.seed(19)
x <- seq.Date(as.Date("2017/01/01"), as.Date("2017/12/31"), by = 1)
y1 <- rnorm(365, 100, 10)
y2 <- rnorm(365, 100, 10)
y0 <- 80
ymax <- max(y1,y2)
dat <- data.frame(x, y1, y2)
plot_ly(dat, x = ~x, y = ~y1, mode = "lines", type = "scatter", fill = "tozeroy", y0 = 80, name = "y1") %>%
add_trace(dat, x = ~x, y = ~y2, fill = 'tozeroy', name = "y2")%>%
layout(xaxis = list(title = ""), yaxis = list(range = c(y0, ymax),title = ""))
The range options in plotly are explained here
Related
I would like to plot a sphere in the following plot:
And that is the code for it:
library(plotly)
fig <- plot_ly(mtcars, x = ~wt, y = ~hp, z = ~qsec,
marker = list(color = ~mpg, colorscale = c('#FFE1A1', '#683531'), showscale = TRUE))
fig <- fig %>% add_markers()
fig <- fig %>% layout(scene = list(xaxis = list(title = 'Weight'),
yaxis = list(title = 'Gross horsepower'),
zaxis = list(title = '1/4 mile time')),
annotations = list(
x = 1.13,
y = 1.05,
text = 'Miles/(US) gallon',
xref = 'paper',
yref = 'paper',
showarrow = FALSE
)) %>% add_markers(x = 2.8, y = 120, z = 20, color="red", marker=list(size=30,
opacity = .65,
line=list(width=2,
color='black')))
fig
At the moment my best version only contains a 2D circle. How could I integrate a 3d/wireframe sphere into it using plotly R-version?
A sphere is an isosurface. You can plot an isosurface with plotly but it is a bit slow, I prefer to use the misc3d package.
library(plotly)
f <- function(x, y, z){
x^2 + y^2 + z^2
}
R <- 2 # radius
x <- y <- z <- seq(-R, 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))
library(misc3d)
cont <- computeContour3d(voxel, level = R^2, x = x, y = y, z = z)
idx <- matrix(0:(nrow(cont)-1), ncol=3, byrow=TRUE)
plot_ly(
x = cont[, 1], y = cont[, 2], z = cont[, 3],
i = idx[, 1], j = idx[, 2], k = idx[, 3],
type = "mesh3d"
) %>% layout(scene = list(aspectmode = "data"))
I wrote a code to make a subplots with scatterplots using my data. Here is a graph:
This is hours on x axis. As you see, not all of them appear on x axis. How could i make all 24 hours be on axis? Even if for example in dataframe there is no value for 23 o'clock, i want it to be on x axis. How to do that?
Here is my code:
plot <- function(df) {
subplotList <- list()
for(metric in unique(df$metrics)){
subplotList[[metric]] <- df[df$metrics == metric,] %>%
plot_ly(
x = ~ hr,
y = ~ actual,
name = ~ paste(metrics, " - ", time_pos),
colors = ~ time_pos,
hoverinfo = "text",
hovertemplate = paste(
"<b>%{text}</b><br>",
"%{xaxis.title.text}: %{x:+.1f}<br>",
"%{yaxis.title.text}: %{y:+.1f}<br>",
"<extra></extra>"
),
type = "scatter",
mode = "lines+markers",
marker = list(
size = 7,
color = "white",
line = list(width = 1.5)
),
width = 700,
height = 620
) %>% layout(autosize = T,legend = list(font = list(size = 8)))
}
subplot(subplotList, nrows = length(subplotList), margin = 0.05)
}
This could be achieved in layout via the attribute xaxis like so. The ticks or breaks can be set via tickvals, the tick labels via ticktext.
This is illustrasted using some random data in the reproducible example below:
library(plotly)
set.seed(42)
d <- data.frame(
x = sort(sample(24, 15)),
y = 1:15 + runif(15),
z = 1:15 + runif(15)
)
plot_ly(d) %>%
add_trace(x = ~x, y = ~y, type = "scatter", mode = "lines+markers") %>%
add_trace(x = ~x, y = ~z, type = "scatter", mode = "lines+markers") %>%
layout(xaxis = list(tickvals = 1:24, ticktext = paste0(1:24, "h")))
I have a plot using plotly r . As of now the variations cant be seen that much. So in order to do so if the y-axis is 0-70% is there a way to show just 50%-70% on y axis so the variation can be seen more clearly.
Below is code I am using
output$plot <- renderPlotly({
if (is.null(ab()))
return(NULL)
y <- list(title = "Percentange")
x <- list(title = "Months")
plot_ly(ab(), x = ~ Month_considered, y = ~ pct * 100,type = 'scatter', mode = 'marker',
fill = 'tozeroy', line = list(color = 'rgb(205, 12, 24)', width = 4)) %>%layout(xaxis = x, yaxis = y)})
You don't provide a reproducible example so it is hard to test, but you could try:
y <- list(title = "Percentage", range = c(50,70))
plot_ly(ab(), x = ~ Month_considered, y = ~ pct * 100,type = 'scatter', mode = 'marker', fill = 'tozeroy', line = list(color = 'rgb(205, 12, 24)', width = 4)) %>% layout(xaxis = x, yaxis = y)})
x1 = c(1:10)
y1 = c(1:10)
z1 = matrix(runif(100,0,25),ncol = 10)
z1_col = matrix(runif(100,0,25),ncol = 10)
x2 = c(2:11)
y2 = c(2:11)
z2 = matrix(runif(100,0,100),ncol = 10)
z2_col = matrix(runif(100,0,25),ncol = 10)
x3 = c(3:12)
y3 = c(3:12)
z3 = matrix(runif(100,0,10),ncol = 10)
z3_col = matrix(runif(100,0,25),ncol = 10)
x4 = c(3:12)
y4 = c(3:12)
z4 = matrix(runif(100,0,5),ncol = 10)
z4_col = matrix(runif(100,0,25),ncol = 10)
I am trying to fix the position of the colorbar in plotly. I have already try to fix it by putting in the x and y axis of the color bar for each plot.
plot_ly(type = "surface", colors = c("blue","green","yellow","orange","red")) %>%
add_trace(x = ~ x1, y = ~ y1, z = ~ z1, surfacecolor = ~ z1_col,
colorbar = list(title = "Concentration", x = 1, y = 0.5)) %>%
add_trace(x = ~ x2, y = ~ y2, z = ~ z2, surfacecolor = ~ z2_col,visible = F,
colorbar = list(title = "Concentration", x = 1, y = 0.5)) %>%
add_trace(x = ~ x3, y = ~ y3, z = ~ z3, surfacecolor = ~ z3_col,visible = F,
colorbar = list(title = "Concentration", x = 1, y = 0.5)) %>%
add_trace(x = ~ x4, y = ~ y4, z = ~ z4, surfacecolor = ~ z4_col,visible = F,
colorbar = list(title = "Concentration", x = 1, y = 0.5)) %>%
layout(updatemenus = list(
list(
y = 0.8,
# FOR line, point, to show A, list(T,F,F)
# but for 3D, list (F,T,F)
buttons = list(
list(method = "restyle",
args = list("visible",list(F,T,F,F)),
label = "A"),
list(method = "restyle",
args = list("visible",list(F,F,T,F)),
label = "B"),
list(method = "restyle",
args = list("visible",list(F,F,F,T)),
label = "C"),
list(method = "restyle",
args = list("visible",list(T,F,F,F)),
label = "D")
)
)
))
However, whenever I change to a new plot, the position of the color bar changes (shifting downward).
Is there any way to fix the position of the color bar for all the plot ?
Try moving the colorbar argument outside of add_trace(), so instead of
plot_ly(type = "surface", colors = c("blue","green","yellow","orange","red")) %>%
add_trace(x = ~ x1, y = ~ y1, z = ~ z1, surfacecolor = ~ z1_col,
colorbar = list(title = "Concentration", x = 1, y = 0.5)) ...
do:
plot_ly(type = "surface", colors = c("blue","green","yellow","orange","red")) %>%
add_trace(x = ~ x1, y = ~ y1, z = ~ z1, surfacecolor = ~ z1_col) %>%
colorbar(title = "Concentration", x = 1, y = 0.5) %>% ...
I have these two 3d graphs made in R, with two different functions:
x <- c(0,50,100,150,200,250,300,350,400,450)
y <- c(0,50,100,150,200,250,300,350,400,450)
z <- c(1,2,1,1,2,1,2,1,2,1)
plot_ly(x=x,y=y,z=z)
scatterplot3d(x, y, z)
I would like to plot in these two graphs, the following lines and the intersection area between them:
Lines plotted in both graphs
As per the lines and intersection with plotly, something like this will do:
library(scatterplot3d)
library(plotly)
x <- c(0,50,100,150,200,250,300,350,400,450)
y <- c(0,50,100,150,200,250,300,350,400,450)
z <- c(1,2,1,1,2,1,2,1,2,1)
data <- data.frame(x=x,y=y,z=z)
line <- data.frame(x = rep(200,5), y = seq(0,500,length.out = 5), z = rep(1,5))
line2 <- data.frame(x = rep(300,5), y = seq(0,500,length.out = 5), z = rep(1,5))
rect <- data.frame(expand.grid(x= c(200,300), y =c(200,300)),z = rep(1,4) )
plot_ly() %>%
add_trace(data=data, x=x, y=y, z=z, type="scatter3d", mode="markers") %>%
add_trace(line, x = line$x, y = line$y, z = line$z, type = 'scatter3d', mode = 'lines', line = list(color = 'rgb(1, 1, 1)', width = 1 , dash = 'dash', width = 4)) %>%
add_trace(line2, x = line2$x, y = line2$y, z = line2$z, type = 'scatter3d', mode = 'lines', line = list(color = 'rgb(1, 1, 1)', width = 1, dash = 'dash', width = 4)) %>%
add_trace(line, x = line$y, y = line$x, z = line$z, type = 'scatter3d', mode = 'lines', line = list(color = 'rgb(1, 1, 1)', width = 1 , dash = 'dash', width = 4)) %>%
add_trace(line2, x = line2$y, y = line2$x, z = line2$z, type = 'scatter3d', mode = 'lines', line = list(color = 'rgb(1, 1, 1)', width = 1, dash = 'dash', width = 4))%>%
add_trace(rect, x=rect$x, y=rect$y, z=rect$z, type="mesh3d" )
Result with plotly: