This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
length_fib = 25
fibonacci = numeric(length_fib)
fibonacci[1] = 1
fibonacci[2] = 1
for (i in 3:length_fib) {
fibonacci[i] = fibonacci[i-1] + fibonacci[i-2]
}
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.1.8
## ✔ 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 conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
tibble::tibble(index = 1:length_fib, fibonacci,
ydir = rep( c(1, 1, -1, -1), length_fib)[1:length_fib],
xdir = rep(c(1,-1, -1, 1),length_fib)[1:length_fib],
ymov = ydir * fibonacci,
xmov = xdir * fibonacci,
ypos = cumsum(ymov),
xpos = cumsum(xmov),
yprev = lag(ypos),
xprev = lag(xpos)
) %>%
mutate(yprev = replace_na(yprev, 0),
xprev = replace_na(xprev, 0)) %>%
slice(1:3) %>%
ggplot() +
aes(x = xpos, y = ypos) +
geom_point() +
geom_rect(aes(xmin = xprev, xmax = xpos,
ymin = yprev, ymax = ypos, fill = fibonacci), alpha = .8, color = "white", linewidth = .2) +
scale_fill_viridis_c(limits = c(1, 7000), trans = "sqrt") +
coord_equal(expand = .2)