DataGraph Forums › Technical Support › Support Desk › Filled polygons for geographic shapes
Hi,
I would like to use Datagraph to illustrate points and plots in geographic space, which is easy enough using – for example – longitude or latitude as X and Y, or in fact any other geographic map projection coordinates. However, for this to really make sense, I would like to also plot the country shapes below it. So I’d like to load in a dataset of polygon data in my respective map projection coordinate system and be able to plot those alongside my points/lines.
I think the only thing missing for that to work is a command for “filled polygons”. It could simply take in ordered data points for the polygon boundaries, with empty lines signifying new polygons, and then one could specify a filling colour.
Or does that already exist?
I know of the cool example using a world map and hurricane data in the examples list, but that doesn’t work for me, as I really would like to have vectorised polygons to zoom in and such. Thanks!
Stephan
I can now provide an example that show what I have in mind.
Here is a map plot made with R’s ggplot:
And here is a map I made with Datagraph:
And you can see that it comes close, but filled Polygons would really help to make the land-mass stand out more clearly.
Thanks so much for showing the image. Very helpful and the map looks very nice in DG 🙂
You can fill a polygon using the Plot command by having the first and last point be the same location. Since you have multiple polygons, here are two ways you could approach adding the fill, depending on the format of the data.
Example 1: Add a line break between each polygon.
You can add a line break into the data by selecting a line, control-clicking, and then select to Add a line.
Here is an example where the break is added in the data table and we have data for two polygons. Note how the first and last x and y location are the same for each. When you change Fill to ‘Solid color’, you will get two filled shapes.
Example 2: Use a mask and a command for each polygon.
If you have a column that defines the polygon, then you can use separate commands for each one and add a mask. In this case, you do not need to have a break between in the data table.
One thing you might want to do is to set the aspect ratio to 1. You can do that in the Axis settings.
Do either of these suggestions seem like they would work?
We are actually quite interested in having a command that could do this without either adding the breaks or the masks. We are also interested in having some basic mapping capabilities native to DataGraph.
Did you export the boundary lines from R to DataGraph? Any details you can provide would be helpful to us. Thanks!
Oh wow… of course I knew about the fill-option, but I didn’t even try it because I thought that could only be used to fill against an axis or against another curve. This is awesome and it worked like a charm.
And yes, using empty lines to delineate one country shape from another is exactly the way I handled this. Here is the R code, for the record:
ed <- rnaturalearth::ne_countries(scale = “medium”, returnclass= “sf”)
coords <- sf::st_coordinates(ed)
rows <- “X Y”
last_levels <- coords[1, c(3,4,5)]
l <- 1
for(i in 1:nrow(coords)) {
if(!setequal(last_levels, coords[i, c(3,4,5)])) {
l <- l + 1
rows[l] <- “”
}
l <- l + 1
rows[l] <- paste(coords[i, c(1, 2)], collapse = ” “)
last_levels <- coords[i, c(3,4,5)]
}
f = file(“results/world_country_polygons.tsv”)
writeLines(rows, f)
close(f)
It looks quite verbose, but I couldn’t figure out how to introduce empty lines in a simpler less-verbose way with dplyr
or tidyr
and friends… so opted with this (ugly) for loop. But it did the job. The file results/world_country_polygons.tsv
then contains the polygon data for the polygon plotting.
Wonderful! Thanks for the help. And I am attaching the new DG version with filled country shapes:
DataGraph Forums › Technical Support › Support Desk › Filled polygons for geographic shapes