R Plotly Colorbar Disappears with Buttons - r

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)

Related

How to add exactly same right axis on plotly R chart?

I have this basic script:
mtcars %>%
plot_ly() %>%
add_trace(x = ~mpg, y = ~wt, type = 'scatter', mode = 'markers' ) %>%
layout(yaxis2 = list(overlaying = "y", side = "right", title = 'test'))
It plots the chart with one axe on the left even though there is a command in layout to add a second chart.
I suppose it doesn't get triggered because nothing is plotted.
So I add the same variable
mtcars %>%
plot_ly() %>%
add_trace(x = ~mpg, y = ~wt, type = 'scatter', mode = 'markers' ) %>%
add_trace(x = ~mpg, y = ~wt, type = 'scatter', mode = 'markers', yaxis = "y2" ) %>%
layout(yaxis2 = list(overlaying = "y", side = "right", title = 'test'))
And in this case both left and right axis are the same.
I suppose I could just do the second series transparent or something.
Is there a more clean solution to force the second axis to show even though nothing is plotted on it?
Try something like this:
library(plotly)
#Code
mtcars %>%
plot_ly() %>%
add_trace(x = ~mpg, y = ~wt, type = 'scatter', mode = 'markers' ) %>%
add_trace(x = ~mpg, y = ~wt, type = 'scatter', mode = 'markers',
yaxis = "y2") %>%
layout(yaxis2 = list(overlaying = "y", side = "right", title = 'test'))
Output:
Old question, but anyway:
You can't display an axis without assigning a trace to it.
However, you don't need to use the entire data again - you can simply create datapoints based on range() to achive the same an hide the trace via the opacity and showlegend parameters (using visible would prevent showing the axis):
library(plotly)
mtcars %>%
plot_ly() %>%
add_trace(x = ~mpg, y = ~wt, type = 'scatter', mode = 'markers') %>%
add_trace(x = ~range(mpg), y = ~range(wt), type = 'scatter', mode = 'markers', yaxis = "y2", opacity = 0, showlegend = FALSE) %>%
layout(showlegend = TRUE, yaxis2 = list(overlaying = "y", side = "right", title = 'test'))

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:

How can I hide a trace in a legend in R plotly

I have a plotly graph with several traces. Some of them I don't want to appear in the legend. How do I do this?
You need to set showlegend = F in your trace:
CODE
library(plotly)
plt <- plot_ly(as.data.frame(mtcars)) %>%
add_markers(x = ~wt, y = ~mpg, name = 'Fuel Eff.', type = 'scatter') %>%
add_markers(x = ~wt, y = ~hp, name = 'Power to wt. ratio', type = 'scatter',
showlegend = F) %>%
layout(
showlegend = T,
legend = list(orientation = 'h')
)
Output

Plotly not showing legend for two confidence intervals in 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')

managing colors in R for plot.ly

I am using the plot.ly library for interactive charting in a shiny app however I am running up against some trouble with managing the colors in the chart.
Reproducible example using plotly 4.3.5 (from github):
library(data.table)
library(plotly)
dt <- data.table(campaign_week = c(1,2,3,1,2,3), category = c(rep("income",3),rep("cost",3)),
amount = c(100,50,35,-500,-20,-15))
dt_net <- dt[, .(amount = sum(amount)), by = campaign_week][,.(campaign_week, amount = cumsum(amount))]
y <- list(title = "Income", tickformat = "$,.0f",hoverformat = "$,.2f")
plot_ly(dt_net, x = ~campaign_week, y = ~amount, type = "scatter",
mode= "lines+markers",
line = list(color = "#00AEFF"), name = "Net Income") %>%
add_trace(data = dt, x = ~campaign_week, y = ~amount, color = ~category, type = "bar",
colors = c("#00ff00", "#ff0000")) %>%
layout(yaxis = y, barmode = "relative")
This creates the chart that I want, however the colours aren't being applied correctly to the trace. I am expecting one of the bars to be red, and the other to be green while the line is a shade of blue.
EDIT Add a screenshot of the plotly chart created
Based on the example at https://plot.ly/r/bar-charts/#bar-chart-with-relative-barmode a separate add_trace for each category is the way to go.
plot_ly(dt_net, x = ~campaign_week, y = ~amount, type = "scatter",
mode= "lines+markers",
line = list(color = "#00AEFF"), name = "Net Income") %>%
add_trace(data = dt[category=="income",] , x = ~campaign_week, y = ~amount, type = "bar", name = "income",
marker=list(color = "#00ff00")) %>%
add_trace(data = dt[category=="cost",] , x = ~campaign_week, y = ~amount, type = "bar", name = "cost",
marker=list(color = "#ff0000")) %>%
layout(yaxis = y, barmode = "relative")
Note, this gives a warning, because the bar chart traces inherit mode and line attributes from the scatter chart, but these attributes are not supported for bars. You can either ignore the warnings, or you can call the barchart before the scatter to avoid them... Like this:
plot_ly() %>%
add_trace(data = dt[category=="income",] , x = ~campaign_week, y = ~amount, type = "bar", name = "income",
marker=list(color = "#00ff00")) %>%
add_trace(data = dt[category=="cost",] , x = ~campaign_week, y = ~amount, type = "bar", name = "cost",
marker=list(color = "#ff0000")) %>%
add_trace(data = dt_net, x = ~campaign_week, y = ~amount, type = "scatter", mode= "lines+markers",
line = list(color = "#00AEFF"), name = "Net Income") %>%
layout(yaxis = y, barmode = "relative")
I reverted the calls and added the inherit=FALSE:
library(data.table)
library(plotly)
dt <- data.table(campaign_week = c(1,2,3,1,2,3), category = c(rep("income",3),rep("cost",3)),
amount = c(100,50,35,-500,-20,-15))
dt_net <- dt[, .(amount = sum(amount)), by = campaign_week][,.(campaign_week, amount = cumsum(amount))]
y <- list(title = "Income", tickformat = "$,.0f",hoverformat = "$,.2f")
plot_ly(data=dt, x = ~campaign_week, y = ~amount, color = ~category, type = "bar",
colors = c("#00ff00", "#ff0000")) %>%
add_trace( data=dt_net, x = ~campaign_week, y = dt_net$amount, type = "scatter",
mode= "lines+markers",
line = list(color = "#00AEFF"), name = "Net Income", inherit=FALSE) %>%
layout(yaxis = y, barmode = "relative")
Still have a problem with the legend:

Resources