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%)
CodePudding user response:
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
))