在子图的每个图上放置几个散点图 [英] Put several scatterplots on each graph of subplots
问题描述
我有一个数据框,可以使用以下代码创建该数据框:
x = data.frame(metrics=c("type1", "type1", "type1","type1", "type1", "type1", "orders", "orders", "orders","orders", "orders", "orders", "mean","mean","mean","mean","mean","mean"), hr=c(6,7,8,6,7,8,6,7,8,6,7,8,6,7,8,6,7,8), actual=c(14,20,34,22,24,27,56,12,34,11,15,45,56,78,89,111,123,156), time=c("today", "yesterday", "today", "yesterday", "today", "yesterday"))
我想使用plot_ly函数可视化此数据.我想为度量"列中的每种类型的值创建三个子图.而且,在每个子图上,对于时间"列中的每种类型的值,必须有两个散点图. (今天,昨天).
我做了所有的工作,除了对时间"列中的每种类型的值进行两次散点图.是不同的颜色: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)
我得到这张图:
如何使每个子图中的这两个散点图具有不同的颜色?
以下是我想你所追求的:
library(plotly)
x = data.frame(metrics=c("type1", "type1", "type1","type1", "type1", "type1", "orders", "orders", "orders","orders", "orders", "orders", "mean","mean","mean","mean","mean","mean"), hr=c(6,7,8,6,7,8,6,7,8,6,7,8,6,7,8,6,7,8), actual=c(14,20,34,22,24,27,56,12,34,11,15,45,56,78,89,111,123,156), time=c("today", "yesterday", "today", "yesterday", "today", "yesterday"))
plot <- function(df) {
subplotList <- list()
for(metric in unique(df$metrics)){
subplotList[[metric]] <- df[df$metrics == metric,] %>%
plot_ly(
x = ~ hr,
y = ~ actual,
name = ~ paste(metrics, " - ", time),
colors = ~ time,
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(width = 1.5)
),
width = 680,
height = 420
)
}
subplot(subplotList, nrows = length(subplotList), margin = 0.1)
}
plot(x)
先前的问题,以供将来的读者使用.>
I have a dataframe, which can be created with this code:
x = data.frame(metrics=c("type1", "type1", "type1","type1", "type1", "type1", "orders", "orders", "orders","orders", "orders", "orders", "mean","mean","mean","mean","mean","mean"), hr=c(6,7,8,6,7,8,6,7,8,6,7,8,6,7,8,6,7,8), actual=c(14,20,34,22,24,27,56,12,34,11,15,45,56,78,89,111,123,156), time=c("today", "yesterday", "today", "yesterday", "today", "yesterday"))
I want to visualise this data using plot_ly function. I want to create three subplots for each type of values in column "metrics". Also, on each subplot there must be two scatterplots for each type of value in column "time" (today, yesterday).
I did all, except making two scatterplots for each type of value in column "time" be different colours:
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)
I get this graph:
How to make these two scatterplots on each subplots be in different colours?
Here is what I think you are after:
library(plotly)
x = data.frame(metrics=c("type1", "type1", "type1","type1", "type1", "type1", "orders", "orders", "orders","orders", "orders", "orders", "mean","mean","mean","mean","mean","mean"), hr=c(6,7,8,6,7,8,6,7,8,6,7,8,6,7,8,6,7,8), actual=c(14,20,34,22,24,27,56,12,34,11,15,45,56,78,89,111,123,156), time=c("today", "yesterday", "today", "yesterday", "today", "yesterday"))
plot <- function(df) {
subplotList <- list()
for(metric in unique(df$metrics)){
subplotList[[metric]] <- df[df$metrics == metric,] %>%
plot_ly(
x = ~ hr,
y = ~ actual,
name = ~ paste(metrics, " - ", time),
colors = ~ time,
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(width = 1.5)
),
width = 680,
height = 420
)
}
subplot(subplotList, nrows = length(subplotList), margin = 0.1)
}
plot(x)
Previous question for future readers.
这篇关于在子图的每个图上放置几个散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!