在 R 中制作 plot_ly 图的子图
Make subplots of plot_ly graph in R
我有一个数据框,可以这样创建:
x = data.frame(metrics=c("type1", "type1", "type1", "orders", "orders", "orders", "mean","mean","mean"), hr=c(6,7,8,6,7,8,6,7,8), actual=c(14,20,34,56,12,34,56,78,89))
我尝试使用 plot_ly 函数绘制散点图。我为它写了一个函数(我需要它是一个函数):
plot <- function(df){
gp <- df %>%
plot_ly(
x = ~ hr,
y = ~ actual,
group = ~ metrics,
hoverinfo = "text",
hovertemplate = paste(
"<b>%{text}</b><br>",
"%{xaxis.title.text}: %{x:+.1f}<br>",
"%{yaxis.title.text}: %{y:+.1f}<br>",
"<extra></extra>"
),
type = "scatter",
mode = "markers",
marker = list(
size = 18,
color = "white",
line = list(color = "black",
width = 1.5)
),
width = 680,
height = 420
)
gp
}
我得到这个情节:
如您所见,所有三个指标都是一对一的图。我如何使用子图将它们中的每一个放在单独的图表上?
使用subplot
,您必须为每个图形创建一个单独的绘图对象。我们可以使用一个循环来做到这一点:
library(plotly)
x = data.frame(
metrics = rep(c("type1", "orders", "mean"), each = 3),
hr = c(6, 7, 8, 6, 7, 8, 6, 7, 8),
actual = c(14, 20, 34, 56, 12, 34, 56, 78, 89)
)
plot <- function(df) {
subplotList <- list()
for(metric in unique(df$metrics)){
subplotList[[metric]] <- df[df$metrics == metric,] %>%
plot_ly(
x = ~ hr,
y = ~ actual,
name = metric,
hoverinfo = "text",
hovertemplate = paste(
"<b>%{text}</b><br>",
"%{xaxis.title.text}: %{x:+.1f}<br>",
"%{yaxis.title.text}: %{y:+.1f}<br>",
"<extra></extra>"
),
type = "scatter",
mode = "markers",
marker = list(
size = 18,
color = "white",
line = list(color = "black",
width = 1.5)
),
width = 680,
height = 420
)
}
subplot(subplotList, nrows = length(subplotList), margin = 0.1)
}
plot(x)
我有一个数据框,可以这样创建:
x = data.frame(metrics=c("type1", "type1", "type1", "orders", "orders", "orders", "mean","mean","mean"), hr=c(6,7,8,6,7,8,6,7,8), actual=c(14,20,34,56,12,34,56,78,89))
我尝试使用 plot_ly 函数绘制散点图。我为它写了一个函数(我需要它是一个函数):
plot <- function(df){
gp <- df %>%
plot_ly(
x = ~ hr,
y = ~ actual,
group = ~ metrics,
hoverinfo = "text",
hovertemplate = paste(
"<b>%{text}</b><br>",
"%{xaxis.title.text}: %{x:+.1f}<br>",
"%{yaxis.title.text}: %{y:+.1f}<br>",
"<extra></extra>"
),
type = "scatter",
mode = "markers",
marker = list(
size = 18,
color = "white",
line = list(color = "black",
width = 1.5)
),
width = 680,
height = 420
)
gp
}
我得到这个情节:
如您所见,所有三个指标都是一对一的图。我如何使用子图将它们中的每一个放在单独的图表上?
使用subplot
,您必须为每个图形创建一个单独的绘图对象。我们可以使用一个循环来做到这一点:
library(plotly)
x = data.frame(
metrics = rep(c("type1", "orders", "mean"), each = 3),
hr = c(6, 7, 8, 6, 7, 8, 6, 7, 8),
actual = c(14, 20, 34, 56, 12, 34, 56, 78, 89)
)
plot <- function(df) {
subplotList <- list()
for(metric in unique(df$metrics)){
subplotList[[metric]] <- df[df$metrics == metric,] %>%
plot_ly(
x = ~ hr,
y = ~ actual,
name = metric,
hoverinfo = "text",
hovertemplate = paste(
"<b>%{text}</b><br>",
"%{xaxis.title.text}: %{x:+.1f}<br>",
"%{yaxis.title.text}: %{y:+.1f}<br>",
"<extra></extra>"
),
type = "scatter",
mode = "markers",
marker = list(
size = 18,
color = "white",
line = list(color = "black",
width = 1.5)
),
width = 680,
height = 420
)
}
subplot(subplotList, nrows = length(subplotList), margin = 0.1)
}
plot(x)