Skip to content

Instantly share code, notes, and snippets.

@rCarto rCarto/msd.R
Created Dec 8, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.