如何将所有级别的所有 xaxis 动态设置为空?
How to set all xaxis dynamically to empty for all levels?
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 = "")
)
这个plot
有三个传说,每个gear
一个。如何动态地将所有 Xaxis 动态设置为 ""
,而不必明确指定 xaxis = list(title = "")
、xaxis1 = list(title = "")
?
我自己的,半心半意的,attempt/idea是把parse
和eval
放在一个function
里面,像这样,
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()
}
但我认为必须有更优雅的解决方案。如果可能,最好是 plotly
或 tidyverse
解决方案!
不确定 plotly
是否提供了实现此目的的简单选项。但是一种方法是为自己编写一个方便的函数来删除(或设置相同的)所有 x 轴的标题:
# Packages; ####
library(tidyverse)
library(plotly)
set_x_axis_title <- function(p, title = "") {
layout <- p$x$layout
xaxes <- names(layout)[grepl("^xaxis", names(layout))]
for (i in xaxes) {
layout[[i]]["title"] <- title
}
p$x$layout <- layout
p
}
# 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"
) %>%
set_x_axis_title()
#> `summarise()` has grouped output by 'gear'. You can override using the `.groups` argument.
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 = "")
)
这个plot
有三个传说,每个gear
一个。如何动态地将所有 Xaxis 动态设置为 ""
,而不必明确指定 xaxis = list(title = "")
、xaxis1 = list(title = "")
?
我自己的,半心半意的,attempt/idea是把parse
和eval
放在一个function
里面,像这样,
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()
}
但我认为必须有更优雅的解决方案。如果可能,最好是 plotly
或 tidyverse
解决方案!
不确定 plotly
是否提供了实现此目的的简单选项。但是一种方法是为自己编写一个方便的函数来删除(或设置相同的)所有 x 轴的标题:
# Packages; ####
library(tidyverse)
library(plotly)
set_x_axis_title <- function(p, title = "") {
layout <- p$x$layout
xaxes <- names(layout)[grepl("^xaxis", names(layout))]
for (i in xaxes) {
layout[[i]]["title"] <- title
}
p$x$layout <- layout
p
}
# 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"
) %>%
set_x_axis_title()
#> `summarise()` has grouped output by 'gear'. You can override using the `.groups` argument.