使用ggplot2在R中不均匀的binning及其在R中的绘图

发布于 2025-01-17 14:50:27 字数 413 浏览 4 评论 0原文

有没有办法在 geom_bin_2d 的 binwidth 内使用数字向量?我想创建一个具有不规则/不均匀分箱长度的分箱热图,但我不知道该怎么做。

一个可重现的例子

library(tidyverse)

data <- tibble(x = sample(100),
               y = sample(100)) %>%
  mutate(x = x * 1.2,
         y = y * 0.8)

binX <- c(18, 39, 60, 81, 102, 120)
binY <- c(18, 30, 50, 62, 80)

data %>%
ggplot() +
geom_bin_2d(aes(x = x, y = y), binwidth = c(binX, binY))

Is there any way to use a vector of numbers within the binwidth for geom_bin_2d? I would like to create a binned heatmap with irregular/uneven bin lengths, but I am not sure how to do it.

A reproducible example

library(tidyverse)

data <- tibble(x = sample(100),
               y = sample(100)) %>%
  mutate(x = x * 1.2,
         y = y * 0.8)

binX <- c(18, 39, 60, 81, 102, 120)
binY <- c(18, 30, 50, 62, 80)

data %>%
ggplot() +
geom_bin_2d(aes(x = x, y = y), binwidth = c(binX, binY))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纸短情长 2025-01-24 14:50:27

您可以将列表中的 x 和 y bin 传递给 breaks 参数:

ggplot(data) + geom_bin_2d(aes(x, y), breaks = list(binX, binY))

< img src="https://i.sstatic.net/WywhF.png" alt="在此处输入图像描述">

You can pass the x and y bins in a list to the breaks argument:

ggplot(data) + geom_bin_2d(aes(x, y), breaks = list(binX, binY))

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文