当绘制像这样的散点图 + 围绕应该分组在一起的点的矩形时,Deneb(Vega-Lite)规范会是什么样的?
以下代码创建散点图,但我不确定如何创建矩形。
{
"data": {
"values": [
{"Average": 6.01,"Groups": "Group1","Index": 6,"Date": "2023-09-16"},
{"Average": 13.21,"Groups": "Group1","Index": 8,"Date": "2023-11-04"},
{"Average": 3.63,"Groups": "Group1","Index": 8,"Date": "2023-12-23"},
{"Average": 5.91,"Groups": "Group1","Index": 7,"Date": "2024-02-10"},
{"Average": 6.19,"Groups": "Group1","Index": 10,"Date": "2024-03-30"},
{"Average": 3.97,"Groups": "Group1","Index": 10,"Date": "2024-05-18"},
{"Average": -1.52,"Groups": "Group1","Index": 10,"Date": "2024-07-06"},
{"Average": 1.28,"Groups": "Group1","Index": 10,"Date": "2024-08-24"},
{"Average": 3.39,"Groups": "Group1","Index": 10,"Date": "2024-10-15"},
{"Average": 1.21,"Groups": "Group1","Index": 23,"Date": "2024-12-03"},
{"Average": -0.13,"Groups": "Group1","Index": 15,"Date": "2025-01-21"},
{"Average": 4.49,"Groups": "Group1","Index": 16,"Date": "2025-03-11"},
{"Average": 34.97,"Groups": "Group2","Index": 6,"Date": "2023-09-16"},
{"Average": 25.14,"Groups": "Group2","Index": 8,"Date": "2023-11-04"},
{"Average": 27.59,"Groups": "Group2","Index": 8,"Date": "2023-12-23"},
{"Average": 27.2,"Groups": "Group2","Index": 7,"Date": "2024-02-10"},
{"Average": 23.91,"Groups": "Group2","Index": 10,"Date": "2024-03-30"},
{"Average": 26.29,"Groups": "Group2","Index": 10,"Date": "2024-05-18"},
{"Average": 26.43,"Groups": "Group2","Index": 10,"Date": "2024-07-06"},
{"Average": 25.21,"Groups": "Group2","Index": 10,"Date": "2024-08-24"},
{"Average": 25.51,"Groups": "Group2","Index": 10,"Date": "2024-10-15"},
{"Average": 38.46,"Groups": "Group2","Index": 23,"Date": "2024-12-03"},
{"Average": 46.44,"Groups": "Group2","Index": 15,"Date": "2025-01-21"},
{"Average": 56.63,"Groups": "Group2","Index": 16,"Date": "2025-03-11"},
{"Average": 17.39,"Groups": "Group3","Index": 6,"Date": "2023-09-16"},
{"Average": 9.15,"Groups": "Group3","Index": 8,"Date": "2023-11-04"},
{"Average": 7.46,"Groups": "Group3","Index": 8,"Date": "2023-12-23"},
{"Average": 6.62,"Groups": "Group3","Index": 7,"Date": "2024-02-10"},
{"Average": 4.15,"Groups": "Group3","Index": 10,"Date": "2024-03-30"},
{"Average": 5.52,"Groups": "Group3","Index": 10,"Date": "2024-05-18"},
{"Average": 6.08,"Groups": "Group3","Index": 10,"Date": "2024-07-06"},
{"Average": 5.54,"Groups": "Group3","Index": 10,"Date": "2024-08-24"},
{"Average": 5.77,"Groups": "Group3","Index": 10,"Date": "2024-10-15"},
{"Average": 5.23,"Groups": "Group3","Index": 23,"Date": "2024-12-03"},
{"Average": 4.83,"Groups": "Group3","Index": 15,"Date": "2025-01-21"},
{"Average": 9.56,"Groups": "Group3","Index": 16,"Date": "2025-03-11"
}
]
},
"layer": [
{
"mark": {
"type": "point"
},
"encoding": {
"y": {
"field": "Average",
"type": "quantitative",
"title": null
},
"x": {
"field": "Date",
"type": "ordinal",
"title": null
},
"color": {
"field": "Groups",
"type": "nominal"
},
"shape": {
"field": "Groups"
}
}
},
{
"mark": {
"type": "rule",
"stroke": "black",
"strokeWidth": 1
},
"encoding": {
"x": {
"field": "MinDate",
"type": "ordinal"
}
},
"transform": [
{
"aggregate": [
{
"op": "min",
"field": "Date",
"as": "MinDate"
}
],
"groupby": ["Index"]
},
{
"calculate": "datum.MinDate - 1",
"as": "MinDate"
}
]
},
{
"mark": {
"type": "rule",
"stroke": "black",
"strokeWidth": 1
},
"encoding": {
"x": {
"field": "MaxDate",
"type": "ordinal"
}
},
"transform": [
{
"aggregate": [
{
"op": "max",
"field": "Date",
"as": "MaxDate"
}
],
"groupby": ["Index"]
},
{
"calculate": "datum.MaxDate + 1",
"as": "MaxDate"
}
]
}
]
}
此代码返回以下 plo,并且 x 轴中的日期看起来不太好并且它有一个 NaN。
是否可以从垂直线中移除 x 轴并仅保留点中的 x 轴?
geom_mark_rect
在 R 中创建所需的输出。使用 R 包中的函数自动创建矩形ggforce
。它基于Index
列。
我可以使用 R visual 在 Power BI 中创建这个图,但速度很慢。
require("ggplot2")
library(ggplot2)
require("ggforce")
library(ggforce)
gt_shape <- 1:length(unique(dataset$Groups))
gt_ymax <- ceiling(max(dataset$Average) + (0.15 * max(dataset$Average)))
gt_ymin <- floor(min(dataset$Average) - (0.15 * min(dataset$Average)))
ggplot(dataset, aes(x = Date, y = Average, group = as.character(Index))) +
geom_point(aes(shape = Groups, color = Groups, fill = Groups), stroke = 1, na.rm = TRUE) +
geom_mark_rect(show.legend = FALSE) +
theme_minimal() +
ylim(gt_ymin, gt_ymax) +
labs(
y = "",
x ="Date"
) +
scale_shape_manual(values = gt_shape) +
scale_colour_viridis_d(option = "turbo") +
scale_fill_viridis_d(option = "turbo", guide = "none") +
theme(
legend.position = "bottom",
text = element_text(colour = "black"),
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)
)
谢谢。