使用 plotly 时如何在绘图标题和文件名中包含唯一的分组变量?
How to include unique grouping variables in plot titles and file names when using plotly?
我正在学习使用plotly
。我有一个类似于下面创建的 data
的数据集。使用下面的代码创建的 .html 图可以按我希望的方式工作;为每个独特的 ID
创建独特的 "interactive" 图。但是,我无法合并我的分组变量 Group
。我希望分组变量的名称包含在每个图的标题中,以及每个文件的名称中。例如,下面创建的图的标题将显示 "plot_for_ID_A"
。我想让它说 "plot_for_Alpha_A"
,Alpha
指的是分组变量。我在粘贴函数中尝试了多种方法,我尝试将 filter(group==.y)
放在 filter()
调用下,以便我可以在 paste()
调用中引用 .y
,以及其他几个没有用的东西。最好的方法是什么?
library(tidyverse)
library(plotly)
ID <- rep(c("A","B","C","D","E","F"), each=1000)
Group <- rep(c("alpha","beta"), each = 3000)
time <- rep(c(1:1000), times = 6)
one <- rnorm(6000)
two <- rnorm(6000)
three <- rnorm(6000)
four <- rnorm(6000)
five<-rnorm(6000)
data <- data.frame(cbind(ID,Group,time,one,two,three,four,five),
stringsAsFactors = FALSE)
data_long <- data %>%
pivot_longer(-c(ID:time), names_to="Variable", values_to="Value")%>%
mutate(time = as.numeric(time),
value = as.numeric(Value))
walk(unique(data_long$ID),
~ htmlwidgets::saveWidget(ggplotly(data_long %>%
filter(ID == .x) %>%
ggplot(aes(x = time,
y = value,
color = Variable)) +
geom_point() +
labs(title = paste(.x))),
paste("plot_for_ID_", .x, ".html", sep = "")))
此外,此代码的输出会为每个绘图创建一个唯一的 .html 文件。有没有办法将所有图粘贴到一个文件中,每个图都有自己的页面?
您可以使用 group_split
基于组拆分数据并从其列值中获取绘图的名称。
library(tidyverse)
library(plotly)
data_long %>%
group_split(ID, Group) %>%
map(~ htmlwidgets::saveWidget(ggplotly(
ggplot(.x, aes(x = time,
y = value,
color = Variable)) +
geom_point() +
labs(title = paste0("plot_for_", .x[[2]][1], "_", .x[[1]][1]))),
paste0("plot_for_", .x[[2]][1], "_", .x[[1]][1], ".html")))
我正在学习使用plotly
。我有一个类似于下面创建的 data
的数据集。使用下面的代码创建的 .html 图可以按我希望的方式工作;为每个独特的 ID
创建独特的 "interactive" 图。但是,我无法合并我的分组变量 Group
。我希望分组变量的名称包含在每个图的标题中,以及每个文件的名称中。例如,下面创建的图的标题将显示 "plot_for_ID_A"
。我想让它说 "plot_for_Alpha_A"
,Alpha
指的是分组变量。我在粘贴函数中尝试了多种方法,我尝试将 filter(group==.y)
放在 filter()
调用下,以便我可以在 paste()
调用中引用 .y
,以及其他几个没有用的东西。最好的方法是什么?
library(tidyverse)
library(plotly)
ID <- rep(c("A","B","C","D","E","F"), each=1000)
Group <- rep(c("alpha","beta"), each = 3000)
time <- rep(c(1:1000), times = 6)
one <- rnorm(6000)
two <- rnorm(6000)
three <- rnorm(6000)
four <- rnorm(6000)
five<-rnorm(6000)
data <- data.frame(cbind(ID,Group,time,one,two,three,four,five),
stringsAsFactors = FALSE)
data_long <- data %>%
pivot_longer(-c(ID:time), names_to="Variable", values_to="Value")%>%
mutate(time = as.numeric(time),
value = as.numeric(Value))
walk(unique(data_long$ID),
~ htmlwidgets::saveWidget(ggplotly(data_long %>%
filter(ID == .x) %>%
ggplot(aes(x = time,
y = value,
color = Variable)) +
geom_point() +
labs(title = paste(.x))),
paste("plot_for_ID_", .x, ".html", sep = "")))
此外,此代码的输出会为每个绘图创建一个唯一的 .html 文件。有没有办法将所有图粘贴到一个文件中,每个图都有自己的页面?
您可以使用 group_split
基于组拆分数据并从其列值中获取绘图的名称。
library(tidyverse)
library(plotly)
data_long %>%
group_split(ID, Group) %>%
map(~ htmlwidgets::saveWidget(ggplotly(
ggplot(.x, aes(x = time,
y = value,
color = Variable)) +
geom_point() +
labs(title = paste0("plot_for_", .x[[2]][1], "_", .x[[1]][1]))),
paste0("plot_for_", .x[[2]][1], "_", .x[[1]][1], ".html")))