R plotly dropdown event for grouped boxplot - r

I would like to use a plotly dropdown event to show different grouped boxplots, however I have not been able to achieve this as yet:
The first plot shows expected output for plotly with dropdown = "4" (obtained using ggplot). The second plot is what I get...
library(tidyverse)
library(plotly)
dat <- mtcars %>%
filter(cyl == 4 | carb == 4) %>%
group_by(cyl, carb, am) %>%
summarise(boxplot= list( setNames(boxplot.stats(disp)$stats,
c('lower_whisker','lower_hinge','median','upper_hinge','upper_whisker')) )) %>%
unnest_wider(boxplot) %>%
arrange(cyl, carb, am) %>%
ungroup() %>%
mutate_at(vars(cyl, carb, am), as.character)
cylinders <- unique(dat$cyl)
dat %>%
filter(cyl == 4) %>%
ggplot(aes(
x = carb,
lower = lower_hinge,
upper = upper_hinge,
middle = median,
ymin = lower_whisker,
ymax = upper_whisker,
colour = am)) +
geom_boxplot(stat = "identity")
p <- plot_ly(type = "box")
for(icyl in cylinders){
dataFilt <- filter(dat, cyl == icyl)
p <- add_trace(p,
visible = TRUE,
q1 = dataFilt$lower_hinge,
median = dataFilt$median,
q3 = dataFilt$upper_hinge,
lowerfence = dataFilt$lower_whisker,
upperfence = dataFilt$upper_whisker,
x = dataFilt$carb,
color = dataFilt$am,
name=icyl
)
}
p %>%
layout(boxmode = "group",
updatemenus = list(
list(
y = 0.8,
buttons = list(
list(label = cylinders[1],
method = "update",
args = list(list(visible = c(TRUE, FALSE, FALSE)))),
list(label = cylinders[2],
method = "update",
args = list(list(visible = c(FALSE, TRUE, FALSE)))),
list(label = cylinders[3],
method = "update",
args = list(list(visible = c(FALSE, FALSE, TRUE))))
))))

I wasn't able to get it to work with the loop, but I did get it to work as you had expected. Instead of using the variation where you did all the manual work to create the hinges, whiskers, etc., I used add_boxplot.
In layout you'll see that there are 6 T or F. That's because plotly doesn't save grouping as a function, it transforms the data into two separate traces. For example, the first two are cyl == "4" & am == "0" and cyl == "4" & am == "1"
cylinders <- unique(mtcars$cyl) # kept similar from original work
# added to simplify build
mtcars <- mtcars %>% mutate_at(vars(cyl, carb, am), as.character)
# made all cylinder options subplots
plot_ly() %>%
add_boxplot(x = ~carb, y = ~disp, color = ~am, colors = "Set2",
data = mtcars[mtcars$cyl == cylinders[1], ],
visible = T, inherit = F) %>% # visible
add_boxplot(x = ~carb, y = ~disp, color = ~am, colors = "Set2",
data = mtcars[mtcars$cyl == cylinders[2], ],
visible = F, inherit = F) %>% # invisible
add_boxplot(x = ~carb, y = ~disp, color = ~am, colors = "Set2",
data = mtcars[mtcars$cyl == cylinders[3], ],
visible = F, inherit = F) %>% # invisible
layout(updatemenus = list(
list(
y = 0.8,
buttons = list(
list(label = cylinders[1], # four cyl
method = "restyle",
args = list("visible", list(T, T, F, F, F, F))),
list(label = cylinders[2], # six cyl
method = "restyle",
args = list("visible", list(F, F, T, T, F, F))),
list(label = cylinders[3], # eight cyl
method = "restyle",
args = list("visible", list(F, F, F, F, T, T)))
) # end buttons
)) # end updatedmenus list list
) # end layout
Here's the 4 cyl plotly and your original 4 cyl ggplot

Related

Plotly grouped barchart : how to create buttons to display different x values R

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")))))

Switching between two plots with groups in Plotly

I have a data frame with 2 columns, one that I want to use as a toggle (so display grp1 or grp2) and another where I want to split the data into different lines. I can't seem to figure out how to get it to work properly with plotly, I think there should be a simple straightforward way to do it but for the life of me I can't get it to stop mixing up the groups.
library(tidyverse)
library(plotly)
library(ggplot2)
# example data my group 1 would be social, group 2 would be grp
df1 = data.frame(grp = "A", social = "Facebook",
days = c("2020-01-01","2020-01-02","2020-01-03","2020-01-04"),
yval = c(0.1, 0.2, 0.3, 0.4))
df2 = df1
df2$grp = "B"
df2$yval = df2$yval + 0.2
df3 = df1
df3$grp = "C"
df3$yval = df3$yval + 0.4
df = rbind(df1, df2, df3)
aux = df
aux$social = "Twitter"
aux$yval = aux$yval + 0.1
df = rbind(aux, df)
rm(aux, df1, df2, df3)
df$days = as.Date(df$days)
df$social_group = paste(df$social, df$grp)
ggplot(data = df, mapping = aes(x = days, y = yval, color = social)) + geom_point() + geom_line() + facet_wrap(facets = ~social)
So what I'm trying to do is to create a plotly that lets me switch between the ggplot facets, by toggling a Facebook or Twitter button.
This is what I currently got, which starts well, but as soon as I toggle the buttons the groups seem to mix, which shouldn't be happening when I consider I'm filtering on another column...
facebook_annotations <- list(
data=df %>% filter(social=="Facebook"),
x=~days,
y=~yval,
color = ~grp,
hovertemplate = paste('%{x}', '<br>Hover text: %{text}<br>'),
text=~days
)
twitter_annotations <- list(
data=df %>% filter(social=="Twitter"),
x=~days,
y=~yval,
color = ~grp,
hovertemplate = paste('%{x}', '<br>Hover text: %{text}<br>'),
text=~days
)
# updatemenus component
updatemenus <- list(
list(
active = 0,
type = "buttons",
buttons = list(
list(
label = "Facebook",
method = "update",
args = list(list(visible = c(TRUE, FALSE)),
list(title = "Facebook",
annotations = list(facebook_annotations, c())))),
list(
label = "Twitter",
method = "update",
args = list(list(visible = c(FALSE, TRUE)),
list(title = "Twitter",
annotations = list(c(), twitter_annotations)))))
)
)
fig <- df %>% plot_ly(type="scatter", mode="lines")
fig <- fig %>% add_lines(
data=df %>% filter(social=="Facebook"),
x=~days,
y=~yval,
color = ~grp,
hovertemplate = paste('%{x}', '<br>Hover text: %{text}<br>'),
text=~days
)
fig <- fig %>% add_lines(
data=df %>% filter(social=="Twitter"),
x=~days,
y=~yval,
color = ~grp,
hovertemplate = paste('%{x}', '<br>Hover text: %{text}<br>'),
text=~days,
visible=FALSE
)
fig <- fig %>% layout(title="Facebook",
xaxis=list(title=""),
yaxis = list(range = c(0, 1), title = "My Title"),
updatemenus=updatemenus)
fig
What am I missing? It's driving me crazy, I'm even considering just adding each group as an individual trace, but that's not really practical when my actual case study has 8 groups...

Using R plotly dropdown menu to select variable and keep ussing the colour variable as a trace

I was adding a variable selector to a plotly graph in R (following the usual approach that consist in hiding the unnecessary traces from the plot)
library(plotly)
dat <- mtcars
dat$cyl <- factor(dat$cyl)
dat$car <- rownames(mtcars)
dat %>%
plot_ly(x = ~car, y = ~mpg,
name='mpg', type='scatter', mode='markers') %>%
add_trace(y = ~hp, name = 'hp', type='scatter', mode='markers') %>%
add_trace(y = ~qsec, name = 'qsec', type='scatter', mode='markers') %>%
layout(
updatemenus = list(
list(
type = "list",
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")
)
)
)
)
The code does the job BUT it overrides the standard use of a group/color variable as a trace selector because we already set up the traces.
The standard use of a color/group variable will be something like this
plot_ly(group_by(dat,cyl), x = ~car, y = ~mpg, color = ~cyl, type='scatter', mode='markers')
NOTE: I use group_by() becuse group = ... is deprecated.
Is possible to add the dropdown menu as a variable selector and still been able to use a color/group variable to hide & show the data of the selected variable?
I tried adding color= ~cyl but, as you can imagine, it doesn’t work.
Thanks in advance!!
Try this. First, I converted the dataset to long format so that the variables become categories of one variable name with values in value. Second, I map name on symbol and cyl on color. Third. I adjust the legend labels so that only cyl shows up in the legend by mapping cyl on name inside add_trace.
library(plotly)
dat <- mtcars
dat$cyl <- factor(dat$cyl)
dat$car <- rownames(mtcars)
dat %>%
tidyr::pivot_longer(c(mpg, hp, qsec)) %>%
plot_ly(x = ~car, y = ~value, color = ~cyl, symbol = ~name) %>%
add_trace(type='scatter', mode='markers', name = ~cyl) %>%
layout(
updatemenus = list(
list(
type = "list",
label = 'Category',
buttons = list(
list(method = "restyle",
args = list('visible', c(TRUE, FALSE, FALSE)),
label = "hp"),
list(method = "restyle",
args = list('visible', c(FALSE, TRUE, FALSE)),
label = "mpg"),
list(method = "restyle",
args = list('visible', c(FALSE, FALSE, TRUE)),
label = "qsec")
)
)
)
)

Looping through R Plotly with subplot and hiding all legend except one

I need to loop through i iteration of factors, and each factor needs to be plotted as one plot in a subplot. What I would like to do is hiding the legend for every iteration bar the first one, and use legendgroup to tie all the legends together. This is what I have done so far:
library(plotly)
library(dplyr)
mtcars %>%
mutate(vs = as.factor(vs)) %>%
group_split(cyl) %>%
lapply(function(i) {
#show.legend <- ifelse(i == 1, TRUE, FALSE)
show.legend <- if(i == 1) {TRUE} else {FALSE}
plot_ly(
data = i
,x = ~gear
,y = ~mpg
,color = ~vs
,type = "bar"
,legendgroup = ~vs
) %>%
layout(
barmode = "stack"
,showlegend = show.legend
)
}) %>%
subplot(
nrows = NROW(.)
,shareX = TRUE
,shareY = TRUE
,titleX = TRUE
,titleY = TRUE
,margin = 0.05
)
However this produces an error and no legend:
Warning messages:
1: In if (i == 1) { :
the condition has length > 1 and only the first element will be used
If I use show.legend <- ifelse(i == 1, TRUE, FALSE) (commented out above), I get multiple legends instead of just once.
I am aware I could do the below, but I need to this in a loop.
p1 <- plot_ly(blah, showlegend = TRUE)
p2 <- plot_ly(blah, showlegend = FALSE)
P3 <- plot_ly(blah, showlegend = FALSE)
subplot(p1,p2,p3)
I believe I am not calling the i iteration properly. As another option I tried case_when:
show.legend <- case_when(
i == 1 ~ TRUE
,i != 1 ~ FALSE
)
However this produces the same result as ifelse.
There are two issues in your code:
i is not 1:3 but your current tibble you are iterating through via lapply (see seq_along below).
That is why you get the warning:
In if (i == 1) { : the condition has length > 1 and only the first
element will be used
showlegend needs to be an argument to plot_ly not to layout because subplot always adopts the layout from one of its plots. see ?subplot and its argument which_layout.
layout options found later in the sequence of plots will override
options found earlier in the sequence
Here is what I think you are after:
library(plotly)
library(dplyr)
tibble_list <- mtcars %>%
mutate(vs = as.factor(vs)) %>%
group_split(cyl)
lapply(seq_along(tibble_list), function(i) {
show_legend <- if (i == 1) {TRUE} else {FALSE}
plot_ly(
data = tibble_list[[i]],
x = ~ gear,
y = ~ mpg,
color = ~ vs,
type = "bar",
legendgroup = ~ vs,
showlegend = show_legend
) %>% layout(barmode = "stack")
}) %>% subplot(
nrows = NROW(.),
shareX = TRUE,
shareY = TRUE,
titleX = TRUE,
titleY = TRUE,
margin = 0.05,
which_layout = 1
)
Please find an offical example here.
library(plotly)
library(dplyr)
## store plot as variable p
p <- mtcars %>%
mutate(vs = as.factor(vs)) %>%
group_split(cyl) %>%
lapply(function(i) {
plot_ly(
data = i
,x = ~gear
,y = ~mpg
,color = ~vs
,type = "bar"
,showlegend = TRUE ## include all legends in stored variable
) %>%
layout(
barmode = "stack"
)
}) %>%
subplot(
nrows = NROW(.)
,shareX = TRUE
,shareY = TRUE
,titleX = TRUE
,titleY = TRUE
,margin = 0.05
)
## remove unwanted legends from plot
for (i in seq(3, length(p[["x"]][["data"]]))) {
p[["x"]][["data"]][[i]][["showlegend"]] <- FALSE
}
## show plot
p

Multiple lines/traces for each button in a Plotly drop down menu in R

I am trying to generate multiple graphs in Plotly for 30 different sales offices. Each graph would have 3 lines: sales, COGS, and inventory. I would like to keep this on one graph with 30 buttons for the different offices. This is the closest solution I could find on SO:
## Create random data. cols holds the parameter that should be switched
l <- lapply(1:100, function(i) rnorm(100))
df <- as.data.frame(l)
cols <- paste0(letters, 1:100)
colnames(df) <- cols
df[["c"]] <- 1:100
## Add trace directly here, since plotly adds a blank trace otherwise
p <- plot_ly(df,
type = "scatter",
mode = "lines",
x = ~c,
y= ~df[[cols[[1]]]],
name = cols[[1]])
## Add arbitrary number of traces
## Ignore first col as it has already been added
for (col in cols[-1]) {
p <- p %>% add_lines(x = ~c, y = df[[col]], name = col, visible = FALSE)
}
p <- p %>%
layout(
title = "Dropdown line plot",
xaxis = list(title = "x"),
yaxis = list(title = "y"),
updatemenus = list(
list(
y = 0.7,
## Add all buttons at once
buttons = lapply(cols, function(col) {
list(method="restyle",
args = list("visible", cols == col),
label = col)
})
)
)
)
print(p)
It works but only on graphs with single lines/traces. How can I modify this code to do the same thing but with graphs with 2 or more traces? or is there a better solution? Any help would be appreciated!
### EXAMPLE 2
#create fake time series data
library(plotly)
set.seed(1)
df <- data.frame(replicate(31,sample(200:500,24,rep=TRUE)))
cols <- paste0(letters, 1:31)
colnames(df) <- cols
#create time series
timeseries <- ts(df[[1]], start = c(2018,1), end = c(2019,12), frequency = 12)
fit <- auto.arima(timeseries, d=1, D=1, stepwise =FALSE, approximation = FALSE)
fore <- forecast(fit, h = 12, level = c(80, 95))
## Add trace directly here, since plotly adds a blank trace otherwise
p <- plot_ly() %>%
add_lines(x = time(timeseries), y = timeseries,
color = I("black"), name = "observed") %>%
add_ribbons(x = time(fore$mean), ymin = fore$lower[, 2], ymax = fore$upper[, 2],
color = I("gray95"), name = "95% confidence") %>%
add_ribbons(x = time(fore$mean), ymin = fore$lower[, 1], ymax = fore$upper[, 1],
color = I("gray80"), name = "80% confidence") %>%
add_lines(x = time(fore$mean), y = fore$mean, color = I("blue"), name = "prediction")
## Add arbitrary number of traces
## Ignore first col as it has already been added
for (col in cols[2:31]) {
timeseries <- ts(df[[col]], start = c(2018,1), end = c(2019,12), frequency = 12)
fit <- auto.arima(timeseries, d=1, D=1, stepwise =FALSE, approximation = FALSE)
fore <- forecast(fit, h = 12, level = c(80, 95))
p <- p %>%
add_lines(x = time(timeseries), y = timeseries,
color = I("black"), name = "observed", visible = FALSE) %>%
add_ribbons(x = time(fore$mean), ymin = fore$lower[, 2], ymax = fore$upper[, 2],
color = I("gray95"), name = "95% confidence", visible = FALSE) %>%
add_ribbons(x = time(fore$mean), ymin = fore$lower[, 1], ymax = fore$upper[, 1],
color = I("gray80"), name = "80% confidence", visible = FALSE) %>%
add_lines(x = time(fore$mean), y = fore$mean, color = I("blue"), name = "prediction", visible = FALSE)
}
p <- p %>%
layout(
title = "Dropdown line plot",
xaxis = list(title = "x"),
yaxis = list(title = "y"),
updatemenus = list(
list(
y = 0.7,
## Add all buttons at once
buttons = lapply(cols, function(col) {
list(method="restyle",
args = list("visible", cols == col),
label = col)
})
)
)
)
p
You were very close!
If for example you want graphs with 3 traces,
You only need to tweak two things:
Set visible the three first traces,
Modify buttons to show traces in groups of three.
My code:
## Create random data. cols holds the parameter that should be switched
library(plotly)
l <- lapply(1:99, function(i) rnorm(100))
df <- as.data.frame(l)
cols <- paste0(letters, 1:99)
colnames(df) <- cols
df[["c"]] <- 1:100
## Add trace directly here, since plotly adds a blank trace otherwise
p <- plot_ly(df,
type = "scatter",
mode = "lines",
x = ~c,
y= ~df[[cols[[1]]]],
name = cols[[1]])
p <- p %>% add_lines(x = ~c, y = df[[2]], name = cols[[2]], visible = T)
p <- p %>% add_lines(x = ~c, y = df[[3]], name = cols[[3]], visible = T)
## Add arbitrary number of traces
## Ignore first col as it has already been added
for (col in cols[4:99]) {
print(col)
p <- p %>% add_lines(x = ~c, y = df[[col]], name = col, visible = F)
}
p <- p %>%
layout(
title = "Dropdown line plot",
xaxis = list(title = "x"),
yaxis = list(title = "y"),
updatemenus = list(
list(
y = 0.7,
## Add all buttons at once
buttons = lapply(0:32, function(col) {
list(method="restyle",
args = list("visible", cols == c(cols[col*3+1],cols[col*3+2],cols[col*3+3])),
label = paste0(cols[col*3+1], " ",cols[col*3+2], " ",cols[col*3+3] ))
})
)
)
)
print(p)
PD: I only use 99 cols because I want 33 groups of 3 graphs

Resources