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

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

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:

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: Subplot shows only bottom plot

I would like to reproduce the "Scaled Subplots" in https://plot.ly/r/subplots/ for the mtcars data.
mtcars %>%
transform(id = as.integer(factor(am))) %>%
plot_ly(x = ~mpg, y = ~qsec, color = ~factor(vs), yaxis = ~paste0("y", id)) %>%
add_markers() %>%
subplot(nrows = 2, shareX = TRUE)
Only the bottom subplot shows up:
I thought that I did faithfully copy/translated the code, but something must be wrong.
Of note, the subplot discriminates am, whereas the color discriminates vs.
I tried am for both the subplot and the color:
mtcars %>%
transform(id = as.integer(factor(am))) %>%
plot_ly(x = ~mpg, y = ~qsec, color = ~am, yaxis = ~paste0("y", id)) %>%
add_markers() %>%
subplot(nrows = 2, shareX = TRUE)
It does not help much, but the two grids appear:
On the latter example, I expected the am==0 (blueish) dots to be in the top subplot.
Any suggestion?
packageVersion('plotly')
[1] ‘4.9.0’
Well, this way here you can do this, but must create two plots and than use subplot()
p1 = mtcars %>%
filter(am==0) %>%
plot_ly(x = ~mpg, y = ~qsec, color = ~vs, legendgroup = "am0", name = "0",
type = "scatter" , mode = "markers", showlegend = FALSE)
p2 = mtcars %>%
filter(am==1) %>%
plot_ly(x = ~mpg, y = ~qsec, color = ~vs, legendgroup = "am1", name = "1",
type = "scatter" , mode = "markers")
subplot(p1,p2,nrows = 2, shareX = TRUE, titleY = TRUE, which_layout = 1)
Here the output:

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