Comments (8)
Should mention I'm happy to have another go myself if there are guidelines that might help me figure out where I went wrong. :-)
from topogram.
Hello David,
Sorry for the laaaate answer. I saw your fork, it reminded me to answer you.
I found a way to create D3 cartograms with any shape, I've put it in another project to use {r2d3} (that's still in development, I haven't figured out yet if {r2d3} is the right solution, or if using {htmlwidget} directly is better...)
The project is here: https://github.com/dreamRs/r2d3maps
You'll need dev {geojsonio}.
Here an example :
# install.packages("devtools")
# devtools::install_github("ropensci/geojsonio")
library( r2d3maps )
library( rnaturalearth )
library( sf )
### New Zealand
# data from Natural Earth
nz <- ne_states(country = "new zealand", returnclass = "sf")
# crop polygons
nz <- sf::st_crop(nz, xmin = 159.104, ymin = -48.385, xmax = 193.601, ymax = -33.669)
# Classic map
d3_map(shape = nz) %>%
add_labs(title = "New Zealand")
# Cartogram
nz$foo1 <- sample(x = 1:1000, size = nrow(nz)) # dummy variable
nz$foo2 <- sample(x = 1:1000, size = nrow(nz)) # dummy variable
nz$foo3 <- sample(x = 1:1000, size = nrow(nz)) # dummy variable
# simplyfy a little shapes
nz <- rmapshaper::ms_simplify(nz)
d3_cartogram(shape = nz) %>%
add_continuous_breaks(var = "foo1", palette = "Blues") %>%
add_tooltip(value = "{name}: {foo1}") %>%
add_legend(title = "Foo") %>%
add_labs(title = "N-Z")
d3_cartogram(shape = nz) %>%
add_select_input(label = "Choose a var:", choices = c("foo1", "foo2", "foo3")) %>%
add_continuous_breaks(var = "foo1", palette = "Blues")
Result :
Again sorry for your 2017 NZ election project.
Best,
Victor
from topogram.
Hi Victor. Thanks for the reply. No worries about the delay - it was just one of several options I wanted to explore last year, with not much time to do it. I'll be better prepared for the next election. :-)
I noticed that you added a New Zealand regions shape in there, but haven't gotten that to work. I don't see any output, but perhaps I just need to figure out the correct origin settings. I can't remember what issues I had when I tried last year to add my own shapes, but I didn't get far when I had a quick trial this time. Perhaps it's a similar issue with origins, but it may be a factor that the TopoJSON I'm creating with geojsonio has a bbox object and no transform object (??).
r2d3maps looks interesting! I've managed to run your example code (though the tooltips are empty in the first one, and the multiple option switching doesn't seem to work in the second) and look forward to trying it out with my own shape files.
Thanks!
from topogram.
To update, the r2d3maps tooltips were only blank in RStudio viewer - they displayed fine in a real browser. (The multiple value example still didn't work in a browser though.)
And I managed to get the topogRam included nz-reg to work using the following code:
topogRam(
data = data.frame(
id = c(0:15),
NAME = c(as.character(0:15)),
my_data = as.integer(runif(16, min = 100, max = 1000)),
my_other_data = as.integer(runif(16, min = 100, max = 1000))
),
key_var = c("my_data", "my_other_data"),
shape = "nz-reg",
geo_lab = "NAME",
width = 500, height = 500,
origin = c(-4500, -850),
scale = 10000
)
However in coming to write this comment I see you've updated and refactored the package. It looks like a simpler and more generic interface, which is great! The examples in the readme worked fine for me, but my own attempt didn't work out of the box:
library(sf)
library(rmapshaper)
nz_regions <-
read_sf("statsnzregional-council-2018-clipped-generalised-GPKG/regional-council-2018-clipped-generalised.gpkg") %>%
filter(REGC2018_V1_00_NAME != "Area Outside Region") %>%
ms_simplify(keep = 0.002, keep_shapes = TRUE)
nzreg$foo <- runif(16, min = 1000, max = 1000000)
topogRam(
shape = nzreg,
value = "foo"
)
Have I missed something here, or have I run into an edge case? (Data from Stats NZ)
from topogram.
Hi David,
Thanks for your feedback on {r2d3maps}, indeed i have some trouble with compatibility between RStudio viewer and browser...
And yes I decided to completely rewrite {topogRam} to use a wonderful wrapper of topogram.js (https://github.com/vasturiano/cartogram-chart). I'm going to add features this week, and for now it doesn't work in RStudio viewer (I'll try with pre-release.
Your example doesn't work for now because it only support Mercator projection, this works if you change CRS (I used shapefiles from the link you provided) :
library(sf)
library(rmapshaper)
library(topogRam)
library(dplyr)
nz_regions <-
read_sf("statsnzregional-council-2018-clipped-generalised-SHP/regional-council-2018-clipped-generalised.shp") %>%
filter(REGC2018_1 != "Area Outside Region") %>%
st_transform(crs = 4326) %>% # here change crs
ms_simplify(keep = 0.002, keep_shapes = TRUE)
nz_regions$foo <- floor(runif(16, min = 1000, max = 1000000))
topogRam(
shape = nz_regions,
value = "foo",
tooltip_label = ~REGC2018_1,
n_iteration = 40
)
Victor
from topogram.
Ah, brilliant, thanks. I've transformed them to Mercator ("+proj=longlat +datum=WGS84", which is the same as 4326 I think) when creating TopoJSON files previously, so probably should have thought to do that here too.
It works great now - I look forward to the planned features. Thanks heaps this package!
from topogram.
The select menu is back, here an example in markdown : https://pvictor.github.io/NZ-topogRam/
Next features planned :
- Proxy method for shiny apps
- Legend
- labs
- Projections sf <> D3
(Feel free to close this issue if you want 😄 )
Victor
from topogram.
Awesome. Yes, you've truly solved the original "issue" now. Thanks! :-)
from topogram.
Related Issues (3)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from topogram.