绘制不同大小和位置的多个饼图

发布于 2025-02-11 01:21:40 字数 766 浏览 0 评论 0原文

我想做一个多个馅饼。

df <- read.table(text="Position Depth Group Value
A   50  G1  30
A   50  G2  60
A   50  G3  10
A   200 G1  10
A   200 G2  10
A   200 G3  30
B   50  G1  24
B   50  G2  48
B   50  G3  8
B   100 G1  8
B   100 G2  8
B   100 G3  24
C   100 G1  36
C   100 G2  72
C   100 G3  12
C   600 G1  12
C   600 G2  12
C   600 G3  36",
header=TRUE)

位置:不同的采样站点;
深度:抽水深度;
:三种不同的动物;
价值:动物的计数。

我想让这个数字喜欢:
派的大小:组的总数(G1+G2+G3);
派组成:组(G1%,G2%,G3%)

I would like to make a multiple pie plot.

df <- read.table(text="Position Depth Group Value
A   50  G1  30
A   50  G2  60
A   50  G3  10
A   200 G1  10
A   200 G2  10
A   200 G3  30
B   50  G1  24
B   50  G2  48
B   50  G3  8
B   100 G1  8
B   100 G2  8
B   100 G3  24
C   100 G1  36
C   100 G2  72
C   100 G3  12
C   600 G1  12
C   600 G2  12
C   600 G3  36",
header=TRUE)

Position: the different sampling site;
Depth: the sampling water depth;
Group: three different animals;
Value: the count of animals.

I'd like to make the figure like:
the size of pie:total count of Group(G1+G2+G3);
pie composition:Group(G1%, G2%, G3%)
enter image description here

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

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

发布评论

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

评论(1

清秋悲枫 2025-02-18 01:21:40

实现所需结果的一种选项是通过scatspie :: geom_scatterpie,它需要一些数据争吵。首先,您必须将分类位置列转换为数字。其次,我们必须将数字位置转换为与depth的比例。第三,我们必须将value列重命名为valueGEOM_SCATTERPIE要求使用长格式工作的数据)。最后,我选择了半径,使得馅饼区域反映了值的总和。根据您的输出格式,您可能必须对半径进行一些重新缩放,例如乘以3。

library(dplyr)
library(scatterpie)
library(ggplot2)

df <- df |>
  mutate(x = as.numeric(factor(Position)), x = scales::rescale(x, to = range(Depth))) |>
  rename(value = Value) |>
  group_by(Position, Depth) |>
  mutate(r = 2 * sqrt(sum(value) / 2 / pi))

ggplot() +
  geom_scatterpie(aes(x = x, y = Depth, fill = Group, r = 3 * r), data = df, cols = "Group", long_format = TRUE) +
  scale_x_continuous(breaks = unique(df$x), labels = unique(df$Position)) +
  scale_y_reverse(breaks = unique(df$Depth)) +
  coord_fixed()

img src =“ https://i.sstatic.net/rus7g.png”

< /strong>

df <- structure(list(Position = c(
  "A", "A", "A", "A", "A", "A", "B",
  "B", "B", "B", "B", "B", "C", "C", "C", "C", "C", "C"
), Depth = c(
  50L,
  50L, 50L, 200L, 200L, 200L, 50L, 50L, 50L, 100L, 100L, 100L,
  100L, 100L, 100L, 600L, 600L, 600L
), Group = c(
  "G1", "G2", "G3",
  "G1", "G2", "G3", "G1", "G2", "G3", "G1", "G2", "G3", "G1", "G2",
  "G3", "G1", "G2", "G3"
), Value = c(
  30L, 60L, 10L, 10L, 10L, 30L,
  24L, 48L, 8L, 8L, 8L, 24L, 36L, 72L, 12L, 12L, 12L, 36L
)), class = "data.frame", row.names = c(
  NA,
  -18L
))

One option to achieve your desired result would be via the scatterpie::geom_scatterpie which however requires some data wrangling. First you have to convert your categorical Position column to a numeric. Second we have to transform the numeric Position to the same scale as Depth. Third, we have to rename the Value column to value (geom_scatterpie demands that in case of working with data in long format). Finally I have chosen the radius such that the pie area reflects the sum of values. Depending on your output format you probably have to do some rescaling of the radius, e.g. multiplied by 3.

library(dplyr)
library(scatterpie)
library(ggplot2)

df <- df |>
  mutate(x = as.numeric(factor(Position)), x = scales::rescale(x, to = range(Depth))) |>
  rename(value = Value) |>
  group_by(Position, Depth) |>
  mutate(r = 2 * sqrt(sum(value) / 2 / pi))

ggplot() +
  geom_scatterpie(aes(x = x, y = Depth, fill = Group, r = 3 * r), data = df, cols = "Group", long_format = TRUE) +
  scale_x_continuous(breaks = unique(df$x), labels = unique(df$Position)) +
  scale_y_reverse(breaks = unique(df$Depth)) +
  coord_fixed()

DATA

df <- structure(list(Position = c(
  "A", "A", "A", "A", "A", "A", "B",
  "B", "B", "B", "B", "B", "C", "C", "C", "C", "C", "C"
), Depth = c(
  50L,
  50L, 50L, 200L, 200L, 200L, 50L, 50L, 50L, 100L, 100L, 100L,
  100L, 100L, 100L, 600L, 600L, 600L
), Group = c(
  "G1", "G2", "G3",
  "G1", "G2", "G3", "G1", "G2", "G3", "G1", "G2", "G3", "G1", "G2",
  "G3", "G1", "G2", "G3"
), Value = c(
  30L, 60L, 10L, 10L, 10L, 30L,
  24L, 48L, 8L, 8L, 8L, 24L, 36L, 72L, 12L, 12L, 12L, 36L
)), class = "data.frame", row.names = c(
  NA,
  -18L
))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文