Plotly not showing legend for two confidence intervals in R - r

I am using plotly (version 4.6.0) in R (version 3.4) to create two lines with confidence intervals around them. The legend is not showing. Anyone have a guess what's up?
Here's the plot:
It seems that the legend switch is being ignored. It is false for the filled (confidence intervals) and true for the main plots. Turning them all to true gives six legend entries, but I only want two.
Here's the code:
plot_ly(x = ~observed$time, y = ~observed$interval_upper,
type = 'scatter',
mode = 'lines',
line = list(color = 'transparent'),
showlegend = FALSE,
name = 'Upper bound')
%>% add_trace(x = ~observed$time, y = ~observed$interval_lower,
type = 'scatter',
mode = 'lines',
fill = 'tonexty',
fillcolor='rgba(255,127,14,0.2)',
line = list(color = 'transparent'),
showlegend = FALSE,
name = 'Lower bound')
%>% add_trace(x = ~observed$time, y = ~observed$observed_power,
type = 'scatter',
mode = 'lines',
line = list(color='rgb(255,127,14)'),
showlegend = TRUE,
name = 'Observed')
%>% add_trace(x = ~forecast$time, y = ~forecast$interval_upper,
type = 'scatter',
mode = 'lines',
line = list(color = 'transparent'),
showlegend = FALSE,
name = 'Upper bound')
%>% add_trace(x = ~forecast$time, y = ~forecast$interval_lower,
type = 'scatter',
mode = 'lines',
fill = 'tonexty',
fillcolor='rgba(31,119,180,0.2)',
line = list(color = 'transparent'),
showlegend = FALSE,
name = 'Lower bound')
%>% add_trace(x = ~forecast$time, y = ~forecast$baseline_power,
type = 'scatter',
mode = 'lines',
line = list(color='rgb(31,119,180)'),
showlegend = TRUE,
name = 'Forecast')
%>% layout(legend = list(x = 0.80, y = 0.90))

The first showlegend in plot_ly should always be TRUE,otherwise it will mask the others, try to swap the traces.
This example take from plotly website show the issue
(https://plot.ly/r/legend/)
library(plotly)
library(tidyr)
library(plyr)
data <- spread(Orange, Tree, circumference)
data <- rename(data, c("1" = "Tree1", "2" = "Tree2", "3" = "Tree3", "4" = "Tree4", "5" = "Tree5"))
#hiding entries
p <- plot_ly(data, x = ~age, y = ~Tree1, type = 'scatter', mode = 'lines', name = 'Tree 1') %>%
add_trace(y = ~Tree2, name = 'Tree 2') %>%
add_trace(y = ~Tree3, name = 'Tree 3', showlegend = FALSE) %>%
add_trace(y = ~Tree4, name = 'Tree 4') %>%
add_trace(y = ~Tree5, name = 'Tree 5')
##no legend
p <- plot_ly(data, x = ~age, y = ~Tree1, type = 'scatter', mode = 'lines', name = 'Tree 1',showlegend = FALSE) %>%
add_trace(y = ~Tree2, name = 'Tree 2') %>%
add_trace(y = ~Tree3, name = 'Tree 3', showlegend = TRUE) %>%
add_trace(y = ~Tree4, name = 'Tree 4') %>%
add_trace(y = ~Tree5, name = 'Tree 5')

Related

Is there a way to not display hover values for add_trace values in R plotly?

I was wanting to know if there is a way of not showing the add_trace values on the plotly graph output? The code below produces a series of cumulative annual line graphs. The add_trace addition was inserted to add a dashed vertical line to the graph. However, when I hover over the graphic it shows the values for the vertical line - can this be turned off, but allow hover values for the cumulative annual lines? I was hoping to do this with a tweak of the code below.
Thanks.
df %>%
filter(op_area_ocode == 'ALL') %>% mutate(Year = as.character(R.Year), DoY = as.Date(DoY.N, origin = "1972-01-01") - 1) %>%
plot_ly(., x = ~ DoY, y = ~cni, color = ~Year, colors = c("#D3D3D3","#D3D3D3","#D3D3D3","#D3D3D3","black","#E69F00","#56B4E9", "#009E73", "#AA4499"), type = 'scatter', mode = 'lines') %>%
layout(title = paste("Reports"), xaxis = list(title = "", type = "date", tickformat = "%d %b"), yaxis = list (title = "Number of reports")) %>%
add_trace(x = as.Date("1972-08-15"),type = 'scatter', mode = 'lines', line = list(color = 'red', width=0.5, dash ="dot"),name = '', showlegend = FALSE)
You should be able to use hoverinfo='none' in your add_trace() call
df %>%
filter(op_area_ocode == 'ALL') %>% mutate(Year = as.character(R.Year), DoY = as.Date(DoY.N, origin = "1972-01-01") - 1) %>%
plot_ly(., x = ~ DoY, y = ~cni, color = ~Year, colors = c("#D3D3D3","#D3D3D3","#D3D3D3","#D3D3D3","black","#E69F00","#56B4E9", "#009E73", "#AA4499"), type = 'scatter', mode = 'lines') %>%
layout(title = paste("Reports"), xaxis = list(title = "", type = "date", tickformat = "%d %b"), yaxis = list (title = "Number of reports")) %>%
add_trace(x = as.Date("1972-08-15"),
type = 'scatter',
mode = 'lines',
line = list(color = 'red', width=0.5, dash ="dot"),
name = '',
showlegend = FALSE,
hoverinfo="none"
)

Overlaying scatter area in plot_ly

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:

Add error bars to points within a plotly box plot

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)

R Plotly Colorbar Disappears with Buttons

I have a toy example below where I have a scatterplot that stays on the same x and y coordinates but I'd like to color the scatter points by the values in different columns. When I click on "Petal.Width" the correct scatter point colors appear but the colorbar disappears. Ideally I'd like the title of the color bar to be the same as the clicked button and the scale of the color bar to change with each clicked button.
updatemenus <- list(
list(
active = -1,
type= 'buttons',
buttons = list(
list(
label = "Petal.Length",
method = "update",
args = list(list(visible = c(FALSE, TRUE)))),
list(
label = "Petal.Width",
method = "update",
args = list(list(visible = c(TRUE, FALSE))))
)
)
)
iris %>%
plot_ly(type = "scatter",
mode = 'markers') %>%
add_trace(x = ~Sepal.Length,
y = ~Sepal.Width,
color = ~Petal.Length,
visible = TRUE,
name = "Petal.Length") %>%
add_trace(x = ~Sepal.Length,
y = ~Sepal.Width,
color = ~Petal.Width,
visible = FALSE,
name = "Petal.Width") %>%
layout(updatemenus=updatemenus)
Here is how the plot is displayed when i run my code
Here it is when I click "Petal.Width"
I will answer in case someone finds this issue in the future:
What worked for me is specifying some arguments for colorbar in both traces (add_trace).
You would end with something like this:
iris %>%
plot_ly(type = "scatter",
mode = 'markers') %>%
add_trace(x = ~Sepal.Length,
y = ~Sepal.Width,
color = ~Petal.Length,
visible = TRUE,
name = "Petal.Length",
colorbar= list(thicknes=20,
len = 0.35,
x=0.05,
y=0.35)
) %>%
add_trace(x = ~Sepal.Length,
y = ~Sepal.Width,
color = ~Petal.Width,
visible = FALSE,
name = "Petal.Width",
colorbar= list(thicknes=20,
len = 0.35,
x=0.05,
y=0.35)
) %>%
layout(updatemenus=updatemenus)
You can further customize your colorbar with the arguments found in the Plotly documentation for R
Move your color parameter to the parameter list of marker
iris %>%
plot_ly() %>%
add_trace(
type = "scatter",
mode = "markers",
x = ~Sepal.Length,
y = ~Sepal.Width,
visible = TRUE,
name = "Petal.Length",
showlegend = F,
marker = list(
color = iris[["Petal.Length"]],
showscale = T,
colorbar = list(title = "aaaaaaaaaaaaa")
)
) %>%
add_trace(
type = "scatter",
mode = "markers",
x = ~Sepal.Length,
y = ~Sepal.Width,
visible = FALSE,
name = "Petal.Width",
showlegend = F,
marker = list(
color = iris[["Petal.Width"]],
showscale = T,
colorbar = list(title = "bbbbbbbbb")
)
) %>%
layout(updatemenus = updatemenus)

R plotly - How to fix colors when plotting grouped charts

I'm trying to plot overlaid line and bar charts using plotly, grouped by the same feature grp.
I need to use fixed colors for both lines and bars.
It works well when I first add the bar-trace :
library(plotly)
df <- data.frame(year = rep((2000:2017), each=2),
grp = rep(c("Grp1", "Grp2"), 18),
y1 = rnorm(36, 100, 40),
y2 = rnorm(36, 50, 10)
)
plot_ly(df) %>%
add_trace( x = ~year, y = ~y1,
type = 'bar',
yaxis = "y2",
opacity = .4,
color = ~grp,
colors = colorRamp(c("grey", "black"))) %>%
add_trace(x = ~year, y = ~y2,
type = 'scatter',
mode = 'lines+markers',
linetype = ~grp,
line = list(color = "red")) %>%
layout(yaxis2 = list(overlaying = "y",
side = "right"))
But If I switch bar trace and line trace, my color selection for bars disappears.
plot_ly(df) %>%
add_trace(x = ~year, y = ~y2,
type = 'scatter',
mode = 'lines+markers',
linetype = ~grp,
line = list(color = "red")) %>%
add_trace( x = ~year, y = ~y1,
type = 'bar',
yaxis = "y2",
opacity = .4,
color = ~grp,
colors = colorRamp(c("grey", "black"))) %>%
layout(yaxis2 = list(overlaying = "y",
side = "right"))
Obviously my syntax is incorrect : does someone know how to write this code properly to ensure colors are stable whatever the order is ?
Many thanks !
Try to specify colors inside plot_ly:
plot_ly(df, colors = colorRamp(c("grey", "black"))) %>%
add_trace(x = ~year, y = ~y2,
type = 'scatter',
mode = 'lines+markers',
linetype = ~grp,
line = list(color = "red")) %>%
add_trace( x = ~year, y = ~y1,
type = 'bar',
yaxis = "y2",
opacity = .4,
color = ~grp) %>%
layout(yaxis2 = list(overlaying = "y",
side = "right"))

Resources