AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 79486146
Accepted
Fernando Brito Lopes
Fernando Brito Lopes
Asked: 2025-03-05 18:23:54 +0800 CST2025-03-05 18:23:54 +0800 CST 2025-03-05 18:23:54 +0800 CST

使用 Power Bi 中的 Deneb 在散点图的点周围添加矩形

  • 772

当绘制像这样的散点图 + 围绕应该分组在一起的点的矩形时,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)
    )

谢谢。

powerbi
  • 1 1 个回答
  • 48 Views

1 个回答

  • Voted
  1. Best Answer
    Daniel Marsh-Patrick
    2025-03-06T05:45:04+08:002025-03-06T05:45:04+08:00

    看起来您正在使用数据集中的 [Index] 字段对矩形进行分组,因此您可以使用aggregate转换来合并 x 和 y 编码字段的最小值和最大值。

    这是我的尝试:

    使用 Vega-Lite 复制原始帖子请求

    然后,我们可以根据需要使用聚合值分配给x/ y(和x2/ y2)编码通道。我已将预期层修改如下:

        {
          "transform": [
            {
              "aggregate": [
                {"op": "min", "field": "Date", "as": "min_date"},
                {"op": "max", "field": "Date", "as": "max_date"},
                {"op": "min", "field": "Average", "as": "min_measure"},
                {"op": "max", "field": "Average", "as": "max_measure"}
              ],
              "groupby": ["Index"]
            }
          ],
          "mark": {
            "type": "rect",
            "stroke": "black",
            "strokeWidth": 1,
            "fill": "transparent",
            "cornerRadius": 5,
            "yOffset": 10,
            "y2Offset": -10
          },
          "encoding": {
            "x": {"field": "min_date", "bandPosition": 0.25},
            "x2": {"field": "max_date", "bandPosition": 0.75},
            "y": {"field": "min_measure"},
            "y2": {"field": "max_measure"}
          }
        }
    

    和编码使用相反的值来移动它们,这样它们就不会绘制在与点完全相同的位置。对于具有单个值的组,否则这些看起来像规则标记x。x2bandPosition

    类似地,yOffset和y2Offset值确保绘制的 y 位置不会与您的点重叠marks。

    请注意,我已将第一层中的某些编码移至顶层,以便两个层都可以继承它们,因此您可能也想检查/查看这些编码。

    这是 Vega Editor 中的可用版本,供您探索。


    编辑offset:在提供此答案后,我意识到,对所有渠道使用并完全避免此方法可能才有意义bandPosition。该层现在看起来像这样:

        {
          "transform": [
            {
              "aggregate": [
                {"op": "min", "field": "Date", "as": "min_date"},
                {"op": "max", "field": "Date", "as": "max_date"},
                {"op": "min", "field": "Average", "as": "min_measure"},
                {"op": "max", "field": "Average", "as": "max_measure"}
              ],
              "groupby": ["Index"]
            }
          ],
          "mark": {
            "type": "rect",
            "stroke": "black",
            "strokeWidth": 1,
            "fill": "transparent",
            "cornerRadius": 5,
            "xOffset": -10,
            "x2Offset": 10,
            "yOffset": 10,
            "y2Offset": -10
          },
          "encoding": {
            "x": {"field": "min_date"},
            "x2": {"field": "max_date"},
            "y": {"field": "min_measure"},
            "y2": {"field": "max_measure"}
          }
        }
    

    编辑器中更新了规范

    • 2

相关问题

  • 为什么我的日历显示的是未来几年?

  • 对不直接相关的表进行 DAX 查询

  • 计算两个单独列中两个单独值出现的次数

  • Power BI:杂乱柱形图上 TopN 值的动态颜色

  • 如何按月划分收入周期

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve