Untitled

Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.

library(waffle)
Loading required package: ggplot2
library(extrafont)
Registering fonts with R
library("emojifont")
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ lubridate 1.9.3     ✔ tibble    3.2.1
✔ purrr     1.0.2     ✔ tidyr     1.3.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(showtext)
Loading required package: sysfonts
Loading required package: showtextdb

Attaching package: 'showtextdb'

The following object is masked from 'package:extrafont':

    font_install
font_add(family = "FontAwesome5Free-Solid", regular = "C:\\Users\\wirze\\Downloads\\fa-solid-900.ttf")

extrafont::fonttable() |> 
  dplyr::as_tibble() |> 
  dplyr::filter(grepl("Awesome", FamilyName)) |>  
  select(FamilyName, FontName, fontfile)
# A tibble: 0 × 3
# ℹ 3 variables: FamilyName <chr>, FontName <chr>, fontfile <chr>
font_import()
Importing fonts may take a few minutes, depending on the number of fonts and the speed of the system.
Continue? [y/n] 
Exiting.
# check that Font Awesome is imported
fonts()[grep("Awesome", fonts())]
character(0)
# [1] "FontAwesome"

library(ggforce)
# Define a sequence of x values
x <- seq(0, 1, length.out = 400)

# Define the Beta distributions
beta_distributions <- data.frame(
  x = rep(x, 2),
  y = c(dbeta(x, 2, 8), dbeta(x, 8, 2)),
  id = rep(1:400, 2),
  distribution = factor(rep(c("Arm 1 B(2, 8)", "Arm 2 B(8, 2)"), each = length(x))),
  facet_group = factor(rep(c("1", "2", "3", "4", "5"), each = length(x)/5))
)

patchwork

generate_one <- function(n = 100, id = 1){

n1 = sum(rbinom(n, 1, 0.6))
n2 = n - n1

v1 = rbinom(n1, 1, 0.6)
v2 = rbinom(n2, 1, 0.4)


df <- data.frame(
  result = c(v1, v2),
  arm = c(rep("Arm A", n1), rep("Arm B", n2)),
  id = id
)

return(df)

}

generate_one(id = 1) -> df1
generate_one(id = 2) -> df2
generate_one(id = 3) -> df3
generate_one(id = 4) -> df4
generate_one(id = 5) -> df5
generate_one(id = 6) -> df6

plot_one_hop <- function(df, title){
  
  
df |>   
  group_by(result, arm, id) |>  
  summarize(n = n()) |> 
  ungroup() |>
  mutate(combcolor = case_when(
    result == 1 & arm == "Arm A" ~ ".A1",
    result == 0 & arm == "Arm A" ~ "A0",
    result == 1 & arm == "Arm B" ~ ".B1",
    result == 0 & arm == "Arm B" ~ "B0"
  )) |> 
  arrange(combcolor) |> 
  mutate(result = result |> factor(labels = c("sucess", "fail"))) |> 
ggplot(aes(values = n, fill = combcolor, color = combcolor)) +
  geom_pictogram(
              n_rows = 10,
              # size = 2, 
              # colour = "white",
              label   = "●",
              # inherit.aes = F
              )+
  coord_equal() +
  theme_void() +
  xlim(0, 8)+
  facet_wrap(~ arm, nrow = 1)+
   scale_color_manual(
    values = c("#00ba38","#619cff", alpha("#00ba38", 1/3), alpha("#619cff", 1/3)),
    labels = c("A1", "B1","A0", "B0")
  )+
  theme(
    legend.position = "none",
    plot.background = element_rect(colour = "gray", fill=NA, size=0.1, 
                                   linetype ="solid"),
      # strip.background = element_blank(),
      strip.text.x = element_text(colour = "gray", lineheight = 10),
    plot.title = element_text(hjust = 0.5, colour = "gray"),
    # panel.border = element_rect(colour = "black", fill=NA, size=5)
    )+
    ggtitle(title)
  
  }


plot_one_hop(df1, title = "Experiment 1") -> p1
`summarise()` has grouped output by 'result', 'arm'. You can override using the
`.groups` argument.
Warning: The `size` argument of `element_rect()` is deprecated as of ggplot2 3.4.0.
ℹ Please use the `linewidth` argument instead.
plot_one_hop(df2, title = "Experiment 2") -> p2
`summarise()` has grouped output by 'result', 'arm'. You can override using the
`.groups` argument.
plot_one_hop(df3, title = "Experiment 3") -> p3
`summarise()` has grouped output by 'result', 'arm'. You can override using the
`.groups` argument.
plot_one_hop(df4, title = "Experiment 4") -> p4
`summarise()` has grouped output by 'result', 'arm'. You can override using the
`.groups` argument.
plot_one_hop(df5, title = "Experiment 5") -> p5
`summarise()` has grouped output by 'result', 'arm'. You can override using the
`.groups` argument.
plot_one_hop(df6, title = "Experiment 6") -> p6
`summarise()` has grouped output by 'result', 'arm'. You can override using the
`.groups` argument.
library(patchwork)


p1 + p2 + p3 + p4 + p5 + p6 +
  plot_annotation(
  title = 'Hypothetical Outcomes of 6 Experiments',
  subtitle = 'These 6 plots will reveal yet-untold secrets about our beloved data-set',
  caption = 'Disclaimer: None of these plots are insightful'
) -> pw1

pw1

# ggsave(gw, file="test.svg", width=10, height=8)

linegraph

line_data = data.frame(
    x = c(0:10),
    y = seq(0.5, 1, by=0.05)
  )


line_data |> 
  ggplot(aes(x = x, y = y)) +
  geom_line() +
  geom_point() +
  theme_minimal()+
  ylim(0, 1) -> l1

l1

# l1 +
#   coord_cartesian(clip = "off") +
  # theme(plot.margin = unit(c(0,0,5,0), "cm"))-> l2


l1 / pw1

ggforce

# lines <- data.frame(
#   x = c(0, 12, 15, 9, 6),
#   y = c(0, 20, 4, 15, 5),
#   xend = c(19, 17, 2, 9, 5),
#   yend = c(10, 18, 7, 12, 1),
#   width = c(1, 10, 6, 2, 3),
#   colour = letters[1:5]
# )

pichart

df_pie <- data.frame(
  group = c("r", "r", "r", "a", "a", "a"),
  outcome = c("A", "B", "C", "A", "B", "C"),
  n = c(33, 33, 33, 12, 24, 64)
)

df_pie |> 
  ggplot(aes(label = outcome)) +
  geom_arc_bar(
    aes(x0 = 0, y0 = 0, r0 = 0.6, r = 1, amount = n, fill = outcome), 
    alpha = 0.9, stat = "pie") +
  theme_void()+
  geom_text(aes(x=c(-0.75, 0, 0.75, -0.8, 0.75, 0.25),
                y=c(0.25, -0.75, 0.25, 0, 0, 0.75),label=outcome))+
  coord_equal() +
  facet_wrap(~group)+
  theme(
    legend.position = "bottom",
  )+
  scale_fill_brewer(palette = 1, type = "qual")-> pie1


pie1

df_pie |> 
  ggplot(aes(x = group, y = n, by = group)) +
  geom_bar(aes(fill = outcome), position = "fill", stat="identity")+
  geom_text(aes(label = n), stat = "identity", 
            position = position_fill(1))+
  coord_flip()+
  theme_void()+
  theme(
    legend.position = "bottom",
  )+
  scale_fill_brewer(palette = 1, type = "qual")

misc emoji

df <- df1 |> 
  bind_rows(df2) |> 
  bind_rows(df3) |> 
  bind_rows(df4) |> 
  bind_rows(df5) |> 
  bind_rows(df6)

df |> 
  count(result, arm, id) |> 
  mutate(combcolor = case_when(
    result == 1 & arm == "Arm A" ~ ".A1",
    result == 0 & arm == "Arm A" ~ "A0",
    result == 1 & arm == "Arm B" ~ ".B1",
    result == 0 & arm == "Arm B" ~ "B0"
  )) |> 
  arrange(combcolor) |> 
  mutate(result = result |> factor(labels = c("sucess", "fail"))) |> 
ggplot() +
  geom_waffle(aes(values = n, fill = combcolor), n_rows = 5, size = 2, colour = "white", geom = "person", inherit.aes = F, glyph = "male") +
  coord_equal() +
  theme_void() + 
  facet_grid(id~ arm)+
   scale_fill_manual(
    values = c("#00ba38","#619cff", alpha("#00ba38", 1/3), alpha("#619cff", 1/3)),
    labels = c("A1", "B1","A0", "B0")
  )+
  theme(legend.position = "bottom")

df |> 
  count(result, arm, id) |> 
  mutate(combcolor = case_when(
    result == 1 & arm == "Arm A" ~ ".A1",
    result == 0 & arm == "Arm A" ~ "A0",
    result == 1 & arm == "Arm B" ~ ".B1",
    result == 0 & arm == "Arm B" ~ "B0"
  )) |> 
  arrange(combcolor) |> 
  mutate(result = result |> factor(labels = c("sucess", "fail"))) |> 
ggplot() +
    geom_pictogram(aes(values = n, fill = combcolor), n_rows = 5, size = 2, colour = "white", label  = "male", inherit.aes = F) +
  coord_equal() +
  theme_void() + 
  facet_grid(id~ arm)+
   scale_fill_manual(
    values = c("#00ba38","#619cff", alpha("#00ba38", 1/3), alpha("#619cff", 1/3)),
    labels = c("A1", "B1","A0", "B0")
  )+
  theme(legend.position = "bottom")

# ?geom_pictogram
df |> 
  count(result, arm, id) |> 
  mutate(combcolor = case_when(
    result == 1 & arm == "Arm A" ~ ".A1",
    result == 0 & arm == "Arm A" ~ "A0",
    result == 1 & arm == "Arm B" ~ ".B1",
    result == 0 & arm == "Arm B" ~ "B0"
  )) |> 
    arrange(combcolor) |> 
  mutate(result = result |> factor(labels = c("sucess", "fail"))) |> 
  ggplot(
    aes(label = result, values = n)
  ) +
  geom_pictogram(
    n_rows = 5, 
    aes(colour = combcolor), 
    # flip = TRUE, 
    make_proportional = F,
    size = 6
  ) +
  scale_label_pictogram(
    name = NULL,
    values = c("male", "male", "male", "male")
  ) +
  coord_equal() +
  theme_enhance_waffle() +
  theme(
    legend.key.height = unit(1, "line"),
    legend.text = element_text(size = 14, hjust = 0, vjust = 0.75)
  ) +
    facet_grid(id~ arm)+
   scale_color_manual(
     name = NULL,
    values = c("#00ba38","#619cff", alpha("#00ba38", 1/3), alpha("#619cff", 1/3)),
    labels = c("A1", "B1","A0", "B0")
  )+
  theme_void()