Jonny Polonsky - 1 year ago 71

R Question

I recently saw this image, and wanted to emulate these plots in

`R`

However, being more of a "data-wrangler" than a mathematician, I'm not sure how to do this. Ideally, I would like to use

`ggplot2`

`library(ggplot2)`

ggplot(data.frame(x = c(1, 100)), aes(x = x)) +

stat_function(fun = function(x){1/x}, geom = 'line')

The ideal answer would use

`ggplot2`

`facet_wrap`

Answer Source

A start with facets:

```
library(ggplot2)
library(dplyr)
L <- data_frame(x = 1:100,
y = 1 / x)
O <- data_frame(t = seq(-pi, pi, l = 100),
x = 3 * cos(t),
y = 3 * sin(t))
V <- data_frame(x = -50:50,
y = abs(-2 * x))
E <- data_frame(y = seq(-pi, pi, l = 100),
x = -3 * abs(sin(y)))
pd <- bind_rows(L = L, O = O, V = V, E = E, .id = 'letter')
pd$letter <- factor(pd$letter,
c('L', 'O', 'V', 'E'),
c('y == 1/x', 'x^2 + y^2 == 9', 'y == abs(-2*x)', 'x == -3*abs(sin(y))'))
ggplot(pd, aes(x, y)) +
geom_vline(xintercept = 0) + geom_hline(yintercept = 0) +
geom_path(size = 1.5, col = 'red') +
facet_wrap(~letter, scales = 'free', labeller = label_parsed) +
theme_minimal() +
theme(axis.text = element_blank(), axis.title = element_blank(),
panel.grid = element_blank())
ggsave('LOVE.png', w = 3.5, h = 4, dpi = 300)
```