Suppose I have a categorical predictor (x1 (0 or 1)) and a
continuous predictor (x2 (0 to 16))
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.0 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.1 ✔ tibble 3.2.0
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
# Data pasta
figure1 <- tibble::tribble(
~x2, ~x1, ~y, ~se, ~cil, ~cih,
1L, 0L, 0.1433498, 0.0102115, 0.1233358, 0.1633639,
1L, 1L, 0.2340015, 0.0152235, 0.204164, 0.263839,
2L, 0L, 0.1465447, 0.0086444, 0.1296019, 0.1634875,
2L, 1L, 0.2325735, 0.0128855, 0.2073184, 0.2578285,
3L, 0L, 0.1500868, 0.007142, 0.1360888, 0.1640847,
3L, 1L, 0.2313928, 0.0106269, 0.2105644, 0.2522211,
4L, 0L, 0.1539935, 0.0057057, 0.1428104, 0.1651765,
4L, 1L, 0.2304721, 0.0084632, 0.2138846, 0.2470596,
5L, 0L, 0.1582811, 0.0043462, 0.1497628, 0.1667995,
5L, 1L, 0.229822, 0.0064232, 0.2172328, 0.2424112,
6L, 0L, 0.1629648, 0.0031091, 0.1568711, 0.1690584,
6L, 1L, 0.229451, 0.0045831, 0.2204682, 0.2384337,
7L, 0L, 0.1680574, 0.0021757, 0.163793, 0.1723217,
7L, 1L, 0.2293651, 0.0031882, 0.2231164, 0.2356139,
8L, 0L, 0.1735696, 0.0020443, 0.1695629, 0.1775763,
8L, 1L, 0.2295681, 0.0028656, 0.2239517, 0.2351845,
9L, 0L, 0.1795089, 0.0028925, 0.1738398, 0.185178,
9L, 1L, 0.230061, 0.0038528, 0.2225097, 0.2376123,
10L, 0L, 0.1858791, 0.0042372, 0.1775743, 0.1941839,
10L, 1L, 0.2308423, 0.0054772, 0.2201072, 0.2415773,
11L, 0L, 0.1926796, 0.0058209, 0.1812709, 0.2040883,
11L, 1L, 0.2319077, 0.0073381, 0.2175253, 0.2462902,
12L, 0L, 0.1999051, 0.0075715, 0.1850652, 0.214745,
12L, 1L, 0.2332508, 0.0093144, 0.2149949, 0.2515066,
13L, 0L, 0.2075447, 0.0094667, 0.1889904, 0.226099,
13L, 1L, 0.2348623, 0.0113684, 0.2125806, 0.2571439,
14L, 0L, 0.2155821, 0.0114937, 0.1930548, 0.2381093,
14L, 1L, 0.2367308, 0.0134863, 0.2102981, 0.2631634,
15L, 0L, 0.223995, 0.0136392, 0.1972627, 0.2507272,
15L, 1L, 0.2388428, 0.0156605, 0.2081488, 0.2695368,
16L, 0L, 0.2327557, 0.0158853, 0.2016212, 0.2638903,
16L, 1L, 0.241183, 0.0178842, 0.2061307, 0.2762353,
17L, 0L, 0.2418316, 0.0182097, 0.2061413, 0.2775219,
17L, 1L, 0.2437345, 0.0201491, 0.2042431, 0.283226
)
# Make subset by x1
sub1 <- filter(figure1, x1 == 0)
sub2 <- filter(figure1, x1 == 1)
colors <- c("g1" = "#1B9E77",
"g2" = "#D95F02")
linetypes <- c("g1" = 1,
"g2" = 2)
# Graph
ggplot() +
geom_line(aes(x = sub1$x2, y = sub1$y, color = "g1", lty = "g1"), linewidth = 1) +
geom_line(aes(x = sub2$x2, y = sub2$y, color = "g2", lty = "g2"), linewidth = 1) +
geom_ribbon(aes(x = sub1$x2, y = sub1$y,
ymin = sub1$cil, ymax = sub1$cih, color = "g1"), alpha = 0.09, lty = "dotted") +
geom_ribbon(aes(x = sub2$x2, y = sub2$y,
ymin = sub2$cil, ymax = sub2$cih, color = "g2"), alpha = 0.09, lty = "dotted") +
theme_bw() +
scale_color_manual(values = colors) +
scale_linetype_manual(values = linetypes) +
labs(x = "x",
y = "y",
color = "",
lty = "")
