summaryrefslogtreecommitdiff
path: root/images/twomoons.R
blob: c825abd1682d36b4a7b736703b8fd7563f39a3a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env Rscript
t <- seq (0, 1, length.out = 101)
labeled <- c (rep (0, 100), 1)
upper_x <- cos (t * pi) + 0.5
upper_y <- sin (t * pi)
lower_x <- -upper_x
lower_y <- -upper_y

`%>%` <- magrittr::`%>%`

data <- (tibble::tibble (x = upper_x, y = upper_y,
                         label = c (rep ("unlabeled", 100), "+"),
                         `semi-supervised solution` = "+")
    %>% rbind (tibble::tibble (x = lower_x, y = lower_y,
                               label = c (rep ("unlabeled", 100), "-"),
                               `semi-supervised solution` = "-"))
    %>% dplyr::mutate (noise_x = 0.1 * rnorm (dplyr::n ()),
                       noise_y = 0.1 * rnorm (dplyr::n ()))
    %>% dplyr::mutate (x = x + noise_x, y = y + noise_y)
    %>% dplyr::mutate (`naive solution` = ifelse (x <= 0, "+", "-")))

scales <- function (plot) {
    (plot
        + ggplot2::scale_size_manual (values = c ("unlabeled" = 1, "+" = 3, "-" = 3))
        + ggplot2::scale_shape_manual (values = c ("+" = 16, "-" = 17, "unlabeled" = 3))
        + ggplot2::scale_color_manual (values = c ("+" = "#E69F00", "-" = "#56B4E9", "unlabeled" = "#009E73"))
        + ggplot2::theme_void ()
        + ggplot2::theme(legend.position="none")
        + ggplot2::coord_fixed ())
}

question <- ((data
    %>% ggplot2::ggplot (ggplot2::aes (x = x, y = y, shape = label, color = label, size = label))
    + ggplot2::geom_point ())
    %>% scales ())

naive_answer <- ((data
    %>% ggplot2::ggplot (ggplot2::aes (x = x, y = y, shape = `naive solution`, color = `naive solution`))
    + ggplot2::geom_point ()
    + ggplot2::geom_vline (xintercept = 0))
    %>% scales ())

semi_supervised_answer <- ((data
    %>% ggplot2::ggplot (ggplot2::aes (x = x, y = y, shape = `semi-supervised solution`, color = `semi-supervised solution`))
    + ggplot2::geom_point ())
    %>% scales ())

plot <- gridExtra::arrangeGrob (question, gridExtra::arrangeGrob (naive_answer, semi_supervised_answer, ncol = 2), nrow = 2)
filename <- Sys.getenv ("OUTPUT")
ggplot2::ggsave (filename, plot, device = "svg", width = 3.5, height = 3.5)