MWE:
# Packages; ####
library(tidyverse)
library(plotly)
# Plot; #####
mtcars %>%
group_by(gear,am) %>%
summarise(
mpg = mean(mpg)
) %>% do(
plot = plot_ly(
data = .,
x = ~gear,
y = ~mpg
) %>% add_bars(
name = ~am
)
) %>% subplot(
nrows = 1,
shareY = TRUE,
shareX = TRUE
) %>% layout(
title = "Facetted Plot",
xaxis = list(title = "")
)
This plot has three legends, one for each gear. How do I dynamically set all Xaxis dynamically to "", without having to explicitly specify xaxis = list(title = ""), xaxis1 = list(title = "")?
My own, halfhearted, attempt/idea was to parse and eval inside a function like this,
plot_function <- function(grouping_vars) {
# Baseplot; ####
base_plot = mtcars %>%
group_by(!!sym(grouping_vars[1]),!!sym(grouping_vars[2])) %>%
summarise(
mpg = mean(mpg)
) %>% do(
plot = plot_ly(
data = .,
x = ~!!sym(grouping_vars[1]),
y = ~mpg
) %>% add_bars(
name = ~am
)
) %>% subplot(
nrows = 1,
shareY = TRUE,
shareX = TRUE
)
# Remove all Xaxis Labels; ####
# Count number of unique values in grouping var;
no_unique <- mtcars %>%
select(grouping_vars[1]) %>%
unique() %>%
nrow()
gen_layout <- 1:no_unique %>% map(
.f = function(i) {
"some expression here"
}
)
base_plot %>%
gen_layout %>%
parse(text = .) %>%
eval()
}
But I reckon there must be a more elegant solution to this. Preferably a plotly or tidyverse solution if possible!
