I'm using the Gmisc package in R to design a flowchart based on nested categorical data, similar to the gtcars example. My main challenge: I can't correctly stack many subcategories (akin to mfr in gtcars) under their respective ctry_origin categories. Despite referencing the package's vignette, I'm still facing positioning issues. Can anyone offer advice on this layout?
Data:
library(Gmisc)
library(gt)
gtcars %>% select(ctry_origin, mfr) %>% filter(ctry_origin %in% c("Germany", "Italy", "Japan")) %>% group_by(ctry_origin, mfr) %>%
summarise(count = n()) %>%
arrange(ctry_origin, desc(count))
How I would like my flowchart to look like using mermaid() from the DiagrammeR package.
# Stackoverlflow
mermaid("
graph TB
A[Cars]-->B[Germany 16]
A[Cars]-->C[Italy 15]
A[Cars]-->D[Japan 2]
B-->1[Audi 5]
1---2[BMW 5]
2---3[Porsche 4]
3---4[Mercedes-Benz 2]
C---5[Ferrari 9]
5---6[Lamborghini 3]
6---7[Maserati 3]
D---8[Acura 1]
8---9[Nissan 1]
")
My attempt:
### Flow chart with my data
# Create the boxes
cars <- boxGrob(glue("Cars",
"n = {n}",
n = txtInt(33),
.sep = "\n"),
x = 0.5,
y = 0.9)
cars.ger <- boxGrob(glue("Germany",
"n = {n}",
n = txtInt(16),
.sep = "\n"),
x = 0.2,
y = 0.5)
cars.ita <- boxGrob(glue("Italy",
"n = {incl}",
incl = txtInt(15),
.sep = "\n"),
x = 0.5,
y = 0.5)
cars.jap <- boxGrob(glue("Japan",
"n = {recr}",
recr = txtInt(2),
.sep = "\n"),
x = 0.8,
y = 0.5)
cars.audi <- boxGrob(glue("Audi",
"n = {n}",
n = txtInt(5),
.sep = "\n"),
x = 0.2,
y = 0.7)
cars.bmw <- boxGrob(glue("BMW",
"n = {incl}",
incl = txtInt(5),
.sep = "\n"),
x = 0.2,
y = 0.7)
cars.porsche <- boxGrob(glue("Porsche",
"n = {recr}",
recr = txtInt(4),
.sep = "\n"),
x = 0.2,
y = 0.7)
cars.mercedes <- boxGrob(glue("Mercedes",
"n = {n}",
n = txtInt(2),
.sep = "\n"),
x = 0.2,
y = 0.7)
cars.ferrari <- boxGrob(glue("Ferrari",
"n = {incl}",
incl = txtInt(9),
.sep = "\n"),
x = 0.5,
y = 0.7)
cars.lamborghini <- boxGrob(glue("Lamborghini",
"n = {recr}",
recr = txtInt(3),
.sep = "\n"),
x = 0.5,
y = 0.7)
cars.maserati <- boxGrob(glue("Maserati",
"n = {n}",
n = txtInt(3),
.sep = "\n"),
x = 0.5,
y = 0.7)
cars.acura <- boxGrob(glue("Acura",
"n = {incl}",
incl = txtInt(1),
.sep = "\n"),
x = 0.7,
y = 0.7)
cars.nissan <- boxGrob(glue("Nissan",
"n = {recr}",
recr = txtInt(1),
.sep = "\n"),
x = 0.7,
y = 0.)
grid.newpage()
cars
cars.ger
cars.ita
cars.jap
# Connect main boxes with arrows
connectGrob(cars, cars.ger, "N")
connectGrob(cars, cars.ita, "v")
connectGrob(cars, cars.jap, "N")
spread_boxes <- spreadVertical(cars.ger, cars.audi,
cars.bmw,.type = "between")
for (b in spread_boxes) {
print(b)
}