Skip to content

Instantly share code, notes, and snippets.

@rCarto
Created December 8, 2016 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rCarto/b7736bc52af1964f3af6f0bf4e38ab53 to your computer and use it in GitHub Desktop.
Save rCarto/b7736bc52af1964f3af6f0bf4e38ab53 to your computer and use it in GitHub Desktop.
library(cartography)
data("nuts2006")
# GDP par habitants (en euros)
nuts2.df$gdpinh <- 1000000 * nuts2.df$gdppps2008 / nuts2.df$pop2008
boxplot(nuts2.df$gdpinh)
hist(nuts2.df$gdpinh)
rug(nuts2.df$gdpinh)
# Bon d'accord la distribution n'est pas vraiment normale...
# Je triche et j'enlève les 3 nuts dont la valeur dépasse 60000 e
# (londres, luxembourg et liechtenstein)
nuts2.df[nuts2.df$gdpinh >= 60000, "gdpinh"] <- NA
boxplot(nuts2.df$gdpinh)
hist(nuts2.df$gdpinh)
rug(nuts2.df$gdpinh)
# Yes c'est normal!
# taille des classes = 1 écart-type, la moyenne est borne de classe
bks <- getBreaks(v = nuts2.df$gdpinh, method = "msd", k = 1, middle = FALSE)
points(x = bks, y = rep(60, length(bks)))
# choix d'une palette de couleurs
cols <- carto.pal(pal1 = "turquoise.pal", n1 = 3, pal2 = "wine.pal", n2 = 4)
# carto
opar <- par(mar = c(0, 0, 1.2, 0))
choroLayer(spdf = nuts2.spdf, df = nuts2.df, var = "gdpinh",
breaks = bks, col = cols, border = NA,
colNA = NA,
legend.pos = "topright")
plot(nuts0.spdf, add=T, lwd = 0.7)
layoutLayer(title = "MSD démo",
sources = "eurostat" ,author = "tg",
theme = "turquoise.pal", scale = NULL,
north = FALSE)
barscale(size = 500, lwd = 1.5, cex = 0.7)
north(pos = "topleft", south = T)
par(opar)
### La même chose avec une classe centrale
# taille des classes = 1 écart-type, la moyenne est e centre d'une classe centrale
bks <- getBreaks(v = nuts2.df$gdpinh, method = "msd", k = 1, middle = TRUE)
hist(nuts2.df$gdpinh)
rug(nuts2.df$gdpinh)
points(x = bks, y = rep(60, length(bks)))
# choix d'une palette de couleurs
cols <- carto.pal(pal1 = "turquoise.pal", n1 = 2, pal2 = "wine.pal", n2 = 3, middle = TRUE)
# carto
opar <- par(mar = c(0, 0, 1.2, 0))
choroLayer(spdf = nuts2.spdf, df = nuts2.df, var = "gdpinh",
breaks = bks, col = cols, border = NA,
colNA = "yellow",
legend.pos = "topright")
plot(nuts0.spdf, add=T, lwd = 0.7)
layoutLayer(title = "MSD démo",
sources = "eurostat" ,author = "tg",
theme = "turquoise.pal", scale = NULL,
north = FALSE)
barscale(size = 500, lwd = 1.5, cex = 0.7)
north(pos = "topleft", south = T)
par(opar)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment