问题:
我有一组分层数据,我想在其中生成分组箱线图。按一个变量着色并按另一个变量填充。这可以在 ggplot2 中轻松完成(提供示例)。但我无法使用plotly() 实现相同的目的。注意:我不想使用
ggplotly()
,因为与plotly()相比,它在处理大型数据集时要慢得多。
奖励:如何使用plotly()解决方案手动定义“group_c”颜色,这可以通过ggplot2中的
scale_colour_manual()
来完成。
数据:
group_a <- 900:904
group_b <- paste0(rep(group_a, each = 3), '_', rep(c('01', '02', '03'), times = length(group_a)))
group_c <- rep(c("A", "B"), times = length(group_b))
group_date <- seq.Date(from = as.Date("2025-01-01"), by = "day", length.out = length(lot_ids))
group_date <- rep(group_date, each = 3)
x <- rep(runif(14), times = length(group_a) * 3 * 2)
# Create the dataframe
df <- data.frame(
group_a = rep(group_a, each = 3 * 2 * 14),
group_b = rep(group_b, each = 14 * 2),
group_date = rep(group_date, each = 2 * 14),
group_c = group_c,
x = x
)
ggplot方法:
df %>%
ggplot(aes(
x = group_date,
y = x,
group = interaction(group_c, group_b),
fill = factor(group_a),
colour = group_c
)) +
geom_boxplot()
你的意思是这样吗?
library(plotly)
library(dplyr)
group_a <- 900:904
group_b <- paste0(rep(group_a, each = 3), '_', rep(c('01', '02', '03'), times = length(group_a)))
group_c <- rep(c("A", "B"), times = length(group_b))
group_date <- seq.Date(from = as.Date("2025-01-01"), by = "day", length.out = length(group_a))
group_date <- rep(group_date, each = 3)
x <- rep(runif(14), times = length(group_a) * 3 * 2)
df <- data.frame(
group_a = rep(group_a, each = 3 * 2 * 14),
group_b = rep(group_b, each = 14 * 2),
group_date = rep(group_date, each = 2 * 14),
group_c = group_c,
x = x
)
plot_ly(data = df) %>%
add_boxplot(
x = ~group_date,
y = ~x,
type = "box",
name = ~interaction(group_b),
color = ~factor(group_a),
transforms = list(
list(
type = 'groupby',
groups = ~group_c,
styles = list(
list(target = "A", value = list(line = list(color = "red"))),
list(target = "B", value = list(line = list(color = "blue")))
)
)
)
) %>%
layout(
boxmode = "group",
boxgap = 0,
boxgroupgap = 0
)