Reputation: 303
I have a data set that looks like this:
date food transp housing other
0 2015-06-01 1510 45.58 0 101.5
1 2015-07-01 1163.91 74.14 210 106.7
2 2015-08-01 101.3 95.03 210 54.5
3 2015-09-01 1131.67 22.28 210 46.3
4 2015-10-01 818.44 88.88 815.2 47.2
I am aiming for chart "Olympic medal" in the link chart, it is implemented in plolty via python and I am trying to replicate in R. What I have been able to do is this
In first glace it looks similar but I don't have the drop down menu 'All' which contain all the lines. At present there is no way to bring back the plot with all the lines unlike in the first chart where there is option 'All'. Code below shows how I created my second plot. Any help is greatly appreciated.
plot <- plot_ly(newf, x = ~round_date, y = ~other, name='other', type='scatter',mode='lines+markers') %>%
add_trace(y = ~food, name = 'food') %>%
add_trace(y = ~housing, name = 'housing') %>%
add_trace(y = ~transport, name = 'transport') %>%
layout(
title = "Button Restyle",
xaxis = list(domain = c(0.1, 1)),
yaxis = list(title = "y"),
updatemenus = list(
list(
type = "buttons",
y = 0.8,
label = 'Category',
buttons = list(
list(method = "restyle",
args = list("y",list(~other)),
label = "houshold"),
list(method = "restyle",
args = list("y",list(~food)),
label = "communication"),
list(method = "restyle",
args = list("y",list(~housing)),
label = "food"),
list(method = "restyle",
args = list("y",list(~transport)),
label = "transport")
))))
Upvotes: 1
Views: 2334
Reputation: 31679
Instead of changing the y-values it is preferable to hide/show axes. You can hide and show axes in plotly by passing an array to the visible attribute, e.g. args = list('visible', c(TRUE, FALSE, FALSE))
would show the first axis and hide the other two.
See below for the full code or here online.
Changing from a scatter plot to a bar chart would screw up the restyle process when the y-values are changed, you would get 4 bars with different colors but identical y-values.
library(plotly)
plot_ly(mtcars, x = rownames(mtcars), y = ~mpg, name='mpg', type='scatter', mode='markers') %>%
add_trace(y = ~hp, name = 'power', type='scatter', mode='markers') %>%
add_trace(y = ~qsec, name = 'qsec', type='scatter', mode='markers') %>%
layout(
updatemenus = list(
list(
type = "buttons",
x = -0.1,
y = 0.6,
label = 'Category',
buttons = list(
list(method = "restyle",
args = list('visible', c(TRUE, TRUE, TRUE)),
label = "View all"),
list(method = "restyle",
args = list('visible', c(FALSE, FALSE, FALSE)),
label = "Hide all")
)
),
list(
type = "buttons",
x = -0.1,
y = 0.7,
label = 'Category',
buttons = list(
list(method = "restyle",
args = list('visible', c(TRUE, FALSE, FALSE)),
label = "mpg"),
list(method = "restyle",
args = list('visible', c(FALSE, TRUE, FALSE)),
label = "hp"),
list(method = "restyle",
args = list('visible', c(FALSE, FALSE, TRUE)),
label = "qsec")
)
)
)
)
Upvotes: 2