I am trying to set the line type("solid", "dash", "dot" etc) for multiple lines in plotly. I have a column(factor variable) in my data frame which specifies the type of line.
Below is the sample code I am working with.
mydf <- data.frame(x = c(1:10), y1 = c(11:20), y2 = c(21:30))
mydf1 <- gather(mydf,'var', 'val', -x)
mydf1$lt <- factor(c(rep("solid",10),rep("dot",10)))
pal <- RColorBrewer::brewer.pal(nlevels(mydf1$lt),"Set1")
p <-plot_ly(mydf1, x = x, y = val, type = 'line', color = var,colors = pal,line = list(width = 3, dash = lt))
p<- layout(p,title = "Hello", annotations = list(x = mydf1$x, y = mydf1$var))
p
mydf1$lt specifies the required line type.
For the above example, y1 must be solid line and y2 must be dotted line.
I can solve the issue by individually adding lines using add_trace(). I am looking for a more concised, elegant way of doing the same. Below is one possible solution.
p1 <- plot_ly(mydf)
p1 <- add_trace(p1, x = x, y = y1, line = list(dash = "dash"))
p1 <- add_trace(p1, x = x, y = y2, line = list(dash = "solid"))
p1
For the above example, y1 must be solid line and y2 must be dotted
line.
You could do
library(ggplot2)
library(tidyr)
library(plotly)
mydf <- data.frame(x = c(1:10), y1 = c(11:20), y2 = c(21:30))
mydf1 <- gather(mydf,'var', 'val', -x)
mydf1$lt <- factor(c(rep("solid",10),rep("dot",10)))
pal <- RColorBrewer::brewer.pal(nlevels(mydf1$lt),"Set1")
p <-plot_ly(
transform(mydf1, lt=c(solid="solid", dot="3")[lt]),
x = x,
y = val,
type = 'line',
color = var,
colors = pal,
line = list(dash = lt)
)
p <- layout(
p,
title = "Hello",
annotations = list(x = mydf1$x, y = mydf1$var)
)
p
Or c(solid="3", dot="solid")[lt] if you want it the other way around.
Related
I have continuous data that I'd like to plot using R's plotly with a box or violin plot without the outliers and whiskers:
set.seed(1)
df <- data.frame(group=c(rep("g1",500),rep("g2",700),rep("g3",600)),
value=c(c(rep(0,490),runif(10,10,15)),abs(rnorm(700,1,10)),c(rep(0,590),runif(10,10,15))),
stringsAsFactors = F)
df$group <- factor(df$group, levels = c("g1","g2","g3"))
I know how to remove outliers in plotly:
plotly::plot_ly(x = df$group, y =df$value, type = 'box', color = df$group, boxpoints = F, showlegend = F)
But I'm still left with the whiskers.
I tried using ggplot2 for that (also limiting the height of the y-axis to that of the 75 percentile):
library(ggplot2)
gp <- ggplot(df, aes(group, value, color = group, fill = group)) + geom_boxplot(outlier.shape = NA, coef = 0) +
scale_y_continuous(limits = c(0, ceiling(max(dplyr::summarise(dplyr::group_by(df, group), tile = quantile(value, probs = 0.75))$tile)))) +
theme_minimal() + theme(legend.position = "none",axis.title = element_blank())
But then trying to convert that to a plotly object doesn't maintain that:
plotly::ggplotly(gp)
Any idea?
This is a workaround.
I changed your plot a bit, first.
# box without outliers
p <- plot_ly(df, x = ~group, y = ~value, type = 'box',
color = ~group, boxpoints = F, showlegend = F,
whiskerwidth = 0, line = list(width = 0)) # no whisker, max or min line
Then I add the medians back to the graph. This requires calculating the medians, matching the colors, and creating the shape lists for Plotly.
For the colors, it's odd, the first three default colors are used, but the order is g3, g2, g1...
# the medians
res = df %>% group_by(group) %>%
summarise(med = median(value))
# default color list: https://community.plotly.com/t/plotly-colours-list/11730/2
col = rev(c('#1f77b4', '#ff7f0e', '#2ca02c')) # the plot is colored 3, 2, 1
# discrete x-axis; domain default [0, 1]
# default box margin = .08, three groups, each get 1/3 of space
details <- function(col){ # need everytime basics
list(type = 'line',
line = list(color = col, width = 4),
xref = "paper", yref = "y")
}
# horizontal segments/ median
segs = lapply(1:nrow(res),
function(k){
x1 <- k/3 - .08 # if the domain is [0, 1]
x0 <- (k - 1)/3 + .08
y0 <- y1 <- res[k, ]$med
line = list("x0" = x0, "x1" = x1,
"y0" = y0, "y1" = y1)
deets = details(col[k])
c(deets, line)
})
Finally, I added them back onto the plot.
p %>% layout(shapes = segs)
I made the lines obnoxiously wide, but you get the idea.
If you wanted the IQR outline back, you could do this, as well. I used functions here, as well. I figured that the data you've provided is not the actual data, so the function will serve a purpose.
# include IQR outline
res2 = df %>% group_by(group) %>%
summarise(q1 = setNames(quantile(value, type = 7, 1/4), NULL),
q3 = setNames(quantile(value, type = 7, 3/4), NULL),
med = median(value))
# IQR segments
rects = lapply(1:nrow(res2), # if the domain is [0, 1]
function(k){
x1 <- k/3 - .08
x0 <- (k - 1)/3 + .08
y0 <- res2[k, ]$q1
y1 <- res2[k, ]$q3
line = list(color = col[k], width = 4)
rect = list("x0" = x0, "x1" = x1,
"y0" = y0, "y1" = y1,
type = "rect", xref = "paper",
yref = "y", "line" = line)
rect
})
rects = append(segs, rects)
p %>% layout(shapes = rects)
I want to draw a bar chart with two bars side by side for each category (LETTERS) and add a horizontal line to each bar (y_ref as red lines & z_ref as green lines). It should look like this:
I tried it with add_trace (and add_lines, add_segment, etc) but can't find the correct way to make it work. Here's one of the reprex's I tried so far.
df <- tibble(x = LETTERS[1:5], y = runif(5), z = runif(5), y_ref = runif(5), z_ref = runif(5))
plot_ly(
df,
x = ~x,
y = ~y,
type = "bar",
name = "a"
) %>% add_trace(
y = ~z,
name = "b"
) %>% layout(
legend = list(
orientation = "h"
)
) %>% add_trace(
y = ~y_ref,
type = 'scatter',
mode = 'lines',
marker = list(
line = list(
width = 2,
color = "red"
)
)
) %>% add_trace(
y = ~z_ref,
type = 'scatter',
mode = 'lines',
marker = list(
line = list(
width = 2,
color = "green"
)
)
)
Edit: I need a solution for n bars.
You have to use shapes for line segments in Plotly. Additionally, because the x-axis is discrete, the x for the shapes will need to be in paper space.
When working in paper space, you use the plot domain to figure out the values for x. Plotly has a default domain set to [0, 1] for both x and y axes.
There is a gap between bars; that has to be accounted for as well. I used three functions to create the line segments, using all of this information.
Lastly, I used a seed for repeatability.
Libraries, seed, and base plot
library(tidyverse)
library(plotly)
set.seed(8)
df <- tibble(x = LETTERS[1:5], y = runif(5), z = runif(5),
y_ref = runif(5), z_ref = runif(5))
p = plot_ly(df, x = ~x, y = ~y,
type = "bar", name = "a") %>%
add_trace(y = ~z, name = "b")
Create the segments
# standard shape elements, call for color
details <- function(col){
list(type = 'line',
line = list(color = col),
xref = "paper", yref = "y")
}
# green lines for orange bars
segs = lapply(1:nrow(df),
function(k){
x1 <- (k + k)/10 - .02 # if the domain is [0, 1]
x0 <- x1 - .08
y0 <- y1 <- df[k, ]$z_ref
line = list("x0" = x0, "x1" = x1,
"y0" = y0, "y1" = y1)
deets = details("green")
c(deets, line)
})
# green lines for blue bars
segs2 = lapply(1:nrow(df),
function(j){
x1 <- (j + j - 1)/10 # if the domain is [0, 1]
x0 <- x1 - .08
y0 <- y1 <- df[j, ]$y_ref
line = list("x0" = x0, "x1" = x1,
"y0" = y0, "y1" = y1)
deets = details("red")
c(deets, line)
})
segs = append(segs, segs2)
Put it together
p %>% layout(legend = list(orientation = "h"),
shapes = segs)
I want to show multiple lines being added to a plotly plot (as an animation) using R. For example, I have the following plotly line graphs (p, p2, p3):
library(plotly)
set.seed(3)
x = 1:10
y = 1:10
y2 = y^2
y3 = y^3
p = plot_ly(data = data.frame(x = x, y = y), x = ~ x, y = ~y, type = "scatter", mode = "lines")
p2 = plot_ly(data = data.frame(x = x, y = y2), x = ~ x, y = ~y2, type = "scatter", mode = "lines")
p3 = plot_ly(data = data.frame(x = x, y = y3), x = ~ x, y = ~y3, type = "scatter", mode = "lines")
Here p, p2, p3 are different plots but they all have the same x axis and different y axis. I want to be able to make an animation where the lines y, y2, y3 will successively appear in the plotly graph.
P.S: It does not strictly have to be done using plotly, but strongly preferred.
An idea might be to create a 'dataset' for each frame.
The first frame contains all values for y and all values for y2 and y3 are located outside the y-axis limits. For the second frame all values from y and y2 are shown and just the values from y3 are beyond the limit. In frame 3 all values are included.
library(tidyverse)
library(plotly)
# transform dataframe into a long format
df <- data.frame(x = 1:10,
y = 1:10) %>%
mutate(y2 = y^2,
y3 = y^3) %>%
pivot_longer(cols = -x,
names_to = "line",
values_to = "value")
# set the values for each frame and line
# (all lines not shown, need to hidden outside the plot limits - NA won't work)
df_plot <- map_df(1:3, ~ mutate(df, frame = .)) %>%
mutate(value = case_when(frame == 1 & line %in% c("y2", "y3") ~ -10,
frame == 2 & line %in% c("y3") ~ -10,
TRUE ~ value))
# create plot
plot_ly(data = df_plot,
x = ~x,
y = ~value,
color = ~line,
type = "scatter",
mode = "line",
frame = ~frame) %>%
layout(yaxis=list(range = c(0, 1000))) %>%
animation_opts(easing = "bounce")
Similar to the question here but this didn't give me excatly what I needed and I couldn't figure it out: Plot ellipse3d in R plotly?. I want to recreate rgl's ellipse3d and surface ellipsoid in plotly. I know there there was an anwer which allowed plotting of an ellipse but as individual opaque markers, I need to get it as a surface ellipsoid that's slightly opaque so I can still see the data points in the ellipsoid.
I tried to figure out how dww's comment for "add_surface" instead works but couldn't figure it out.... Can anyone help please?
if (!require("rgl")) install.packages("rgl")
dt <- cbind(x = rnorm(100), y = rnorm(100), z = rnorm(100))
ellipse <- ellipse3d(cov(dt))
plot3d(dt)
plot3d(ellipse, add = T, color = "red", alpha = 0.5)
dww's answer was:
if (!require("plotly")) install.packages("plotly")
if (!require("rgl")) install.packages("rgl")
dt <- cbind(x = rnorm(100), y = rnorm(100), z = rnorm(100))
ellipse <- ellipse3d(cov(dt))
p <- plot_ly(mode = 'markers') %>%
add_trace(type = 'scatter3d', size = 1,
x = ellipse$vb[1,], y = ellipse$vb[2,], z = ellipse$vb[3,],
opacity=0.01) %>%
add_trace(type = 'scatter3d', x = dt[,1], y = dt[,2], z = dt[,3])
p
# shows more obviously what dww's code does to create the visual ellipsoid
w <- plot_ly(mode = 'markers') %>%
add_trace(type = 'scatter3d',
x = ellipse$vb[1,], y = ellipse$vb[2,], z = ellipse$vb[3,],
opacity=0.5) %>%
add_trace(type = 'scatter3d', x = dt[,1], y = dt[,2], z = dt[,3])
w
Their comment on how to use add_surface was
Note that for simplicity, I plotted the ellipse as a cloud using markers. If you want to use add_surface instead, you will have to first convert the ellipse into a different format, with a vector of x locations, a vector of y locations, z as a matrix (dimensions equal to x by y). You'll also need to split the z values into two separate surface layers one for the top half of the ellipsoid and one for the bottom. I don't have time right now to do all this, but if you get stuck I can work this out later
This is my solution if anyone is interested in it. This allows using of the buttons in plotly to toggle the ellipsoid on and off so that you can still hover over and select data points inside the ellipsoid when desired:
if (!require("rgl")) install.packages("rgl", dependencies=TRUE, repos="http://cran.rstudio.com/")
if (!require("plotly")) install.packages("plotly", dependencies=TRUE, repos="http://cran.rstudio.com/")
dt <- cbind(x = rnorm(100), y = rnorm(100), z = rnorm(100))
ellipse <- ellipse3d(cov(dt))
updatemenus <- list(
list(
active = 0,
type= 'buttons',
buttons = list(
list(
label = "Ellipsoid",
method = "update",
args = list(list(visible = c(TRUE, TRUE)))),
list(
label = "No Ellipsoid",
method = "update",
args = list(list(visible = c(TRUE, FALSE)))))
)
)
plot<- plot_ly()%>%
# Plot raw scatter data points
add_trace(data = dt, x = dt[,1], y = dt[,2], z = dt[,3],
type = "scatter3d", mode = 'markers', marker = list(size = 3)) %>%
# Plot ellipsoid
add_trace(x=ellipse$vb [1,], y=ellipse$vb [2,], z=ellipse$vb [3,],
type='mesh3d', alphahull = 0, opacity = 0.4)%>%
# Axes Titles
layout(updatemenus = updatemenus)
plot
Here is a possibility, using the mesh3d type, and with the help of the misc3d package.
pts <- cbind(x = rnorm(10), y = rnorm(10), z = rnorm(10))
C <- chol(cov(pts))
SVD <- svd(t(C))
A <- solve(t(SVD$u)) %*% diag(SVD$d)
cr <- colMeans(pts)
r <- sqrt(qchisq(0.95,3))
fx <- function(u,v){
cr[1] + r*(A[1,1]*cos(u)*cos(v) + A[1,2]*cos(u)*sin(v) + A[1,3]*sin(u))
}
fy <- function(u,v){
cr[2] + r*(A[2,1]*cos(u)*cos(v) + A[2,2]*cos(u)*sin(v) + A[2,3]*sin(u))
}
fz <- function(u,v){
cr[3] + r*(A[3,1]*cos(u)*cos(v) + A[3,2]*cos(u)*sin(v) + A[3,3]*sin(u))
}
library(misc3d)
tris <- parametric3d(fx, fy, fz,
umin=-pi/2, umax=pi/2, vmin=0, vmax=2*pi,
n=100, engine="none")
n <- nrow(tris$v1)
cont <- matrix(NA_real_, ncol=3, nrow=3*n)
cont[3*(1:n)-2,] <- tris$v1
cont[3*(1:n)-1,] <- tris$v2
cont[3*(1:n),] <- tris$v3
idx <- matrix(0:(3*n-1), ncol=3, byrow=TRUE)
library(plotly)
p <- plot_ly() %>%
add_trace(type = "mesh3d",
x = cont[,1], y = cont[,2], z = cont[,3],
i = idx[,1], j = idx[,2], k = idx[,3],
opacity = 0.3) %>%
add_trace(type = "scatter3d", mode = "markers",
data = as.data.frame(pts),
x = ~x, y = ~y, z = ~z,
marker = list(size = 5)) %>%
layout(scene = list(aspectmode = "data"))
To add some colors:
midpoints <- (tris$v1 + tris$v2 + tris$v3)/3
distances <- apply(midpoints, 1, function(x) crossprod(x-cr))
intervals <- cut(distances, 256)
colorsPalette <- viridisLite::viridis(256)
colors <- colorsPalette[as.integer(intervals)]
p <- plot_ly() %>%
add_trace(type = "mesh3d",
x = cont[,1], y = cont[,2], z = cont[,3],
i = idx[,1], j = idx[,2], k = idx[,3],
facecolor = colors,
opacity = 0.3) %>%
add_trace(type = "scatter3d", mode = "markers",
data = as.data.frame(pts),
x = ~x, y = ~y, z = ~z,
marker = list(size = 5)) %>%
layout(scene = list(aspectmode = "data"))
Another solution with the Rvcg package. We use the vcgSphere function which generates a triangulated sphere.
sphr <- Rvcg::vcgSphere() # triangualted sphere
library(rgl) # to use scale3d and transform3d
ell <- scale3d(transform3d(sphr, A), r, r, r)
vs <- ell$vb[1:3,] + cr
idx <- ell$it - 1
p <- plot_ly() %>%
add_trace(type="mesh3d",
x = vs[1,], y = vs[2,], z = vs[3,],
i = idx[1,], j = idx[2,], k = idx[3,],
opacity = 0.3) %>%
add_trace(type = "scatter3d", mode = "markers",
data = as.data.frame(pts),
x = ~x, y = ~y, z = ~z,
marker = list(size = 5)) %>%
layout(scene = list(aspectmode = "data"))
I have an histogram like the one below and I want to plot a vertical line of a specific value on top of the histogram.
My code for the histogram is:
library(plotly)
p <- plot_ly(x = ~rnorm(50,mean = 50,sd = 10), type = "histogram")
p
I want to overlay a line on top of the histogram, for example say,
x <- 42
Can anyone help getting this?
library(plotly)
set.seed(1)
p <- plot_ly(x = ~rnorm(50,mean = 50,sd = 10), type = "histogram") %>%
add_segments(x=42, y=0, xend=42, yend=14, line=list(color="red", width = 4))
p
A different solution (see here for details):
vline <- function(x = 0, color = "red") {
list(
type = "line",
y0 = 0,
y1 = 1,
yref = "paper",
x0 = x,
x1 = x,
line = list(color = color)
)
}
p <- plot_ly(x = ~rnorm(50, mean = 50,sd = 10), type = "histogram") %>%
layout(shapes = list(vline(42)))
p