Let's animate maps with R

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ก.ค. 2024
  • In this tutorial, you'll learn how to easily access one of several thousand World Development Indicators (WDI) from the World Bank database and create awesome animated maps on the global, continent, and country levels. Let's roll!
    0:00 Intro
    0:51 World Bank data
    2:33 Load packages
    3:13 Fetch World Bank data
    5:38 Modify column names
    5:57 Load world shapefile
    6:56 Plot world shapefile
    7:16 Remove Antarctica
    7:55 Join dataframe and shapefile
    8:53 Robinson projection
    9:42 Legend breaks
    11:10 Color palette
    11:36 Static map
    15:14 Plot static map
    15:40 Animate world map
    21:14 Plot animated world map
    21:56 Filter Asia
    23:13 Plot Asia map
    23:30 Animate Asia map
    24:50 Plot animated Asia map
    25:22 Filter India
    26:28 Animate India map
    28:11 Plot animated India map
    28:30 Closing
    Check the full code in my GitHub repo:
    github.com/milos-agathon/anim...
    Follow my work on Instagram:
    / milos_makes_maps
    Let's connect on Twitter:
    / milos_agathon
    If you like my work, consider buying me a coffee:
    www.buymeacoffee.com/milospop...
    Music credits go to:
    "Alex Productions - Fashion" is under a Creative Commons (CC-BY 3.0) license
    @alexproductionsnocopyright
    Music promoted by BreakingCopyright: bit.ly/bkc-fashion

ความคิดเห็น • 27

  • @oumardiallo7292
    @oumardiallo7292 ปีที่แล้ว +3

    Thanks for the tut. That was very neat. I love your map viz

    • @milos-makes-maps
      @milos-makes-maps  ปีที่แล้ว

      You're welcome! Thanks for the kind words and the encouragement to keep it going 😊🙏🏼

  • @lbarq
    @lbarq ปีที่แล้ว +3

    Thanks for this great tutorial.

    • @milos-makes-maps
      @milos-makes-maps  ปีที่แล้ว

      You're welcome! Happy that my new tutorial is useful

  • @AleksPopovic
    @AleksPopovic ปีที่แล้ว +1

    Cool animations tutorial Milos!

    • @milos-makes-maps
      @milos-makes-maps  ปีที่แล้ว +1

      Happy you like it 😁. It took some time to make it but it was totally worth it

  • @pavloshevchuk2454
    @pavloshevchuk2454 ปีที่แล้ว +1

    You did a great job, Milos! I wish that you would have more subscribers.

    • @milos-makes-maps
      @milos-makes-maps  ปีที่แล้ว

      Thank you dear Pavlo for your kind comment! It takes time to build up the channel but I really enjoy interactions with you guys while the channel is small and cozy 😀

  • @kunalkunal6227
    @kunalkunal6227 ปีที่แล้ว +3

    Would it be possible for you to create a tutorial on how to work with netCDF files, using any variable such as surface temperature from satellite data in a netCDF (.nc) file? I have noticed that many people in the atmospheric science community who work with satellite and model data tend to use Python for data analysis, and there is a wide range of Python-based tutorials available. However, it seems that tutorials on handling netCDF files using R are still lacking or not as advanced as those for Python. As a result, many in the atmospheric science community prefer using Python for plotting spatial maps. So I guess, if you can make a series on this topic that would be great.

    • @milos-makes-maps
      @milos-makes-maps  ปีที่แล้ว +2

      That's an excellent proposition! I've worked with .nc files in R before so making a tutorial on handling/making those files is on my agenda!

    • @Aaqib..
      @Aaqib.. ปีที่แล้ว +1

      Thats so true.

    • @Aaqib..
      @Aaqib.. ปีที่แล้ว +1

      ​@@milos-makes-maps thank you

  • @kevinmorgan4234
    @kevinmorgan4234 ปีที่แล้ว +1

    Thank You Milos that was a great tutorial. Question that I came away with after watching is whether this can be integrated into a R-Markdown file or do you have to save the GIF animation as a standalone?

    • @milos-makes-maps
      @milos-makes-maps  ปีที่แล้ว +1

      Hi Kevin, I bumped into two solutions on Stackoverflow that discuss exporting gganimate objects to html and pdf.
      stackoverflow.com/questions/53876485/how-to-render-a-gganimate-graph-in-html-using-rmarkdownrender-without-gener
      stackoverflow.com/questions/63042208/rmarkdown-rendering-to-a-pdf-that-uses-gganimate

    • @kevinmorgan4234
      @kevinmorgan4234 ปีที่แล้ว +1

      @@milos-makes-maps I will check it out. Thank You Milos.

  • @julianandressalazar5755
    @julianandressalazar5755 ปีที่แล้ว +1

    Hi Milos!
    I was trying to follow your tutorial, but when I run the next code I have this error:
    > animated_world

    • @milos-makes-maps
      @milos-makes-maps  ปีที่แล้ว +1

      Hi Julián! It's my pleasure to share knowledge with R community! Thanks for flagging the error. Some folks have already warned me about experiencing `Error: arguments have different crs` when they run the code. The issue is with gganimate 1.0.8. I have gganimate 1.0.7, which explains why I didn't get this error. Several possibilities to resolve this error are explained in the accepted answer of this post:
      stackoverflow.com/questions/75900914/problem-animating-with-geom-sf-and-gganimate-error-arguments-have-different-cr. However, if none of them work, please uninstall your current version and install gganimate 1.0.7.

  • @zabihullahazimy5046
    @zabihullahazimy5046 ปีที่แล้ว +1

    This part gives the following error:
    animated_world

    • @milos-makes-maps
      @milos-makes-maps  ปีที่แล้ว

      Hi! The error message "arguments have different crs" indicates that there are spatial objects or data frames with different coordinate reference systems (CRS) being used in your code. This can occur when you are trying to combine or plot spatial data that have different CRS settings.
      To address this issue, you can try the following steps:
      1) Check the CRS of each spatial object or data frame: Use the st_crs() function from the sf package to inspect and compare the CRS of each spatial object.
      2) Ensure CRS consistency: If you find that the CRS settings are different, you can use the st_transform() function from the sf package to transform the spatial objects to a consistent CRS.
      3) Recheck the data and attempt the operation again: After ensuring that all spatial objects have the same CRS, try running your code or operation again to see if the error is resolved.
      Btw, the warning messages you encountered regarding NAs introduced by coercion may be related to data type mismatches or issues in your data. You may need to examine and clean your data to ensure consistency and correctness.
      If the issue persists or if you need further assistance, please provide additional details about the specific data and operations you are working with so that I can help you more effectively.

    • @zabihullahazimy5046
      @zabihullahazimy5046 ปีที่แล้ว +1

      @@milos-makes-maps thank you so much for your reply.
      I first tried to run the exact same code, but it showed me the error.
      After solving this, I will modify this for COVID dataset
      Thanks again

    • @milos-makes-maps
      @milos-makes-maps  ปีที่แล้ว

      @@zabihullahazimy5046 I'm glad if it works now! Let me know how it goes

    • @zabihullahazimy5046
      @zabihullahazimy5046 ปีที่แล้ว +1

      I have tried the very same code, but it shows the same error.
      I'm running it on Mac M1 chip. Does it also have to do anything with that?

    • @milos-makes-maps
      @milos-makes-maps  ปีที่แล้ว

      ​@@zabihullahazimy5046Let's debug it! Could you open a GitHub repo, store your code there and send me an invitation to it?

  • @pavloshevchuk2454
    @pavloshevchuk2454 ปีที่แล้ว +1

    Dear Milos, I have reproduced your map using my data. It works well. However, I don't understand the legend's breaks in the legend. Your data are continuous. Look at the labels. Under the first key rectangle is 0. So, I assume this colour represents values between 0 and the next one, which is 11.5. The next colour (correct me if I'm wrong) represents values between 11.5 and 27.8 and so on. Using this logic, the penultimate colour represents values between 80.1 and 100. What represents the last coloured rectangle?

    • @milos-makes-maps
      @milos-makes-maps  ปีที่แล้ว

      Hi Pavlo, you're right the scale is continuous and the breaks are confusing because the legend keys and breaks don't always correspond to each other. That's why I usually go for discrete legend keys because then legend keys and labels correspond to each other. But in this case I as a bit lazy and went for an easier solution. Funny, many people ask me to use bins like this one instead of discrete keys. In this case, you can think of breaks as markers that are close enough to the nearby color. So, they don't represent from-to-value. The last colored rectangle always shows the maximum value (100 in this case) while the first shows the minimum (0 in this case)

    • @pavloshevchuk2454
      @pavloshevchuk2454 ปีที่แล้ว +1

      @@milos-makes-maps Thank you, Milos! My data on the map can be confusing in itself. Besides, this kind of visualisation is usually made for demonstrations to other people. There is no need to make it more difficult to understand. I'll try (I'm not very experienced) to rewrite your code for the case of discrete breaks. Thanks for the explanation!