R for the Rest of Us

In this episode, I chat with Terence Teo, Professor at Seton Hall University and expert in creating stunning 3D maps using the {rayshader} package in R. Terence discusses his journey into data visualization, specifically his use of R and the RayShader package to create mesmerizing 3D maps. Terence shares insights from his academic background in political science, his creative process for making maps, and how he balances artistic flair with technical rigor. The discussion dives into geospatial data, the intricacies of the {rayshader} and {rayrender} packages, and the value of experimentation in visual storytelling.

Important resources mentioned:
Connect with Terence on:  
Subscribe to our newsletter: https://rfortherestofus.com/newsletter

What is R for the Rest of Us?

You may think of R as a tool for complex statistical analysis, but it's much more than that. From data visualization to efficient reporting, to improving your workflow, R can do it all. On this podcast, I talk with people about how they use R in unique and creative ways.

David Keyes:

Hi. I'm David Keyes, and I run R for the Rest of Us. You may think of R as a tool for complex statistical analysis, but it's much more than that. From data visualization to efficient reporting to improving your workflow, R can do it all. On this podcast, I talk with people about how they use R in unique and creative ways.

David Keyes:

I'm delighted to be joined today by Terrence Teo. Terence is a professor at Seton Hall University in the department of political science. But if you are around the art community, you are more likely to know Terence for the incredible maps that he makes using the various ray shader packages, which we are going to discuss today. So, Terrence, welcome, and thanks for joining me.

Terence Teo:

Thank you for having me, David.

David Keyes:

Yeah. So tell me first, if you don't mind, just a bit about your academic background, and then we'll build on that because I'm curious to hear to what degree that relates to your interest in making maps.

Terence Teo:

So, yeah, I'm trained as a political scientist, and I do work primarily on democracies and dictatorships. So I study, in particular, authoritarian regimes, why some of them are stable, and more recently, why democracies may backslide into dictatorships. So that's So

David Keyes:

that's a very relevant topic Yes. For those of us in The US. Correct. So

Terence Teo:

it's good. Yeah. I'm gonna be very busy, in the in the next few years. So that's my day job. So I I that's my research.

Terence Teo:

When I work, I do work along those lines. And and when I I teach along those lines as well on on democracy and dictatorship, but also mainly research methods. So I I teach methods and statistics to social science undergrads as well as some game theory at times to to students.

David Keyes:

Yeah. Cool. So like I said before, the reason I know you, and I think the reason many people in the art community know you, is for the incredible maps that you make. But I'm curious if you could talk, first of all, how you got into making maps in the first place. Did it come from your academic work, or was it just a general interest in maps?

Terence Teo:

Yes. This was I had never considered, making maps as an academic. Back when I was in grad school and when I finished grad school, I was more focused on on running statistics, run through statistical models, and then figuring out how to present them the estimates, the quantities of interest in an accessible and accurate way. Right? So most of my time was spent, how do I visualize descriptive data?

Terence Teo:

And then how do I then visualize the the statistics, the estimates that that I get? So I stumbled upon maps when I first became a dad. I think I I was sleep deprived. And my wife and I, we were taking turns with working his shifts. And so sometimes late at night, like, two, 3AM.

Terence Teo:

After a while, I I I was into I found out the database society had just started up. So this was, I think, five years ago. And I I joined the Slack. I heard people talking about it. And so I joined the conversation there.

Terence Teo:

I met a lot of people doing incredible work. And then people also on Twitter. Now I had a Twitter account back in 2012 that that I started, and there there was a way to procrastinate from writing a dissertation. But I never used it. But through the Slack and I saw people sharing stuff on on Twitter, I thought, why not go see what's what's happening there?

Terence Teo:

I stumbled upon the work of Tyler Morgan Wall, the developer of Ray Shader and the Ravers. The first thing I saw I remember was he had crafted a sword made of cubes in three d in arm and it was spinning. And I thought that was incredibly cool and I never imagined that you could do that with r. Right? Because all I knew about r was it's a fantastic language for programming, for statistical analysis, for doing reproducible work, but not three d rendering.

Terence Teo:

Right? Much less maps. And then the rest as people say, right, it is history. So I started following his work and then trying it out, watching development of the packages he was writing. And then when Ray Sheeter came out with maps, I thought this was just amazing.

Terence Teo:

And so I tried it out. I loved it. I just fell in love with it. And that that's how it's how I started and how I got into maps. So, really, the connection was DataViz and then just getting really excited by the things we could do with art.

Terence Teo:

Right? Something that I never thought possible. But Tyler, he threw his wizardry or sorcery. He put packages out.

David Keyes:

Yeah. And so at this point, the maps you make, do you use them at all in your academic research, or is it really just two separate interests?

Terence Teo:

Yeah. Right now, honestly, they they are separate. So it is a hobby of mine to create maps. Although I I I have recently started to see that that there are connections that could be made. So for instance, I think lots of the work that I do with dictatorships, some dictators, what they do is they only direct resources to people who support them.

Terence Teo:

And so there's a spatial component to that. Right? So if you look at you pick a country, you can see perhaps some cut some areas of the country where the support of dictator would get lots of they might get pipes. They might get roads built, where those that don't get it. Right?

Terence Teo:

So I'm starting to see that there are connections that that we could make. And in political science, I think geospatial data, is starting to come up, hasn't quite become popular, if you will. Right? So there's a niche of researchers doing great work that brings in the geospatial component. But most of the time, I think a few of us are looking at economic data, political data, survey data, experiments.

Terence Teo:

I've been toying with the idea of really bringing in a geospatial component since I've been working on maps for a while now.

David Keyes:

Okay. Interesting. So let's talk a bit about the raverse. So the raverse the the verse part of it is similar to the idea of the tidyverse. It's a collection of packages.

David Keyes:

So can you talk about the packages that you typically use and what they do? Because the maps you make are very unique. So, yeah, talk through the some of the packages that you use within the ravers.

Terence Teo:

Right. So within the ravers, the two so two most common packages that that that I use are ray shader, right, which is a three d hill shading package that Tyler developed. And then for the final product, it's also the patch called ray render. And so this one is where the magic happens, where you take the the mesh object from ray shader and then put it through a path tracing set of algorithms that gives it that final Blender y look, if you like, right, where you simulate realistically how light would travel in the scene and how it would reflect and bounce off surfaces. And so that gives us the final product.

Terence Teo:

In addition to the ravers, I think, because it's geospatial data mostly, the simple features package, right, the SF package that allows our users to work with all sorts of geospatial data from lines, polygons, points, and the like. That's something that's almost always, right, in in my workflow. And then there's also elevator. Right? The elevator package by Jeff Hollister.

Terence Teo:

I think he works at the EPA. A fantastic package that allows us to just get rest of data from any point on the globe. Right? That and just one line of code, you give it the shape file or the boundary of the country or the region, and then it pulls in elevation data from any resolution that you want. So that's also one that I always have in my workflow.

Terence Teo:

Yeah.

David Keyes:

Yeah. Because the maps that you make so so I do a lot of mapping, but it's almost always just using vector data or yeah. It's it's using simple features data, not

Terence Teo:

any kind

David Keyes:

of raster data, and definitely not the three d component that I think really makes your maps unique. And I think that that that really is the thing that that is unique about using the kind of Rayverse is the being able to plot in what looks like three d. I, honestly, I never would have thought about that as a way to make maps, but when you look at it, it gives it a really distinctive visual look and feel. Yeah. And it's just it's really amazing.

David Keyes:

So I've talked a bit about why I like the Rayverse, but you've described yourself as the Rayverse's number one fan. What is it about it, the Rayverse, that you really so much?

Terence Teo:

I think what I really liked was the ease of creating sort of high quality three d visualizations. Right? So that that was what first got me into the Rayverse. What struck me about the the final product that that one can create. And then just the documentation, at least in my opinion, for the what Tyler has put out with respect to all the packages with ray shader, ray render, and his other packages as well.

Terence Teo:

It's just very comprehensive and accessible. So much of what what I know is from just reading the docs. And I I think for us, we we know good documentation where where we see it when we see them. And so I had this huge appreciation for everything being very nicely laid out. All the arguments are there, then with examples.

Terence Teo:

So that was another reason why that. And then as well, I think being in R, it allows us to it gives us a canvas, to explore and to experiment. So that's another reason why I I really liked it that I could play with it and play with it in real time. So it's not like you have to wait for it to render. You can move it around.

Terence Teo:

It opens up a window. You can spin it around with your mouse. You can try different things. And on top and above all, it it works, I think I'm not sure if this is it's probably something that Tyler intended. It works on the principles of the grammar of graphics.

Terence Teo:

It's like Uh-huh. Ggplot, but with a three d

David Keyes:

Sure.

Terence Teo:

Capability. Right? So you can add layers. And so what what I've learned in g g plot or practiced in g g plot, I could bring it right to ray shader. And then when I think about these visualizations, I think in terms of adding layers on top of the blank raster canvas layer, if you will.

Terence Teo:

So that that's why I'm so enamored with with the package. And there's just so much data out there that we can I think we can overlay on maps and just play around? So that's why I like it so much.

David Keyes:

Yeah. It's interesting because I think, you know, some people when they think of three d data visualization, the classic is right, like the, like, silly Microsoft Excel, like, three d pie charts or whatever. I I think because three d has been done so poorly in the past, there's been a stigma against three d as just a terrible gimmicky way to show data. But I'm curious for you because you've you know, you make these maps in three d. Do you think there's value in three d that people might overlook?

Terence Teo:

Yeah. The definitely. And I I I totally hear you about what what some might call gratuitous three d and and and Microsoft and things things in the nineties. It doesn't help things. But I I think these days with the increase in computational power, the computing power that we have, and I think judicious use of three d.

Terence Teo:

So I think in general, we we see the world in three d. Right? And it it has this sort of connection with what we actually observe and sometimes it makes things more impactful. Now, of course, not everything needs to be three d. In fact, maybe the majority of visualizations need not be in three d.

David Keyes:

Sure.

Terence Teo:

Right? But if we want that added engagement from people and so that's why I've seen from some of the work that that I put out, it makes people look closer. Right? I think grabbing the attention, hopefully, in a positive way, allows people to explore further and it gives people more more to look at. And that that's despite the the issues that are, to be fair, are inherent in Swedish, which is the issue of perspective.

Terence Teo:

Sure. So, things that look further away, might look somewhat different. So there's some distortion if you're unable to manipulate the the image on screen. But nonetheless, I think there is value. I I don't believe really in dismissing a type of database outright, I think.

David Keyes:

Right.

Terence Teo:

And and really just being careful about when you go three d. And for my part, it's really a lot of experimentation. Yeah. So does it look good in three d? No.

Terence Teo:

Not. Okay. Then let let's not do that. Yeah.

David Keyes:

It's interesting because the thing you said about three d getting people's attention, I think is something that people who just focus on what's the right type of visualization ignore. Honestly, I wouldn't be talking to you today if your maps weren't so visually stunning and didn't grab my attention. And in a world where people are scrolling, people's attention spans are limited, being able to produce something that makes people stop for a second and pay attention, I think, is hugely valuable even for dry academic research. I know that's not necessarily what you're using it for, but getting people to pay attention can be a a huge value that comes from three d.

Terence Teo:

And sometimes you say, here's a map. You wanna put some stuff on it. We don't really need shaded release. Right? You don't need to know, the elevation of the country.

Terence Teo:

But it just I think depend again, depending on on the specific case, it does add something. It makes it look makes people look at it again, and maybe makes them pay attention, right, beyond just, here's another flat, two d image. I see the country boundary.

David Keyes:

Right.

Terence Teo:

Here's some stuff on it that I should pay attention to. But if you have some relief in the background, maybe elevation actually might have something to say about why the points or lines are low certain ways. For example, the work that I did with population density maps, I think that that sort of took off in a way that I did not expect. Uh-huh. It was less about one could say we just have a orthographic top down view Yeah.

Terence Teo:

And then use color or hue to show where where people are. Yeah. Yeah. And that's fine. But if you add elevation, then you will see that people don't live where there are mountains.

Terence Teo:

Right. And so you have where people live and where people don't live because of their physical geography. And I think that's interesting. I thought that was cool when I when I when I first did it. Yeah.

Terence Teo:

Yeah.

David Keyes:

Well, it's interesting. I I mean, I'm thinking so I'm I'm in Oregon, and I don't know how much you know about the topography of Oregon, but there are mountains that run right down the middle of the state. And I make a lot of maps in Oregon, but I've never done anything to show the mountains, which as you were talking, I was thinking, like, yeah. When I do things like show, I don't know, median income or whatever outcome, it doesn't make sense to, in some ways, to show it without that information there because or population density is probably a better example. Right?

David Keyes:

Because, obviously, it's not just that those are, like, unpopulated areas because people choose not to live there. They choose not to live there because they're in the middle of the mountains. It snows a lot in the winter, whatever. I think that's a really good point.

Terence Teo:

Yeah.

David Keyes:

Cool. I was hoping that you could walk through an example of making, a map. Hey. David here. Just wanted to let you know that at this point in the conversation, we switched to a screencast.

David Keyes:

Now obviously, showing code doesn't work very well in an audio podcast. So if you wanna see the rest of this conversation, check out the video version of this podcast on YouTube. You can find a link to that in the show notes. I know that making these three d maps can take a while. Right?

David Keyes:

Like, how just out of curiosity, like, what when you make a map and it it's not even the coding part that takes a while. Right? It's the leaving it to render part. Like, how long does that kinda thing typically take?

Terence Teo:

Yeah. But you're right. So you're right. The the coding part actually is reasonably quick. Right?

Terence Teo:

So, again, large part is the cleaning. Right? The munging the data, the wrangling, getting any shape that we want. And once you get the code once you get that cleaned out, then it it's pretty straightforward given all the tools that Tyler has built into to the packages. The rendering part is where, yes, that's where most of the time is spent.

Terence Teo:

Right? Although, I would say that there's been a recent update, and Tyler has been very good with updates to the Rayverse packages, it has sped up. But usually, depending on the size so if I'm running the entire world, that can take up to a couple of hours. Oh, wow. So I leave it to run.

Terence Teo:

It runs solely on the CPU. So it's not accelerated by the video card. But if it's just a small area, it can take anything from a few minutes, to maybe ten minutes. Yeah.

David Keyes:

Okay. Interesting. Is there anything else you wanna add about reshaping, raver stuff before before I do the wrap up?

Terence Teo:

I I would say that really for people interested in this, that there are lots of people now who are doing terrific work and sharing tutorials. I mean, Tyler puts up tutorials on on his website. That that's really cool thing. So that this way I I saw I got my start. And then just looking at the documentation, which I think is really great, and then just trying things out.

Terence Teo:

But that said, there are also other other people who've been sharing tutorial. I know Milos has been doing a great series of of work on that. And there are many people I think now who are playing around with this. And it's really, I think it's just fun to play. I see this as play.

Terence Teo:

And then whatever comes out, it's a bonus, right? That that, oh, something nice has come up. And you don't have to be an expert, I think.

David Keyes:

Yeah. In

Terence Teo:

these things. Right? You just I think this is true for most of r. Right? Where Yeah.

Terence Teo:

You have a project. You wanna do something. Let's see if you can do it in r, and then see where it goes with that. You learn a lot more.

David Keyes:

Before, this isn't this didn't come out of your academic interest. This came out of your own personal interest and wanting to make some interesting maps. So Yeah. This has been great, Terrence. Thank you again.

David Keyes:

If people wanna learn more about your work, see more examples of your work, what's the best place to go?

Terence Teo:

So I I'm on do sky at t terrence, and then I'm on Twitter as well and the research remora. And so that's usually where the two two main places where I share the the work that I do. Yep.

David Keyes:

Great. We will post links to both of those in the description. So, Terrence, thanks again for joining me. I really appreciate it.

Terence Teo:

Yeah. Thank thank you so much, David. This was great.

David Keyes:

That's it for today's episode. I hope you learned something new about how you can use R. Do you know anyone else who might be interested in this episode? Please share it with them. If you're interested in learning R, check out R for the rest of us.

David Keyes:

We've got courses to help you no matter whether you're just starting out with R or you've got years of experience. Do you work for an organization that needs help communicating effectively with data? Check out our consulting services @rfortherestofus.comslashconsulting. We work with clients to make high quality data visualization, beautiful reports made entirely with R, interactive maps, and much, much more. And before we go, one last request.

David Keyes:

Do you know anyone who's using R in a unique and creative way? We're always looking for new guests for the R for the Rest of Us podcast. If you know someone who would be a good guest, please email me at david@rfortherestofus.com. Thanks for listening, and we'll see you next time.