Making a Forest Fire in R

Making a Forest Fire in RPatricio CarrascoBlockedUnblockFollowFollowingJan 8Maybe you’re like me and you need to reign in your desires to spontaneously start fires.

Maybe you’re interested in using programming to do cool stuff.

Let’s make a forest fire in R!We’ll be coding this in R.

If you don’t have it yet, you can install using homebrew if on mac:brew install RIf you’re on windows, you can download R from https://cran.



Once you’ve installed R to your computer, download and install Rstudio from https://www.


com/products/rstudio/download/#downloadOpen it up and let’s get started!PreworkBefore we get started, we’re going to install the libraries we’ll be using for our simulation.

If you’re new to R, you can install libraries by running:install.

packages("package_name") # note: package_name is a stringYou only need to do this once!.Once a package is installed on your computer, you can load it into a project using:library(package_name) # note: package_name is not a stringOkay, now that we’ve installed all of our dependancies, we can start to make the model.

First, let’s load in the libraries we’ll need to use to make the simulation.

Load libraries at the top of your script fileAwesome!.Now you have access to functions from both the igraph and tidyverse packages.

Igraph is an open-source library that contains a collection of tools used for network analysis (read more at https://igraph.


The tidyverse is a collection of packages that is designed for data science.

You can read more by visiting it’s website at https://www.


org/ or by reading the docs once tidyverse is installed using:help("tidyverse")Model Parameters and Data CollectionOnce everything is loaded, we’re going to set up our model parameters.

Let’s create a 2-D forest with x amount trees.

We’ll set the simulation to run 100 times.

For this particular model, we want to investigate what effect tree density has on the propagation of wildfires in forests.

Therefore, we set percent_alive to a range of values between 10% and 90% forest density.

Finally, we set up a table to feed data from our simulations into using a tidyverse tibble.

Great!.Our parameters are set and a data table has been established.

Let’s start up our model.

Before each individual run, we want to reset network parameters.

Once those network parameters are set, we can generate our forest, populate it with trees, and start a fire.

Before the model is run, we want to establish and record the initial conditions within the forest.

Finally, we can run our model.

We use a while loop, as it is unclear when the fire will be sequestered.

If a for loop is used here, the the model can run longer than the fire has run, or the model can end before the fire has run out.

This looks like a-lot.

Fundamentally there are two events that are programmed below:a green tree that comes into contact with a red tree will turn reda tree that was previously red at (t-1) will turn browFinally, we end our run and our models.

We can look at our last simulation using:plot(f, vertex.

color = V(f)$col, layout = layout.


kawai, vertex.

shape = 'square', vertex.

label = '', vertex.

size = 4)From 1 simulation of N= 30, percent_alive = .

6 : STARTFrom 1 simulation of N= 30, percent_alive = .

6 : ENDCool right?Here’s a link to the .

csv data produced by this model.


. More details

Leave a Reply