# Help me with Julia/R comparison project, how to plot US county maps in Plots/StatsPlots (or what's best alternative)?

**URL:** <https://discourse.julialang.org/t/help-me-with-julia-r-comparison-project-how-to-plot-us-county-maps-in-plots-statsplots-or-whats-best-alternative/75686>\
**Category:** Visualization\
**Tags:** plotting\
**Created:** [February 2, 2022, 9:30pm UTC](https://discourse.julialang.org/t/help-me-with-julia-r-comparison-project-how-to-plot-us-county-maps-in-plots-statsplots-or-whats-best-alternative/75686 "2022-02-02T21:30:22Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [February 2, 2022, 9:30pm UTC](https://discourse.julialang.org/t/help-me-with-julia-r-comparison-project-how-to-plot-us-county-maps-in-plots-statsplots-or-whats-best-alternative/75686/1 "2022-02-02T21:30:22Z")

</div>

I’m working with Phil Price over at Andrew Gelman’s blog [https://statmodeling.stat.columbia.edu/](https://statmodeling.stat.columbia.edu/) to do a comparison between using R and using Julia for basic data analysis stuff.

We’ve got a few simple tasks and are implementing them in both languages to show comparisons. In fact, it started because I expressed frustration with the Tidyverse and its reliance on Fexprs but it’s probably turning out to show essentially that for simple things both ecosystems let you do the simple stuff relatively easily and without fuss… which is fine. I’m OK with just “Julia is very usable if you’re familiar with R”. There is one example simulation based task where it’ll highlight the speed differences, and the existence of good general purpose computing tools such as data structures (the Julia implementation will use a “Set” whereas probably R would be done with vectors and a lot of copying)

However, R has a lot more time to develop specialized tools for visualizations. One task is to visualize some COVID data from the CDC. In R Phil is plotting a map of the US with each county colored. I don’t know how to do that in StatsPlots. Is there a simple way? Yes I know that Julia has other plotting tools, but I don’t want to complexify the comparison by highlighting the fact that there are like 5 viable plotting libraries (Plots/StatsPlots, VegaLite, Makie, Gadfly, PGFPlots, etc etc)

So, how can I plot the counties of the US on a map using StatsPlots/Plots ? If this is not directly possible, what is the primary 1 or 2 tools that would be recommended to plot very simple map plots of the US such as this one that Phil generated (showing that the dataset we were looking at had mostly missing data, grey, and we needed to find a better data source)

 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/8/b8c87dc9fb0cbf3b383ce16f98bcb6a967e2335c.jpeg)

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [February 2, 2022, 9:38pm UTC](https://discourse.julialang.org/t/help-me-with-julia-r-comparison-project-how-to-plot-us-county-maps-in-plots-statsplots-or-whats-best-alternative/75686/2 "2022-02-02T21:38:34Z")

</div>

> [@dlakelan](#):
>
> Yes I know that Julia has other plotting tools, but I don’t want to complexify the comparison by highlighting the fact that there are like 5 viable plotting libraries (Plots/StatsPlots, VegaLite, Makie, Gadfly, PGFPlots, etc etc)

Would you accept a solution in any of these packages? I assume you wouldn’t have to present _all_ of them.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [February 2, 2022, 9:39pm UTC](https://discourse.julialang.org/t/help-me-with-julia-r-comparison-project-how-to-plot-us-county-maps-in-plots-statsplots-or-whats-best-alternative/75686/3 "2022-02-02T21:39:28Z")

</div>

I guess for the purposes of this thread I’ll accept any and all solutions, and then will try to choose the one that seems to provide the least resistance to present it 😉

---

<div class="post-metadata">

**Author:** ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)\
**Post date:** [February 2, 2022, 10:04pm UTC](https://discourse.julialang.org/t/help-me-with-julia-r-comparison-project-how-to-plot-us-county-maps-in-plots-statsplots-or-whats-best-alternative/75686/4 "2022-02-02T22:04:01Z")

</div>

Maybe this is helpful

> [@U.S. Counties Choropleth](https://discourse.julialang.org/t/u-s-counties-choropleth/20845/27):
>
> I know it’s been a while since this question was posted, but it took me a while to plot a choropleth from a DataFrame so I’ll post the way I do it by using Shapefile.jl and Plots.jl I hope this could help someone out there. The resulting map without working so much on the aesthetics is the following: using Plots using Shapefile using DataFrames using Colors #building a custom categorical colorbar colorbar = cgrad(["#FFFFFF", "#ffffcc", "#a1dab4", "#41b6c4", "#253494"], categorical =…

[https://www.queryverse.org/VegaLite.jl/stable/examples/examples\_maps/](https://www.queryverse.org/VegaLite.jl/stable/examples/examples_maps/)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 3, 2022, 12:07am UTC](https://discourse.julialang.org/t/help-me-with-julia-r-comparison-project-how-to-plot-us-county-maps-in-plots-statsplots-or-whats-best-alternative/75686/5 "2022-02-03T00:07:10Z")

</div>

Fyi, a GMT solution for this problem is [provided here](https://www.generic-mapping-tools.org/GMT.jl/dev/gallery/choropleths/choropleth_cv19/).

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [February 3, 2022, 4:08am UTC](https://discourse.julialang.org/t/help-me-with-julia-r-comparison-project-how-to-plot-us-county-maps-in-plots-statsplots-or-whats-best-alternative/75686/6 "2022-02-03T04:08:37Z")

</div>

Nice the GMT version seems simple enough and uses a “plot” method which seems straightforward as an interface. the vegalite interface is fine for someone who’s familiar with vegalite and Julia and etc, but will I think be off-putting as an introduction to mapping on Julia.

One question: where do I get the shapefile for the US counties?

I guess to maybe answer my own question: would this be what I’m looking for? [https://www2.census.gov/geo/tiger/GENZ2018/shp/cb\_2018\_us\_county\_5m.zip](https://www2.census.gov/geo/tiger/GENZ2018/shp/cb_2018_us_county_5m.zip) from [Cartographic Boundary Files - Shapefile](https://www.census.gov/geographies/mapping-files/time-series/geo/carto-boundary-file.html)

That zip file contains:

```julia
Archive: cb_2018_us_county_5m.zip
  inflating: cb_2018_us_county_5m.shp.ea.iso.xml  
  inflating: cb_2018_us_county_5m.shp.iso.xml  
  inflating: cb_2018_us_county_5m.shp  
  inflating: cb_2018_us_county_5m.shx  
  inflating: cb_2018_us_county_5m.dbf  
  inflating: cb_2018_us_county_5m.prj  
 extracting: cb_2018_us_county_5m.cpg  

```

which since I’m not familiar with shapefiles leaves me with question how to actually use it?

Installing GMT.jl results in:

```julia
Failed to precompile GMT [5752ebe1-31b9-557e-87aa-f909b540aa54] to /var/local/dlakelan/dotjulia/compiled/v1.7/GMT/jl_6mcLAj.
ERROR: Caught signal number 11 (Segmentation fault) at
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x25)[0x7f0af0c2c945]
[0x557263ef6588]
Stack backtrace:
/lib/x86_64-linux-gnu/libgmt.so.6(sig_handler_unix+0xf4)[0x7f0af0e9b344]
/lib/x86_64-linux-gnu/libc.so.6(+0x3c910)[0x7f0af0bde910]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x25)[0x7f0af0c2c945]
/lib/x86_64-linux-gnu/libproj.so.22(_ZN5osgeo4proj6common13UnitOfMeasureD1Ev+0x6f)[0x7f0aec85197f]
/lib/x86_64-linux-gnu/libc.so.6(__cxa_finalize+0xc6)[0x7f0af0be1556]
/lib/x86_64-linux-gnu/libproj.so.19(+0xb8083)[0x7f0ae8c87083]
    Building Conda → `/var/local/dlakelan/dotjulia/scratchspaces/44cfe95a-1eb2-52ea-b672-e2afdf69b78f/6cdc8832ba11c7695f494c9d9a1c31e90959ce0f/build.log`
    Building GMT ──→ `/var/local/dlakelan/dotjulia/scratchspaces/44cfe95a-1eb2-52ea-b672-e2afdf69b78f/d1068159f18828ec1831efd34f1880f84a792b98/build.log`
ERROR: Caught signal number 11 (Segmentation fault) at
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x25)[0x7f5df1963945]
[0x555d3716e552]
Stack backtrace:
/lib/x86_64-linux-gnu/libgmt.so.6(sig_handler_unix+0xf4)[0x7f5df1bd2344]
/lib/x86_64-linux-gnu/libc.so.6(+0x3c910)[0x7f5df1915910]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x25)[0x7f5df1963945]
/lib/x86_64-linux-gnu/libproj.so.22(_ZN5osgeo4proj6common13UnitOfMeasureD1Ev+0x6f)[0x7f5ded58897f]
/lib/x86_64-linux-gnu/libc.so.6(__cxa_finalize+0xc6)[0x7f5df1918556]
/lib/x86_64-linux-gnu/libproj.so.19(+0xb8083)[0x7f5de99be083]
ERROR: LoadError: failed process: Process(`gmt --show-library`, ProcessSignaled(11)) [0]

```

when I run `gmt --show-library` it gives the same segfault/backtrace. This is using the current Debian/testing version of GMT, anyone else having this issue?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 3, 2022, 3:03pm UTC](https://discourse.julialang.org/t/help-me-with-julia-r-comparison-project-how-to-plot-us-county-maps-in-plots-statsplots-or-whats-best-alternative/75686/7 "2022-02-03T15:03:08Z")

</div>

> [@dlakelan](#):
>
> `Failed to precompile GMT`

There is a [related thread here](https://discourse.julialang.org/t/instructions-to-install-gmt-jl-on-ubuntu/73486/5) concerning GMT installation issues in linux, in case it helps.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [February 3, 2022, 4:39pm UTC](https://discourse.julialang.org/t/help-me-with-julia-r-comparison-project-how-to-plot-us-county-maps-in-plots-statsplots-or-whats-best-alternative/75686/8 "2022-02-03T16:39:24Z")

</div>

I reported a bug to Debian and they helped me figure out what was wrong (another package was keeping an older version of libproj installed) after appropriate upgrades and removing the old library gmt works on the command line, so I’ll make some effort to see if I can do something with the GMT.jl solution thanks much!

@rafael.guerra I have the shape file from the above mentioned county information, and would like to just see a map of all counties. The linked example produces the Choropleth after manipulating various things, but for just a first peek, I tried:

```julia
countymap = gmtread("data/cb_2018_us_county_5m.shp")
GMT.plot(countymap)

```

and get nothing. Can you offer suggestion as to how to just see outlines of all the counties, or something similar?

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [February 3, 2022, 5:54pm UTC](https://discourse.julialang.org/t/help-me-with-julia-r-comparison-project-how-to-plot-us-county-maps-in-plots-statsplots-or-whats-best-alternative/75686/9 "2022-02-03T17:54:24Z")

</div>

@dlakelan Glad you were able to figure out the installing problem because I was clueless.

Regarding the plots, you must first understand that GMT works a bit differently from the other plotting packages (but, don’t they always do among each other?). In GMT, the plotting command adds a layer to the PostScript file. This means we never have the entire plot in memory but that we keep adding layers at need. We only display the figure when it’s finalized, and we do that by using the option `show=true`. The intermediate layers are added by using the `!` notation. _i.e._  
`plot(...); plot!(...); plot!(..., show=true)`  
since you didn’t provided the `show` option there is nothing to be … shown.

I tried that shapefile and there is something wrong somewhere (likely in the GMT.jl side) when I try to plot it all because many polygons are not shown, but it worked nicely whem imposing a map limit

```julia
countymap = gmtread("cb_2018_us_county_5m/cb_2018_us_county_5m.shp");
plot(countymap, region=(-125,-65,20,50), proj=:guess, show=true)

```

 ![GMTjl_tmp-fs8](https://global.discourse-cdn.com/julialang/original/3X/e/d/ed7d3499008dbe91b5f3869c586ba917370ab94e.png)

And if you want to follow that example with the Portuguese COVID map, I have to say that I should have updated it while I remembered better how to. Since then an attribute field has been added to the `GMTdataset` type and the function `make_zvals_vec()` was greatly improved to allow fetching polygons by attribute fields.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [February 3, 2022, 5:57pm UTC](https://discourse.julialang.org/t/help-me-with-julia-r-comparison-project-how-to-plot-us-county-maps-in-plots-statsplots-or-whats-best-alternative/75686/10 "2022-02-03T17:57:40Z")

</div>

Great. Thanks for this help. I’d be interested in adding a GMT related tutorial to my archive of [data analysis tutorials](https://github.com/dlakelan/JuliaDataTutorials), both so I can learn more about mapping with GMT, and so others can benefit. Would you be interested in collaborating on that?

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [February 3, 2022, 6:02pm UTC](https://discourse.julialang.org/t/help-me-with-julia-r-comparison-project-how-to-plot-us-county-maps-in-plots-statsplots-or-whats-best-alternative/75686/11 "2022-02-03T18:02:50Z")

</div>

Well, yes. Tutorials and examples is a lagging area in GMT.jl

---

<div class="post-metadata">

**Author:** ![alejandromerchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alejandromerchan/32/10500_2.png) [@alejandromerchan](https://discourse.julialang.org/u/alejandromerchan)\
**Post date:** [February 3, 2022, 6:20pm UTC](https://discourse.julialang.org/t/help-me-with-julia-r-comparison-project-how-to-plot-us-county-maps-in-plots-statsplots-or-whats-best-alternative/75686/12 "2022-02-03T18:20:38Z")

</div>

> Well, yes. Tutorials and examples is a lagging area in GMT.jl

Yes please. GMT and GMT.jl are great tools for mapping, but I do find them very complex. I know you can really do amazing plots and analysis with them, but as a novice they feel overwhelming. I’ve used them, would love to learn more, and tutorials would be a great tool for that.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [February 3, 2022, 6:25pm UTC](https://discourse.julialang.org/t/help-me-with-julia-r-comparison-project-how-to-plot-us-county-maps-in-plots-statsplots-or-whats-best-alternative/75686/13 "2022-02-03T18:25:35Z")

</div>

Great, I’ll PM you, and anyone who wants an invite to the discussion PM me and I’ll add you to the discussion. @alejandromerchan do you want to be on that PM?

---

<div class="post-metadata">

**Author:** ![alejandromerchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alejandromerchan/32/10500_2.png) [@alejandromerchan](https://discourse.julialang.org/u/alejandromerchan)\
**Post date:** [February 3, 2022, 6:28pm UTC](https://discourse.julialang.org/t/help-me-with-julia-r-comparison-project-how-to-plot-us-county-maps-in-plots-statsplots-or-whats-best-alternative/75686/14 "2022-02-03T18:28:54Z")

</div>

I did some mapping in Julia a while aga, on a smaller scale (California counties) using VegaLite. Just like you, I had experience with R and I used a project I had as an excuse to learn more Julia. I haven’t done mapping since, so I don’t know how much things have changed. You’re welcome to loop me in, I gladly help if I can.

---

<div class="post-metadata">

**Author:** ![TheCedarPrince](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thecedarprince/32/17323_2.png) [@TheCedarPrince](https://discourse.julialang.org/u/TheCedarPrince)\
**Post date:** [February 3, 2022, 6:55pm UTC](https://discourse.julialang.org/t/help-me-with-julia-r-comparison-project-how-to-plot-us-county-maps-in-plots-statsplots-or-whats-best-alternative/75686/15 "2022-02-03T18:55:18Z")

</div>

There is this example I made a while ago using Plots.jl to plot US counties if that is interesting:

> <https://gist.github.com/TheCedarPrince/52f9be6c14479fecce0b403751fafcc2>

I also have a Makie example I think somewhere if that is also of interest!  
Makie.jl was just a tad more complicated to get built than Plots’s version.

---

<div class="post-metadata">

**Author:** ![alejandromerchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alejandromerchan/32/10500_2.png) [@alejandromerchan](https://discourse.julialang.org/u/alejandromerchan)\
**Post date:** [February 3, 2022, 7:35pm UTC](https://discourse.julialang.org/t/help-me-with-julia-r-comparison-project-how-to-plot-us-county-maps-in-plots-statsplots-or-whats-best-alternative/75686/16 "2022-02-03T19:35:31Z")

</div>

Did you use GeoMakie.jl? I haven’t used it but I know there’s an effort on developing that tool.

---

<div class="post-metadata">

**Author:** ![TheCedarPrince](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thecedarprince/32/17323_2.png) [@TheCedarPrince](https://discourse.julialang.org/u/TheCedarPrince)\
**Post date:** [February 3, 2022, 8:26pm UTC](https://discourse.julialang.org/t/help-me-with-julia-r-comparison-project-how-to-plot-us-county-maps-in-plots-statsplots-or-whats-best-alternative/75686/17 "2022-02-03T20:26:42Z")

</div>

I did not!  
I actually just used Makie.jl as GeoMakie.jl was not where I needed it to be.  
I can see if I can find my example using a Shapefile plus Makie if that would be of interest @alejandromerchan 🙂

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [August 26, 2022, 6:34pm UTC](https://discourse.julialang.org/t/help-me-with-julia-r-comparison-project-how-to-plot-us-county-maps-in-plots-statsplots-or-whats-best-alternative/75686/18 "2022-08-26T18:34:11Z")

</div>

Hi all, I was just reminded of this thread because of a notification. Here is a link to the github where I added a bunch of mapping examples to the GMTMaps.jmd file [GitHub - dlakelan/JuliaDataTutorials: Tutorials For Data Analysis in Julia](https://github.com/dlakelan/JuliaDataTutorials)

I don’t remember exactly what the status was because last updated 7 months ago, but I wanted to let people easily find the mapping examples. Enjoy

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [August 26, 2022, 10:23pm UTC](https://discourse.julialang.org/t/help-me-with-julia-r-comparison-project-how-to-plot-us-county-maps-in-plots-statsplots-or-whats-best-alternative/75686/19 "2022-08-26T22:23:52Z")

</div>

@dlakelan I took the liberty to add your nice examples to the [GMT.jl manual](https://www.generic-mapping-tools.org/GMTjl_doc/tutorials/dlakelan/GMTMaps/)
