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'))
Related
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"
)
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
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)
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:
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"))