Giter Club home page Giter Club logo

gcammaptools's Introduction

Build Status

gcammaptools

Overview

The gcammaptools package provides functions for plotting GCAM data on world or regional maps. This includes functions for making plots for regional or gridded data, as well as default projection and theme settings that provide a house style for GCAM plots.

Installation

This package must be installed from the github repository using install_github. To do this, you will need to install devtools first if you don't have it already.

install.packages('devtools') # if you don't have it already
devtools::install_github('JGCRI/gcammaptools')

Optionally, you can build the "examples" vignette, which shows how to do several common mapping tasks. To build these examples, replace the install_github command above with:

devtools::install_github('JGCRI/gcammaptools', dependencies=TRUE,
                         build_vignettes=TRUE)

You can display the vignette by running

vignette('examples','gcammaptools')

Sometimes R can't find the CRAN repository without help. If during the install_github steps you get errors about missing packages or functions, rerun them with the repos option:

cran <- 'http://cran.us.r-project.org'
devtools::install_github('JGCRI/gcammaptools', build_vignettes=TRUE,
                         dependencies=TRUE, repos=cran)

This should allow R to fetch the packages it needs to complete the installation from CRAN.

Usage

Preparing GCAM data

The recommended way to load your GCAM data is by using the rgcam package create a project data file from a GCAM database, and then querying that file for the data you want to plot.

prj <- rgcam::loadProject('myproj.dat')
co2 <- rgcam::getQuery(prj, 'CO2 emissions by region', 'Reference')

Alternatively, you can start with any data frame that has a region column and one or more data columns.

Once you have loaded the data, you must add the region identifiers used in the map data to the data frame using the add_region_ID function:

co2 <- add_region_ID(co2)

Plotting maps

The main plotting function is plot_GCAM. You can supply your own map of region boundaries, but most of the time you will want to use the ones supplied with the package. plot_GCAM:

plot_GCAM(map.rgn32, col='value', gcam_df=co2, title='CO2 Emissions', legend=TRUE)

gcammaptools's People

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

gcammaptools's Issues

API for map plotting needs refactoring

Right now the gcammaptools API takes a monolithic approach to specifying plots. For example, plot_GCAM sets the aesthetics, draws the graticule, draws the base map, colors the map if necessary, sets the color sale, applies the map projection, and sets theme elements. Bundling all that functionality into a single function makes it hard to reuse any of this functionality. For example, plot_GCAM_grid uses plot_GCAM to draw its base map, but this causes some problems with the legend title that have to be worked around, and fine-tuning the color scale is likely to be a major hassle.

What we need is a set of customized ggplot elements that we can chain together according to ggplot conventions, so we could write something like

plot <- gcam_basemap(...) + gcam_grid(...) + gcam_colorscale(...) + gcam_theme(...)

Each one of those could provide a set of sensible defaults to narrow down the range of options and provide a consistent house style for geographic plots, while still allowing us to remix as required.

Common utility for transforming data

It would be a useful utility to map differences between scenarios within the function rather than switching between wide and long dataframe formats.

Map data needs to be updated

The geojson file for GCAM-USA is missing entirely, so that needs to be updated.

The one for GCAM-China has a practical problem; it is painfully slow to render. I think this is due to excessively high resolution in the China subregion boundaries:

filter(map.chn, id<=32) %>% nrow / nrow(map.chn)
[1] 0.3350259

So, over 65% of the vertices in that data set are in the subregions (all of which have id values >= 33). I think we could do with a bit less resolution in those subregion boundaries, especially if it speeds up map rendering.

Along those lines, all of the map data could have its resolution reduced a bit. It's nice that we have all of the detail around the deltas of major rivers, all the islands of the Canadian arctic, and the fjords of Norway, but they don't really show up at the scales of our plots, and they slow the rendering dramatically.

Plotting functions don't work unless the namespace is attached

Example:

> gcammaptools::plot_GCAM_grid(co2grid, 'value')
Error in import_mapdata(mapdata) : object 'map.rgn32' not found

When you attach the namespace it works as expected.

> library(gcammaptools)
> gcammaptools::plot_GCAM_grid(co2grid, 'value')
although coordinates are longitude/latitude, st_intersection assumes that they are planar

However, you shouldn't have to attach the package namespace in order to get the package to work. Notably, as it stands the function can't be used from another package.

Update basin map

The basin235 map is blocky. Update to a more detailed version of the map.

add_region_ID not working for basin235

After changing the water basin variable name to 'region' to allow for use of add_region_ID the following code:

groundwater <- add_region_ID(groundwater, lookupfile = basin235)

results in a data frame that is all 0's except for 'region' and 'id'. These two variables are also reversed, id values in the region variable and vis versa, which is likely causing add_region_ID to work incorrectly.

Starting over and changing the water basin variable name to 'id' allows add_region_ID to work, but provides 'id' variables as water basin names rather than water basin numbers and 'region' as the water basin number as class character rather than numeric

add_region_ID munges other columns

Original:

                            region       total Units               scenario year
1                   Africa_Eastern  0.30006688    EJ GCAM-China-Ref-d633d8b 2020
2                  Africa_Northern  1.43266595    EJ GCAM-China-Ref-d633d8b 2020
3                  Africa_Southern  0.36476475    EJ GCAM-China-Ref-d633d8b 2020
4                   Africa_Western  0.43875343    EJ GCAM-China-Ref-d633d8b 2020
5                               AH  0.87788441    EJ GCAM-China-Ref-d633d8b 2020
6                        Argentina  0.71153013    EJ GCAM-China-Ref-d633d8b 2020
7                     Australia_NZ  1.19388696    EJ GCAM-China-Ref-d633d8b 2020
8                               BJ  0.22668671    EJ GCAM-China-Ref-d633d8b 2020
9                           Brazil  2.67441480    EJ GCAM-China-Ref-d633d8b 2020
10                          Canada  2.43183550    EJ GCAM-China-Ref-d633d8b 2020
...
18                   Europe_Non_EU  2.07783819    EJ GCAM-China-Ref-d633d8b 2020
19 European Free Trade Association  0.74806197    EJ GCAM-China-Ref-d633d8b 2020

After add_region_ID:

                            region      total Units               scenario year id
1                                          NA    EJ                   <NA>   NA  0
2                   Africa_Eastern  0.3000669    EJ GCAM-China-Ref-d633d8b 2020  2
3                  Africa_Northern  1.4326660    EJ GCAM-China-Ref-d633d8b 2020  3
4                  Africa_Southern  0.3647647    EJ GCAM-China-Ref-d633d8b 2020  4
5                   Africa_Western  0.4387534    EJ GCAM-China-Ref-d633d8b 2020  5
6                            Anhui  0.8778844    EJ GCAM-China-Ref-d633d8b 2020 44
7                        Argentina  0.7115301    EJ GCAM-China-Ref-d633d8b 2020 31
8                     Australia_NZ  1.1938870    EJ GCAM-China-Ref-d633d8b 2020  6
9                     Beijing  Shi  0.0000000    EJ                      0    0 33
10                          Brazil  2.6744148    EJ GCAM-China-Ref-d633d8b 2020  7
...
18                European_Eastern  0.0000000    EJ                      0    0 14
19                 European_Non_EU  0.0000000    EJ                      0    0 15

Travis-CI `warnings_are_errors` are set to false

This is NOT good, as warnings from R CMD CHECK are generally things you need to pay attention to and fix. But:

  • rgdal wouldn't install on Travis via apt-get approach
  • So I installed it via r_binary_packages
  • But the binary is perhaps an old version? Anyway, the vignette wouldn't build b/c of some undefined symbol error in rgeos or rgdal
  • So I added '--no-build-vignettes' to the R CMD CHECK and BUILD arguments
  • But this resulted in a warning of no matching vignette html/pdf file
  • So, I set warnings_are_errors: false

At some point, we'd like to revisit this--either figure out how to do a source rgdal installation; or include the vignette-build products in the repo I guess.

See commits in PR #3 for various .travis.yml changes I tried.

How to plot pies on base map?

How to plot pies for different fuel types based on GCAM outputs on the base map (like on mapUS49)?
like the picture below:
Screen Shot 2022-06-04 at 8 07 58 PM

Package install fails with staged install

Another one of these darn things.

ERROR: hard-coded installation path: please report to the package maintainer and use ‘--no-staged-install’

Honestly, I'm not sure where the problem is here. All of the places where we use a path from the package data appear to be within the guidelines established here:
https://developer.r-project.org/Blog/public/2019/02/14/staged-install/index.html

Probably we'll just convert all of the stuff currently read from the package library to package data objects and be done with it.

EPSG 3395 causes error

The command plot_GCAM(map.rgn32, proj_type = 'EPSG', proj = 3395) gives the error

Error in if (needClosed && any(vapply(x, NotClosed, TRUE))) stop("polygons not (all) closed") : 
  missing value where TRUE/FALSE needed

Needs more looking into.

Color scale is not properly centered when negative values are present

If you're making a difference plot, then you want 0 to be at the center of the diverging color scale, and you want the positive and negative sides to have the same scale. Here's an example where that doesn't happen:
image

The zero point of the color scale is all the way over in the deep blue, which is misleading (you can also see that Antarctica has been assigned a value of 0, a described in #10 ).

Unplotted user inputted data gets removed

When calling plot_GCAM() where the gcam_df parameter contains more columns than the specified col column for plotting, the rest of the columns get filtered out. This makes sense internally, but the user may want to keep that data, for example with ggplot2::facet_wrap().

For example, calls like plot_GCAM(map.basin235, col='value', gcam_df=df, gcam_key='id') + ggplot2::facet_wrap(~scenario) fail, when that is a reasonable thing to do.

Clean up test suite

Clean up existing test suite. Investigate the shape processing aspect of the choropleth function in maps.R.

Update README

Specifically, remove use of depreciated functions.

Documentation links not working

A number of documentation links don't work, i.e. devtools::check() is reporting errors on them. I'm about to open a PR that changes them (for now) to just \code{}, i.e. removes the links, because I want to make sure the package passes R CMD CHECK for #1 . They include

  • \code{\link{mapproj::coord_map}} in diag_header:43
  • \code{\link{map.usa}} in gcammaptools:36
  • \code{\link{ggalt::coord_proj}} and in Map_Functions:49
  • \code{\link{mapproj::coord_map}} in Map_Functions:440

Need documentation for gridded plot capabilities

The latest version adds new capabilities for plotting gridded data, but neither the quick start guide on the front page, nor the examples vignette mention it. I can put something in the quick start shortly, but to get it working in the vignette I'll need to scare up an example data set.

Fix package vignette

The examples vignette needs to be updated to work with the new changes on the alter-proj4-method branch.

gcam32_colors is not exported

I see gcam14_colors is exported, but not 32 colors. Seems like it should be, but I'm just starting to look at the package, so might be my error...

Geojson shapefile for 32-region GCAM has distorted Antarctica

I noticed that the Antarctica polygon for the 32-region GCAM shapefile is distorted at the bottom. This occurs in the Robinson, Eckert-III and Winkel-Tripel projections. I think it can be fixed by replacing the Antarctica polygon in the shapefile. I can work on this.
gcam32shapefile

addRegionID not handling null region correctly

addRegionID assigns the null region a value of 0 in all of its columns (including the Units column). This causes Antarctica to be plotted as if it has data, instead of being drawn in neutral grey.

Error on Choropleth

Error:
[1] "There was an error at - `General` - building your map:" <simpleError in process_data(map_data, data_key_field, data_col, data_key_field, shape_obj, shape_data_field, shape_key_field): unused argument (shape_key_field)>

Executed Code:

shp_file <- '/Users/d3y010/projects/gcammaptools/world_shape_file/TM_WORLD_BORDERS_SIMPL-0.3.shp'
csv_file <- '/Users/d3y010/projects/gcammaptools/data.csv'
test <- gcammaptools::choropleth(shape_data = shp_file,
                                 shape_label_field = NULL,
                                 map_data = csv_file,
                                 data_key_field = "Country",
                                 shape_key_field = "NAME",
                                 output_file = "/Users/d3y010/projects/gcammaptools/test_output.png",
                                 data_col = "X2015",
                                 map_legend_title = "Population",
                                 map_title = "2005 Population",
                                 simplify = TRUE,
                                 bin_method = "pretty",
                                 bins = 8,
                                 map_palette_reverse = FALSE,
                                 map_palette_type = "seq")

Let me know if you need version specifics.

Fix warnings in package check

Right now the package check stage throws several warnings on the alter-proj4-method branch. I've disabled warnings_are_errors on the CI for that branch, but ultimately the package needs to pass checks with no warnings.

Feature for coloring maps with no regional data

Maybe we should let the map FILL_COLOR be an optional parameter of plot_GCAM. It shouldn't matter too much when plotting just to show region boundaries, but may affect the look of gridded data.

Raster data extends past map bounds

The plot_GCAM_grid function crops raster data to the bounds of the map, but it is not exact. Cells that are mostly within bounds are still included which causes the edge of the plot to be pushed past the edge of the map.

Create choropleth map functionality

Create choropleth map functionality that ingests a polygon vector dataset (e.g., shapefile, sf object, etc.) that contains a field to bin. Optionally, ingest a complementary tabular (e.g., CSV file, data.frame, etc.) dataset to supply the values as linked to the polygons by some common key.

ggalt development version does not support some projections

The current development version of ggalt did not support eck3, robinson and witntri projections resulting in the following error when using plot_GCAM:

Error in zero_range(from) : x must be length 1 or 2

The issue was fixed by installing and older commit (12/30/16):
devtools::install_github('hrbrmstr/ggalt',ref="017b4824f3")

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.