Introduction to GIS, Part VI Plotting Maps with ggplot2

[SQUEAKING] [RUSTLING] [CLICKING] HELENA VAILLICROSA: During this course, when we've had the need to plot a map, we use the code that is provided by default by R. But there are other ways to plot maps and make them easy to be tuned according on our taste. So now, I'm going to show you how to display nice maps that are easy to tune using ggplot. And to do that, I'm going to charge the map that we've been using, our friend, this temperature map. Also, to make the process easier, faster for R, I'm going to aggregate this map, so make it coarser. In this case, I'm going to use a factor of 5. So it means that from each five pixels I'm just creating one. It's going to affect the result. In this case, it's going to make the image to be less defined. But this is just because we aggregated the data. And we also need to transform this map into a data frame. This is a requirement to be able to plot it with ggplot. So that's what I'm doing here, as data frame. By default, this command is not included. So this means that when transforming the map into a data frame, we are maintaining latitude and longitude as a variable. By default, this is false. So it wouldn't be-- well, I'm just going to show you how it is by default. See, we only have one variable. But in this case, if we would give this to ggplot, that wouldn't be able to provide us a map because x and y would be lacking. So we need to include this chunk of code here that's going to give us the latitude and longitude. So now, we have three variables instead of one. And if we click on here, we have the x, the y. And that's the name of the map that we've used. Just because it's very long and not easy to work with, I decided to change that name into temp, temperature. So that's what I'm doing here. And I'm going to charge the packages we're going to need to do this process, which is the map tools that is related to working with maps. Then we have the ggplot2, which is the main package we're going to be using here. And then we have this BDDs that is about the color palette we're going to use to create the map. And I'm going to also charge this map that we've been using, which is the one that separates the water bodies. So it's the shape of the ground. And this would be the most simple code that would give us a map using ggplot. So this part here would not be necessary. We could erase it. But in this case, I am storing this code into this object. So I am saying to R that I'm going to be using ggplot. And our base, it's going to be this data frame. I'm going to add a plus. I'm going to say to ggplot, what do I want me-- what do I want ggplot to provide. In this case, I want to have a gmraster. So I want ggplot to give me a raster based on this as an x, this as a y. So we could type it here and it would work equal weight. Just if x or y would have different names, we would have to specify these names right here. And then in fill, I want ggplot to fill this x and y with the temperature, which is the third column that we have in that data frame. So let's just run this piece of code and see how it looks like. OK. This is clearly not the most beautiful plot you could have ever seen. But this is just the basic. And we're going to keep improving this map line by line. So you can see how each piece of code changes the result of the map we're going to have. First of all, what I want to include here is the shape of the Earth. So I want to include that map that we just uploaded on top of this one to make a good differentiation between what's the sea and what's not. So that's why I included this piece of code here. I'm going to erase this plus to make the code to stop right here and see how that looks like. OK. Now, we have this nice black line around. But now, it turns out that I don't want this black shape here. And I want to change the color palette. So to do that, I'm going to include this piece of code as well, right here. And I'm saying that the NA values are going to be NA. So I'm asking this code not to show me the NA's. So that's going to erase this gray area here. Also, I'm going to change the direction of the color palette, meaning that now the black color is related to lower temperatures. And I want it the other way around. And also, I am changing the color palette to have this shape. So if we go to the Help with Fn and F1, here we see that this is the beginning of the Help section. If we scroll down, we get to the different options we have for color palette. These are the different options we have. So if instead of this letter here, I put an A, B, or C. We would have different color palettes as an outcome. I chose this one just because. This is how our map has changed. Oh, I have to run it first, of course. And I have to put a plus because otherwise it wouldn't read this new line that I added. OK. I have changed the direction of the color palette, as you can see. Now, this gray area around has disappeared. And the color palette also has changed. I still want to keep improving this map because I don't like this background here. That's something called the theme of the map. You can change the theme. And in this case, I want the theme minimal. And I also don't like this chunk to be displayed. So I decided to reduce the y limits of the y-axis. And instead of being their total extent, I'm just going to cut it at minus 60. So it's going to be cut somewhere like here. To do so, I'm just going to include the plus this out, make it nicer and close. But just run it and see how these affect our map. OK. Now, we changed that gray area we had in the background as well. And we are no longer displaying the southern latitudes. Now, I want this to have a title. And I want this to have also not display this or that. And I want to change the legend position. Instead of being on the right, I want it to be on the left. So all these changes are the ones that are implemented here. This is not displaying x and y labs here. This is going to be to change the position of the legend. This is the title. And this is going to change this here. Instead of temp, I want it to explain that it's the January temperature. So I'm going to include them all together. And this is an example of how we can create and tune outcomes in a nice way. So we can use this outcome as a graphic representation in our scientific projects. OK. This looks kind of nice and tidy here. But what if we want to extract this image and store it in our computer? A nice way to do it is using TIF extension. What I have to do is just to write TIF, write the file name, where I want to create or generate this outcome, how big I want this image to be. The resolution by default is 100. I just put 200 here to make everything appear bigger. And then you have to include here what's the object you want to store in that object you just created. And then click on this part, which is giving the order to E to close that file and give it as an outcome, so you can consult in your computer and use it at your convenience.

Comments

Popular posts from this blog

12. Ion Implantation and Annealing - Analytic Models and Monte Carlo

17. Thin Film Deposition and Epitaxy - Introduction to CVD, Si Epitaxial Growth

Strategic Communication