Related
this is my dataframe :
artists <- c("Black Waitress", "Black Pumas")
tee_x<- c(20, 0)
tee_y <- c(3, 18)
tee_z <- c (30,0)
tee_t <- c(0,35)
data2 <- data.frame(artists, tee_x, tee_y,tee_t)
And this is what I am trying to create :
fig <- plot_ly(data=data2, x = ~artists, y = ~tee_x, type = 'bar', name = 'tee_x')
fig <- fig %>% add_trace(y = ~tee_y, name = 'tee_y')
fig <- fig %>% add_trace(y = ~tee_t, name = 'tee_t')
fig <- fig %>% layout(yaxis = list(title = 'Count'), barmode = 'group',
updatemenus = list(
list(
y = 0.8,
buttons = list(
list(method = "restyle",
args = list("x", list(data2[c(1),(2:4)])),
label = "Black Waitress"),
list(method = "restyle",
args = list("x", list(data2[c(2),(2:4)])),
label = "Black Pumas")))
))
fig
I am trying to create a grouper barplot in plotly which shows, for each artist the number of tees they sold and their type. I am also trying to create buttons so that you can look at individual artists instead of both of them. However it is not working and I have no clue how to solve the problem.
Thank you
EDIT :
I have been also trying this way
product <- c("tee_X","tee_y","tee_t")
artists <- c("Black Waitress", "Black Pumas")
Black_Waitress<- c(20, 0, 0)
Black_Pumas <- c(3, 18, 0)
tee_z <- c (30,0)
tee_t <- c(0,35)
data2 <- data.frame(product, Black_Waitress, Black_Pumas)
show_vec = c()
for (i in 1:length(artists)){
show_vec = c(show_vec,FALSE)
}
get_menu_list <- function(artists){
n_names = length(artists)
buttons = vector("list",n_names)
for(i in seq_along(buttons)){
show_vec[i] = TRUE
buttons[i] = list(list(method = "restyle",
args = list("visible", show_vec),
label = artists[i]))
print(list(show_vec))
show_vec[i] = FALSE
}
return_list = list(
list(
type = 'dropdown',
active = 0,
buttons = buttons
)
)
return(return_list)
}
print(get_menu_list(artists))
fig <- plot_ly(data=data2, x = ~product, y = ~Black_Waitress, type = 'bar')
fig <- fig %>% add_trace(y = ~Black_Pumas)
fig <- fig %>% layout(showlegend = F,yaxis = list(title = 'Count'), barmode = 'group',
updatemenus = get_menu_list(artists))
fig
However the problem is that when I choose an artist in the dropdown menu I want to be shown ONLY his/her products (in other words I would like to get rid of the 0 values dynamically) Is this possible?
Without the 0 Values and Initially Blank Plot
Essentially, you need to add a trace with no data. Additionally, since visibility settings were defined, all of that requires updating (because there are more traces now).
This can be further customized, of course. Here's a basic version of what I think you're looking for.
plot_ly(data = data3, x = ~artists, y = 0, type = "bar", color = ~tees,
visible = c(T, T, T)) %>%
add_bars(y = ~values, split = ~artists, # visibility F for all here
legendgroup = ~tees, name = ~tees, visible = rep(F, times = 4),
color = ~tees) %>%
layout(
yaxis = list(title = "Count"), barmode = "group",
updatemenus = list(
list(y = .8,
buttons = list(
list(method = "restyle", # there are 7 traces now; 3 blank
args = list(list(visible = c(F, F, F, F, T, F, T))),
label = "Black Waitress"),
list(method = "restyle",
args = list(list(visible = c(F, F, F, T, F, T, F))),
label = "Black Pumas")))))
Without the 0 Values
By your request, here is a version where the zero values are removed. First, I filtered the data for the non-zero values. This changed the number of traces from 6 to 4, so that needed to be accounted for in the areas where visibility is declared.
In this version, I only commented where there was something that changed from my original answer.
library(plotly)
library(tidyverse)
artists <- c("Black Waitress", "Black Pumas")
tee_x <- c(20, 0)
tee_y <- c(3, 18)
# tee_z <- c(30,0)
tee_t <- c(0,35)
data2 <- data.frame(artists, tee_x, tee_y, tee_t)
data3 <- pivot_longer(data2, col = starts_with("tee"),
names_to = "tees", values_to = "values") %>%
filter(values != 0) # <----- filter for non-zeros
plot_ly(data = data3, x = ~artists, y = ~values, split = ~artists,
legendgroup = ~tees, name = ~tees,
visible = rep(c(F, T), times = 2), # <---- 4 traces
color = ~tees, type = "bar") %>%
layout(
yaxis = list(title = "Count"), barmode = "group",
updatemenus = list(
list(y = .8,
buttons = list(
list(method = "restyle", # 4 traces
args = list(list(visible = c(F, T, F, T))),
label = "Black Waitress"),
list(method = "restyle", # 4 traces
args = list(list(visible = c(T, F, T, F))),
label = "Black Pumas")))))
With the 0 Values
I think it will be a lot easier to use visibility than trying to change out the data. If you wanted to see one artist at a time and use the dropdown to switch between the groups, this works.
First, I rearranged the data to make this easier. When I plotted it, I used split, so that the traces were split by the values on the x-axis, along with the colors. The traces are ordered artist 1, tee_t, artist 2, tee_t... and so on. When using visibility, you need the method restyle (because it's a trace attribute) and a declaration of true or false for each trace.
library(tidyverse)
library(plotly)
data3 <- pivot_longer(data2, col = starts_with("tee"),
names_to = "tees", values_to = "values")
plot_ly(data = data3, x = ~artists, y = ~values, split = ~artists,
legendgroup = ~tees, name = ~tees, visible = rep(c(F, T), times = 3),
color = ~tees, type = "bar") %>%
layout(
yaxis = list(title = "Count"), barmode = "group",
updatemenus = list(
list(y = .8,
buttons = list(
list(method = "restyle",
args = list(list(visible = c(F, T, F, T, F, T))),
label = "Black Waitress"),
list(method = "restyle",
args = list(list(visible = c(T, F, T, F, T, F))),
label = "Black Pumas")))))
I am working with the R programming language. I am trying to replicate the following tutorial with some fake data that I generated: https://plotly.com/r/dropdowns/.
That is, I generated some fake data and made 4 scatter plots. Using the "plotly" library, I then want to "attach" these 4 plots together and let the user "toggle" (switch, shuffle) between these graphs.
I have attached the code below:
#load libraries
library(plotly)
library(MASS)
library(dplyr)
# create data
x <- sample( LETTERS[1:4], 731, replace=TRUE, prob=c(0.25, 0.25, 0.25, 0.25) )
y <- rnorm(731,10,10)
z <- rnorm(731,5,5)
df <- data.frame(x,y, z)
df$x = as.factor(df$x)
fig = plot_ly()
fig = fig %>% add_trace( data = df[which(df$x == "A"),],
type = "scatter", mode = "markers", x = ~ y, y = ~z,
name= "graph1") %>% layout(title = list(text = "graph 1"))
fig = fig %>% add_trace( data = df[which(df$x == "B"),],
type = "scatter", mode = "markers", x = ~ y, y = ~z,
name= "graph2") %>% layout(title = list(text = "graph 2"))
fig = fig %>% add_trace( data = df[which(df$x == "C"),],
type = "scatter", mode = "markers", x = ~ y, y = ~z,
name= "graph2") %>% layout(title = list(text = "graph 3"))
fig = fig %>% add_trace( data = df[which(df$x == "D"),],
type = "scatter", mode = "markers", x = ~ y, y = ~z,
name= "graph2") %>% layout(title = list(text = "graph 4"))
final = fig %>% layout(xaxis = list(domain = c(0.1, 1)),
yaxis = list(title = "y"),
updatemenus = list(
list(
y = 0.7,
buttons = list(
list(method = "restyle",
args = list("visible", list(FALSE, FALSE, TRUE, FALSE)),
label = "A"),
list(method = "restyle",
args = list("visible", list(FALSE, FALSE, TRUE, FALSE)),
label = "B"),
list(method = "restyle",
args = list("visible", list(FALSE, FALSE, TRUE, FALSE)),
label = "C"),
list(method = "restyle",
args = list("visible", list(FALSE, FALSE, FALSE, TRUE)),
label = "D")))))
#view final file
final
#save
library(htmlwidgets)
saveWidget( final, "file.html")
The code seems to run successfully. However, there seems to be a few problems.
Problem 1) When the user "views" the plotly object for the first time:
#view final file
final
Even though the drop down menu is selected as "A", observations corresponding to all 4 groups (4 different colors) all appear on the graph. Also, the title "graph 4" appears instead of "graph 1".
Problem 2) When you try to toggle between all 4 graphs:
None of the titles change at all. Also, the first three graphs appear to be identical.
Can someone please show me what am I doing wrong? Is there a way to fix this?
Thanks
I think this tutorial from plotly explains some of the issues in your code. Probably worth having a look!
The title is an attribute of the whole plot and not of each individual trace of the plot. So when you are setting layout(title='graph #') when you add each trace to the plot, you are setting it for the whole plot and overriding the value set in the previous trace. Since the last one you set is layout(title='graph 4'), this is the one you see when the plot is created.
You need to set the initial value of the visible attribute when you create each trace, so when the plot is created you see only one trace. In this case, when you create the traces, you need to set visible=TRUE for A and visible=FALSE for the rest.
Because you want to update the title of the plot, you cannot use the restyle method (take a look at the tutorial link above). You have to use the update method, which is used to change both the attributes of each trace and also to change the layout of the plot. With the update method, the value of arg is a list of lists. The first element of the main list is a list with the named values for updating the traces (just as you did before with restyle). The second element of the main list is another list with the named values for changing the layout.
Here is my code.
fig = plot_ly() %>%
# for each trace set the initial visibility
add_trace(data = df[which(df$x == "A"),],
type = "scatter", mode = "markers", x = ~ y, y = ~z,
name= "graph1", visible=T) %>%
add_trace(data = df[which(df$x == "B"),],
type = "scatter", mode = "markers", x = ~ y, y = ~z,
name= "graph2", visible=F) %>%
add_trace(data = df[which(df$x == "C"),],
type = "scatter", mode = "markers", x = ~ y, y = ~z,
name= "graph3", visible=F) %>%
add_trace(data = df[which(df$x == "D"),],
type = "scatter", mode = "markers", x = ~ y, y = ~z,
name= "graph4", visible=F) %>%
# set initial value of title in the layout
layout(title="Graph 1",
xaxis = list(domain = c(0.1, 1)),
yaxis = list(title = "y"),
updatemenus = list(
list(y = 0.7,
buttons = list(
list(method = "update",
args = list(list(visible =c(TRUE, FALSE, FALSE, FALSE)),
list(title='Graph 1')),
label = "A"),
list(method = "update",
args = list(list(visible =c(FALSE, TRUE, FALSE, FALSE)),
list(title='Graph 2')),
label = "B"),
list(method = "update",
args = list(list(visible =c(FALSE, FALSE, TRUE, FALSE)),
list(title='Graph 3')),
label = "C"),
list(method = "update",
args = list(list(visible =c(FALSE, FALSE, FALSE, TRUE)),
list(title='Graph 4')),
label = "D")))))
#view final file
fig
There were other issues that iI fixed like all traces had the same name ('graph2') and the value of visible was always list(FALSE, FALSE, TRUE, FALSE) (which would result in always the same trace being shown). But I believe these were typos.
I am using the R programming language. I am trying to follow the tutorial here on "switching between graphs" : https://plotly.com/r/dropdowns/ (first example).
First, I generated some data in R:
library(plotly)
library(MASS)
x <- sample( LETTERS[1:4], 1000, replace=TRUE, prob=c(0.25, 0.25, 0.25, 0.25) )
y <- rnorm(1000,10,10)
z <- rnorm(1000,5,5)
df <- data.frame(x,y, z)
df$x = as.factor(df$x)
colnames(df) <- c("x", "y", "z")
I tried to modify the code from this tutorial to make the final result:
fig <- plot_ly(df, x = ~x, y = ~y, z = ~z alpha = 0.3)
fig <- fig %>% add_markers(marker = list(line = list(color = "black", width = 1)))
fig <- fig %>% layout(
title = "Drop down menus - Plot type",
xaxis = list(domain = c(0.1, 1)),
yaxis = list(title = "y"),
updatemenus = list(
list(
y = 0.8,
buttons = list(
list(method = "restyle",
args = list("type", "scatter"),
label = "Scatter A"),
list(method = "restyle",
args = list("type", "scatter"),
label = "Scatter B"),
list(method = "restyle",
args = list("type", "scatter"),
label = "Scatter C"),
list(method = "restyle",
args = list("type", "scatter"),
label = "Scatter D")
))
But this does not seem to be working.
Instead, I had a different idea: Perhaps I could create a series of graphs that I want to be able to "switch" between:
df_1 <- df[which(df$x == "A"),]
df_2 <- df[which(df$x == "B"),]
df_3 <- df[which(df$x == "C"),]
df_4 <- df[which(df$x == "D"),]
graph_1 <- plot_ly( data = df_1, type = "scatter", mode = "markers", x = ~ y, y = ~z) %>% layout(title = "graph 1")
graph_2 <- plot_ly( data = df_2, type = "scatter", mode = "markers", x = ~ y, y = ~z) %>% layout(title = "graph 2")
graph_3 <- plot_ly( data = df_3, type = "scatter", mode = "markers", x = ~ y, y = ~z) %>% layout(title = "graph 3")
graph_4 <- plot_ly( data = df_4, type = "scatter", mode = "markers", x = ~ y, y = ~z) %>% layout(title = "graph 4")
graph_5 <- plot_ly(df, y = ~y, color = ~x, type = "box") %>% layout(title = "boxplot")
Now, is it possible to modify the plotly code to "tie" all these graphs (graph_1, graph_2, graph_3, graph_4, graph_5) together, so that the user can click the tab on the left and switch between these graphs?
Thanks
The example you should look at the tutorial is the last one (with the sine waves). It hides and shows different traces of the plot depending on the value of the selection in the dropdown menu.
You just need to change the format of your dataframe to wide.
df.wide <- df %>% tidyr::pivot_wider(names_from = x, values_from=z)
df.wide
## A tibble: 1,000 x 5
# y D B A C
# <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 6.48 6.21 NA NA NA
# 2 23.6 NA 15.3 NA NA
# 3 -9.99 -2.16 NA NA NA
# 4 19.6 NA NA 0.0683 NA
# 5 18.8 -1.40 NA NA NA
# 6 -2.71 9.80 NA NA NA
# 7 2.32 NA NA NA 3.77
# 8 11.9 NA 4.35 NA NA
# 9 21.4 NA NA NA 13.9
#10 2.34 NA 2.10 NA NA
## … with 990 more rows
Then add a separate scatter trace for each column. In the arguments of the dropdown menu you can set which traces are going to be visible when each option is selected. For example args = list("visible", list(TRUE, FALSE, FALSE, FALSE)) means that only the first trace added (in this case column A) is going to be visible.
fig <- plot_ly(df.wide, x = ~y)
fig <- fig %>%
add_trace(y = ~A, name = "A", type='scatter', mode='markers') %>%
add_trace(y = ~B, name = "B", type='scatter', mode='markers', visible = F) %>%
add_trace(y = ~C, name = "C", type='scatter', mode='markers', visible = F) %>%
add_trace(y = ~D, name = "D", type='scatter', mode='markers', visible = F) %>%
layout(xaxis = list(domain = c(0.1, 1)),
yaxis = list(title = "y"),
updatemenus = list(
list(
y = 0.7,
buttons = list(
list(method = "restyle",
args = list("visible", list(TRUE, FALSE, FALSE, FALSE)),
label = "A"),
list(method = "restyle",
args = list("visible", list(FALSE, TRUE, FALSE, FALSE)),
label = "B"),
list(method = "restyle",
args = list("visible", list(FALSE, FALSE, TRUE, FALSE)),
label = "C"),
list(method = "restyle",
args = list("visible", list(FALSE, FALSE, FALSE, TRUE)),
label = "D")))))
EDIT: Adding also a box plot
Adding an option for a different type of plot (like a box plot) is a little bit harder. The issue now is that x axis in the box plot and scatter plot are different. So you can't use the same axis. Fortunately, plotly lets you map different traces to different axis. Then you can set the position of this new axis within the complete plot using the domain attribute.
My solution is a little bit hacky because I used the domain attribute to "hide" the "plot" I did not want to use by making it very small (I also made the corresponding data invisible by setting visible = FALSE). This is because hiding the axis only hides the lines. You are still left with the background of the plot.
Note that now I use the method update (instead of restyle) because it allows you to change also the layout of the plot (https://plotly.com/r/custom-buttons/).
But it seemed to work very well!
# I had to reorder the dataframe because the boxplot was not following the order of the factors. Apparently it follows the orders that the letter appear.
df <- df %>% dplyr::arrange(x)
df.wide <- df %>% tidyr::pivot_wider(names_from = x, values_from=z)
# this is a list with axis config for scatter plot (define here to avoid repetition)
axis.config.scatter <- list(xaxis = list(title = "x", domain = c(0.1, 1), visible=T),
yaxis = list(title = "y", domain = c(0, 1), visible=T),
xaxis2 = list(title = "group", domain = c(0.99, 1), visible=F),
yaxis2 = list(title = "y", domain = c(0,99, 1), visible=F))
# this is a list with axis config for box plot (define here to avoid repetition)
axis.config.box <- list(xaxis = list(title = "x", domain = c(0.99, 1), visible=F),
yaxis = list(title = "y", domain = c(0.99, 1), visible=F),
xaxis2 = list(title = "group", domain = c(0.1, 1), visible=T, anchor='free'),
yaxis2 = list(title = "y", domain = c(0, 1), visible=T, anchor='free'))
fig <- plot_ly(df.wide)
fig <- fig %>%
add_trace(x = ~y, y = ~A, name = "A", type='scatter', mode='markers') %>%
add_trace(x = ~y, y = ~B, name = "B", type='scatter', mode='markers', visible = F) %>%
add_trace(x = ~y, y = ~C, name = "C", type='scatter', mode='markers', visible = F) %>%
add_trace(x = ~y, y = ~D, name = "D", type='scatter', mode='markers', visible = F) %>%
add_trace(data=df, x=~x, y=~z, name='box', type='box', visible=F, xaxis='x2', yaxis='y2') %>%
layout(xaxis = list(title = "x", domain = c(0.1, 1)),
yaxis = list(title = "y"),
xaxis2 = list(title = "group", domain = c(0.99, 1), visible=F),
yaxis2 = list(title = "y", domain = c(0.99, 1), visible=F),
updatemenus = list(
list(
y = 0.7,
buttons = list(
list(method = "update",
args = list(list(visible = c(TRUE, FALSE, FALSE, FALSE, FALSE)),
axis.config.scatter),
label = "A"),
list(method = "update",
args = list(list(visible = c(FALSE, TRUE, FALSE, FALSE, FALSE)),
axis.config.scatter),
label = "B"),
list(method = "update",
args = list(list(visible = c(FALSE, FALSE, TRUE, FALSE, FALSE)),
axis.config.scatter),
label = "C"),
list(method = "update",
args = list(list(visible = c(FALSE, FALSE, FALSE, TRUE, FALSE)),
axis.config.scatter),
label = "D"),
list(method = "update",
args = list(list(visible = c(FALSE, FALSE, FALSE, FALSE, TRUE)),
axis.config.box),
label = "box")
))))
I've made a filtered plotly stacked bar chart however it is showing empty spaces for bars that don't exist under the specified filter.
Here is my code:
event <- c("eve1","eve2","eve3","eve1","eve3","eve4","eve3","eve1","eve1","eve2","eve3","eve4","eve2","eve1")
year <-c("2017","2017","2017","2018","2018","2019","2019","2019","2020","2020","Total","Total","Total","Total")
cat1 <- c(20,14,25,64,0,5,34,2,14,6,78,0,0,23)
cat2 <- c(0,4,0,4,3,9,0,2,4,12,8,42,31,2)
eve <- data.frame(event,year,cat1,cat2)
fig <- plot_ly(eve, x = ~event, y = ~cat1, type = 'bar', name = 'cat1',
transforms = list(list(type = 'filter',target = ~year, operation = '=',value = eve$year))) %>%
add_trace(y = ~cat2, name = 'cat2')%>%
layout(yaxis = list(title = 'count'), barmode = 'stack',
updatemenus = list(list(type = 'dropdown',active = 0,buttons =
list(list(method = "restyle",
args = list("transforms[0].value", unique(eve$year)[1]),
label = unique(eve$year)[1]),
list(method = "restyle",
args = list("transforms[0].value", unique(eve$year)[2]),
label = unique(eve$year)[2]),
list(method = "restyle",
args = list("transforms[0].value", unique(eve$year)[3]),
label = unique(eve$year)[3]),
list(method = "restyle",
args = list("transforms[0].value", unique(eve$year)[4]),
label = unique(eve$year)[4]),
list(method = "restyle",
args = list("transforms[0].value", unique(eve$year)[5]),
label = unique(eve$year)[5])
))))
fig
Why is "eve2" appearing for 2019 when it doesn't appear in the data?
Found the solution a couple of months later, posting it in case anyone else comes across a similar issue.
Under layout you need to specify categoryorder and categoryarray. This stops Plotly from auto-sorting the dataframe which can interfere with the transforms element of the filter.
Here is the corrected code:
event <- c("eve1","eve2","eve3","eve1","eve3","eve4","eve3","eve1","eve1","eve2","eve3","eve4","eve2","eve1")
year <-c("2017","2017","2017","2018","2018","2019","2019","2019","2020","2020","Total","Total","Total","Total")
cat1 <- c(20,14,25,64,0,5,34,2,14,6,78,0,0,23)
cat2 <- c(0,4,0,4,3,9,0,2,4,12,8,42,31,2)
eve <- data.frame(event,year,cat1,cat2)
fig <- plot_ly(eve, x = ~event, y = ~cat1, type = 'bar', name = 'cat1',
transforms = list(list(type = 'filter',target = ~year, operation = '=',value = eve$year))) %>%
add_trace(y = ~cat2, name = 'cat2')%>%
layout(yaxis = list(title = 'count'),xaxis=list(categoryorder = "array",
categoryarray = eve$year), barmode = 'stack',
updatemenus = list(list(type = 'dropdown',active = 0,buttons =
list(list(method = "restyle",
args = list("transforms[0].value", unique(eve$year)[1]),
label = unique(eve$year)[1]),
list(method = "restyle",
args = list("transforms[0].value", unique(eve$year)[2]),
label = unique(eve$year)[2]),
list(method = "restyle",
args = list("transforms[0].value", unique(eve$year)[3]),
label = unique(eve$year)[3]),
list(method = "restyle",
args = list("transforms[0].value", unique(eve$year)[4]),
label = unique(eve$year)[4]),
list(method = "restyle",
args = list("transforms[0].value", unique(eve$year)[5]),
label = unique(eve$year)[5])
))))
fig
I am trying to map color over two variables using Restyle Buttons of plotly keeping the y and x-axis dynamics when changing the colours. When I add color=~fruit in the main plot it gives the result I am looking for, but I lose the dynamics of the axes when changing the variables. I basically want to change the color of the lines referent to the fruits. Below are data and the code I am using for playing with that. Thanks for any help or hints!
libraries
library(dplyr); library(plotly);
data
dfake <- tibble(days = seq(1,100, by=1),
bask = seq(1,500, by=5),
fruit = c(rep("grape", 50),
rep("apple", 50)));
plotting code
plot <- dfake %>%
plot_ly(x = ~days, y = ~bask, text = ~fruit,
type = 'scatter',
mode = 'lines',
hoverinfo = 'text',
transforms = list(
list(type = 'filter',
target = ~fruit,
operation = '=',
value = unique(dfake$fruit)[1]))) %>%
layout(updatemenus = list(
list(type = 'dropdown',
active = 1,
buttons = list(
list(method = "restyle",
args = list("transforms[0].value",
unique(dfake$fruit)[1]),
label = unique(dfake$fruit)[1]),
list(method = "restyle",
args = list("transforms[0].value",
unique(dfake$fruit)[2]),
label = unique(dfake$fruit)[2])))));
plot;
Yes, wasn't sure of the data was in the best possible format. So, I was fiddling with this in the following manner:
Make a dataframe, where each Y-axis variable goes into individual column (refer to the tidyverse philosopy).
Appending the lines layer-by-layer into plotly.
Using updatemenus to get the interactive buttons & desired visibility.
#Converting into a dataframe, mutating new columns for each fruit and getting their name:
df_dfake <- as.data.frame(dfake)
df_dfake <- df_dfake %>% mutate(fruit1_bask = case_when(fruit == "grape" ~ bask),
fruit2_bask = case_when(fruit == "apple" ~ bask))
fruit1 <- unique(dfake$fruit)[1]; fruit2 <- unique(dfake$fruit)[2];
#Plotly, adding layer by layer:
fig <- df_dfake %>% plot_ly(type = 'scatter',
mode = 'lines',
hoverinfo = 'text');
fig1 <- fig %>% add_lines(x = ~days , y = ~fruit1_bask, text = ~fruit,
line=list(color="#33CFA5"));
fig2 <- fig1 %>% add_lines(x = ~days, y = ~fruit2_bask, text = ~fruit,
line=list(color="#F06A6A"));
fig2;
fig2
Now, updatemenus component, to make the interactive buttons
updatemenus <- list(
list(
active = -1,
type= 'buttons',
buttons = list(
list(
label = unique(dfake$fruit)[1],
method = "update",
args = list(list(visible = c(FALSE, TRUE)),# this defines visibility on click
list(title = "fruit1",
annotations = list(c(), df_dfake$fruit1_bask)))),
list(
label = unique(dfake$fruit)[2],
method = "update",
args = list(list(visible = c(T, F)),# this defines visibility on click
list(title = "fruit2",
annotations = list(c(), df_dfake$fruit2_bask))))
)
)
)
fig3 <- fig2 %>% layout(title = "Apples & Oranges", showlegend=FALSE,
xaxis=list(title="Days"),
yaxis=list(title="Basket"),
updatemenus=updatemenus); fig
Which results in the following graphs with interactive buttons:
fig3
Check Update Button to learn more :)
I finally got what I was looking for,
very easy actually, I just needed to have my grape and apple not inside column fruit but as different columns and treat them with add_trace. Setting one as invisible. In each add_trace I was free to play with color, width etc.
After this organisation it was easier to work with button .
I hope this simple coding can help someone. If not sorry.
dfake <- tibble(days = seq(1,100, by=1),
grape = seq(1,500, by=5),
apple = seq(501,1000, by=5))
fig <- plot_ly(dfake, x = ~days) %>%
add_trace(y = ~grape, name = 'Bask',
mode = 'lines+markers', type = "scatter",
marker = list(color = 'blue'),
line = list(color = 'blue', width = 4)) %>%
add_trace(y = ~apple, name = 'New', visible = F,
mode = 'lines+markers', type = "scatter",
marker = list(color = 'red'),
line = list(color = 'red', width = 4)) %>%
layout(
title = "Corona global Cases and Depths",
xaxis = list(domain = c(0.1)),
yaxis = list(title = "yaxis"),
updatemenus = list(
list(y = 0.9,
buttons = list(
list(method = "restyle",
args = list("visible", list(TRUE, FALSE)),
label = "Grape"),
list(method = "restyle",
args = list("visible", list(FALSE, TRUE)),
label = "Apple")))))
fig