Here is my data:
set.seed(42)
mydata = data.frame(A = rnorm(20), B = rnorm(20), Index = sample(190:400,20))
I am trying to divide the data into 20 different intervals based on the Index value and then color the scatter points according to their interval value. Below is my code. It is not working perfectly.
cols = colorRampPalette(c("red", "black"), space = "rgb")(20)
mydata$interval = cut(mydata$Index,breaks = 20)
mydata$cols = cols[mydata$interval]
require(plotly)
x = list(title = "A")
y = list(title = "B")
plot_ly(mydata, x = ~A, y = ~B, color = ~cols, type = "scatter",
mode = 'markers', hoverinfo = 'text',
text = ~paste(interval)) %>%
layout(xaxis = x, yaxis = y)
How do I get a colorbar in the legend where the colors are based on Index value.
Are you looking for this:
plot_ly(mydata, x = ~A, y = ~B, type = "scatter",
mode = 'markers', hoverinfo = 'text', colors = colorRampPalette(c("red", "black"), space = "rgb")(20), color = ~Index, text = ~paste(interval), marker = list(size=14)) %>%
layout(xaxis = x, yaxis = y) %>%
colorbar(title = "My Legend")
Related
The dynamic hoverlabel background color in R plotly does not seem to work when using scattergl instead of scatter as depicted in the example below.
Works as intended with type = "scatter":
library(plotly)
X <- data.frame(x = 1:6,
y = 1:6,
z = 1:6)
plot_ly(data = X, x = ~x, y = ~y,
type = "scatter", mode = "markers",
marker = list(color = ~x,
colorscale = list(c(0, .5, 1), c('#0d71db', "#dbc00d", "#db220d"))))
The hoverlabel background color becomes black for all data points with type = "scattergl":
plot_ly(data = X, x = ~x, y = ~y,
type = "scattergl", mode = "markers",
marker = list(color = ~x,
colorscale = list(c(0, .5, 1), c('#0d71db', "#dbc00d", "#db220d"))))
I guess a solution could be to pass the same color array used in colorscale to the bgcolor argument via hoverlabel = list(bgcolor = ???). However I have no idea how to do so.
Edit
Tried this based on #Quinten's answer, without success. As can be seen the colors are the default plot_ly colors and do not correspond to what is specified in cols.
library(plotly)
n <- 5000
X <- data.frame(x = sample(1:100, n, replace = TRUE),
y = sample(1:100, n, replace = TRUE),
z = sample(1:500, n, replace = TRUE))
length_unique_vals <- length(unique(X$z))
cols <- colorRampPalette(c('#0d71db', "#dbc00d", "#db220d"))(length_unique_vals)
cols <- cols[factor(X$z)]
plot_ly(data = X, x = ~x, y = ~y,
type = "scattergl", mode = "markers",
marker = list(color = ~z,
colorscale = cols,
colorbar = list(title = "Colorbar")),
hoverlabel = list(bgcolor = cols)) %>%
toWebGL()
You can create a vector of 6 different colors using RColorBrewer. These colors you assign to the color of your marker and to the bgcolor of your hoverlabel which will show the right color. You can use the following code:
library(plotly)
X <- data.frame(x = 1:6,
y = 1:6,
z = 1:6)
library(RColorBrewer)
cols <- brewer.pal(n = 6, name = "Set3")
plot_ly(data = X, x = ~x, y = ~y,
type = "scattergl", mode = "markers",
marker = list(color = cols),
hoverlabel = list(bgcolor = cols))
Output:
As you can see from the plot, the label is the same color as the marker.
I am trying to reproduce a graph generated via ggplotly with plot_ly. I am struggling however with the colorbar.
This is the ggploty plot that I would like to reproduce, and in particular the colorbar:
library(plotly)
X <- data.frame(w = rep(c("a", "b"), each = 8),
x = 1:16,
y = 1:16,
z = c(1, 1:13, 13, 13))
X$z_scaled <- (X$z-min(X$z))/(max(X$z)-min(X$z)) # scale to 0-1
# ggplot
gg <- ggplot(X) +
geom_point(aes(x, y, color = z_scaled, alpha = w, text = paste0(x, ", ", y))) +
scale_color_gradient2(low = '#0d71db', mid = "#dbc00d", high = '#db220d',
midpoint = .5, breaks = 0:1, limits = 0:1) +
scale_alpha_manual(name = " ", values = rep(1, nrow(X))) +
labs(color = "Z", x = "", y = "")
ggplotly(gg, type = "scattergl", tooltip = "text") %>% toWebGL()
This is what I have with plot_ly:
length_unique_vals <- length(unique(X$z))
.colors <- colorRampPalette(c('#0d71db', "#dbc00d", "#db220d"))(length_unique_vals)
.colors <- .colors[factor(X$z)]
plot_ly() %>%
add_markers(
data = X, x = ~x, y = ~y,
split = ~w,
text = ~paste0(x, ", ", y),
hoverinfo = "text",
type = "scattergl",
mode = "markers",
marker = list(
# color = ~z_scaled,
color = .colors,
# colorscale = list(c(0, .5, 1), c("#0d71db", "#dbc00d", "#db220d")),
colorscale = .colors,
hoverlabel = list(bgcolor = .colors),
colorbar = list(
title = list(text = "Z"),
len = .5,
x = 1,
y = .7
)
)
) %>%
layout(
legend = list(x = 1, y = .4, bgcolor = 'rgba(255,255,255,0.6)')
) %>% toWebGL() %>% partial_bundle(local = FALSE)
As you can see, the colorbar is not displaying correctly. I have tried multiple possibilities (commented above) without success. What am I missing?
Edit
#Kat's answer solves the colorbar issue. However, if you want to use scattergl or toWebGl you will need to fix the hoverlabel background so it remains dynamic. Here is a solution for that below building on her answer.
length_unique_vals <- length(unique(X$z))
.colors <- colorRampPalette(c('#0d71db', "#dbc00d", "#db220d"))(length_unique_vals)
.colors <- .colors[factor(X$z)]
plot_ly() %>%
add_trace(x = ~x,
y = ~y,
split = ~w, # instead of alpha or opacity
data = X,
type = "scattergl",
mode = "markers",
color = ~z_scaled, # color = var and colors = literal colors
colors = c('#0d71db', "#dbc00d", '#db220d'),
hoverlabel = list(bgcolor = .colors)) %>% # Fix hovercolor bg
layout(xaxis = list(title = "",
dtick = 4,
zeroline = F,
gridcolor = "white"), # white on gray
yaxis = list(title = "",
dtick = 4,
zeroline = F,
gridcolor = "white"), # white on gray
plot_bgcolor = "#eeeeee") %>% # gray background
colorbar(title = "Z", # colorbar title
dtick = c(0, 1), # colorbar ticks
thickness = 25) %>% # width
toWebGL() %>% partial_bundle(local = FALSE)
Edit 2
The hoverlabel bgcolor breaks down then the split factor is not ordered. This is why it needs to be ordered first.
library(plotly)
library(data.table)
X <- data.table(w = rep(c("a", "b"), 8), #not ordered
x = 1:16,
y = 1:16,
z = c(1, 1:13, 13, 13))
X[, z_scaled := (X$z-min(X$z))/(max(X$z)-min(X$z))] # scale to 0-1
# Get colors for hoverlabel bgcolor
X <- X[order(w)]
length_unique_vals <- length(unique(X$z))
.colors <- colorRampPalette(c('#0d71db', "#dbc00d", "#db220d"))(length_unique_vals)
.colors <- .colors[factor(X$z)]
plot_ly() %>%
add_trace(x = ~x,
y = ~y,
split = ~w, # instead of alpha or opacity
data = X,
type = "scattergl",
mode = "markers",
color = ~z_scaled, # color = var and colors = literal colors
colors = c('#0d71db', "#dbc00d", '#db220d'),
hoverlabel = list(bgcolor = .colors),
marker = list(size = 10)) %>% # Fix hovercolor bg
layout(xaxis = list(title = "",
dtick = 4,
zeroline = F,
gridcolor = "white"), # white on gray
yaxis = list(title = "",
dtick = 4,
zeroline = F,
gridcolor = "white"), # white on gray
plot_bgcolor = "#eeeeee") %>% # gray background
colorbar(title = "Z", # colorbar title
dtick = c(0, 1), # colorbar ticks
thickness = 25) %>% # width
toWebGL() %>% partial_bundle(local = FALSE)
Assuming you even wanted the gray background, this should work. If something isn't clear or not what you were looking for, let me know.
You don't even need the text, because you specified the default hover content.
plot_ly() %>%
add_trace(x = ~x,
y = ~y,
split = ~w, # instead of alpha or opacity
data = X,
type = "scatter",
mode = "markers",
color = ~z_scaled, # color = var and colors = literal colors
colors = c('#0d71db', "#dbc00d", '#db220d')) %>%
layout(xaxis = list(title = "",
dtick = 4,
zeroline = F,
gridcolor = "white"), # white on gray
yaxis = list(title = "",
dtick = 4,
zeroline = F,
gridcolor = "white"), # white on gray
plot_bgcolor = "#eeeeee") %>% # gray background
colorbar(title = "Z", # colorbar title
dtick = c(0, 1), # colorbar ticks
thickness = 25) # width
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'd like to do plotly chart and plot filled area shape and bars on one plot. However area shape overlaying bars. I couldn't change order of elements. Is it possible to bring bars in fron?
data <- data.frame(years = c(2005,2006,2007), values1 = c(1,2,3), values2 = c(3,3,2))
plot_ly(data, x = data$years, y=data$values1, type = 'bar') %>%
add_trace(x=data$years, y=data$values2, type = 'scatter', mode = 'lines', fill = 'tozeroy')
This is adapted from the answer by #Maximilian Peters. This code
data <- data.frame(years = c(2005,2006,2007), values1 = c(1,2,3), values2 = c(3,3,2))
plot_ly(data) %>%
add_trace(x=~years, y=~values1, type = 'bar') %>%
add_trace( x = ~years, y=~values2, type = 'scatter', mode = 'lines', fill = 'tozeroy', yaxis='y2'
) %>%
layout(title = 'Trace order Plotly R',
xaxis = list(title = ""),
yaxis = list(side = 'left', title = 'Y - Axis', overlaying = "y2"),
yaxis2 = list(side = 'right', title = "" )
)
generates this output:
I'm creating a R plotly boxplot for these data:
set.seed(1)
df <- data.frame(value = rnorm(100),
value.error. = runif(100,0.01,0.1),
treatment = rep(LETTERS[1:10], 10),
replicate = rep(1:10, 10), stringsAsFactors = F)
df$treatment <- factor(df$treatment)
Where in each box I add the replicates as points:
library(dplyr)
plotly::plot_ly(x = df$treatment, split = df$treatment, y = df$value,
type = "box", showlegend = F, color = df$treatment,
boxpoints = F, fillcolor = 'white') %>%
plotly::add_trace(x = df$treatment, y = df$value, type = 'scatter', mode = "markers",
marker = list(size = 8), showlegend = F, color = df$treatment)
Which gives:
Now I'd like to add vertical error bars to each point (according to df$value.error).
This:
plotly::plot_ly(x = df$treatment, split = df$treatment, y = df$value,
type = "box", showlegend = F, color = df$treatment,
boxpoints = F, fillcolor = 'white') %>%
plotly::add_trace(x = df$treatment, y = df$value, type = 'scatter', mode = "markers",
marker = list(size = 8), showlegend = F, color = df$treatment) %>%
plotly::add_trace(error_y = list(array = df$sd), showlegend = F)
Gives me the same plot above.
However, if I only plot the points and add their errors using:
plotly::plot_ly(x = df$treatment, y = df$value,
type = 'scatter', mode = "markers",
marker = list(size = 8), showlegend = F, color = df$treatment) %>%
plotly::add_trace(error_y =list(array = df$sd), showlegend = F)
I do get the points with their vertical error bars:
So my question is how to have the box + points + error bars to work?
And, if the solution can also combine jittering the points with their error bars that will be even better.
You can add the box plot after plotting the points and error bars.
library(plotly)
plot_ly(data = df,
x = ~treatment, y = ~value,
type = 'scatter', mode = "markers",
marker = list(size = 8), showlegend = F, color = df$treatment) %>%
add_trace(error_y =list(array = ~value.error.), showlegend = F) %>%
add_boxplot(x = ~treatment, split = ~treatment, y = ~value,
showlegend = F, color = ~treatment,
boxpoints = F, fillcolor = 'white')
Data:
set.seed(1)
df <- data.frame(value = rnorm(100),
value.error. = runif(100,0.01,0.1),
treatment = rep(LETTERS[1:10], 10),
replicate = rep(1:10, 10),
stringsAsFactors = F)
df$treatment <- factor(df$treatment)