-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreg_two_way.rmd
More file actions
33 lines (29 loc) · 857 Bytes
/
reg_two_way.rmd
File metadata and controls
33 lines (29 loc) · 857 Bytes
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
---
title: "Plot two way regression with ggplot package"
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = F, warning = F)
```
## get regline for x ~ y and y ~ x with ggplot
```{r}
library(tidyverse)
library(ggpubr)
tibble(x = rnorm(50, 2, 2),
y = 2 * x + rnorm(50, 0, 1)) %>%
gather(key = "vars", value = "values") %>%
mutate(id = 1:nrow(.)) %>%
relocate("id", .before = "vars") %>%
mutate(values2 = values[c(51:nrow(.),
1:50)],
id2 = case_when(vars == "x" ~ "x ~ y",
TRUE ~ "y ~ x")) %>%
ggplot(aes(x = values, y = values2, colour = id2)) +
geom_point() +
geom_smooth(method = "lm", aes(group = id2),
formula = y ~ x, se = FALSE) +
labs(x = "x", y = "y", title = "Two way regression
in one plot", caption = "made by stats9") +
theme_bw() +
guides(color = guide_legend(title = "formula"))
```