Anti
Anti

Reputation: 397

r - Map overlaps plot borders in complex layout

I'm going to create a more complex graph with R showing values of a variable on 6 maps (ordered in 2 columns and 3 rows).

However, once I execute the code shown below, the first map always exceeds its plot borders as shown in the attached image.

require(maps)

layout(matrix(c(14,  1,  1,  1,  1,  2,  2,  2,  2,  3,  3,
                 4,  8,  8,  8,  8, 11, 11, 11, 11, 15, 15,
                 4,  8,  8,  8,  8, 11, 11, 11, 11, 15, 15,
                 4,  8,  8,  8,  8, 11, 11, 11, 11,  7, 17,
                 5,  9,  9,  9,  9, 12, 12, 12, 12,  7, 17,
                 5,  9,  9,  9,  9, 12, 12, 12, 12,  7, 17,
                 5,  9,  9,  9,  9, 12, 12, 12, 12,  7, 17,
                 6, 10, 10, 10, 10, 13, 13, 13, 13,  7, 17,
                 6, 10, 10, 10, 10, 13, 13, 13, 13, 16, 16,
                 6, 10, 10, 10, 10, 13, 13, 13, 13, 16, 16
), nrow=10, byrow=T))

par(mar=c(0.5,0.5,0.5,0.5))
plot.new()
text(0.5, 0.5, "observed", cex=2)

plot.new()
text(0.5, 0.5, "estimated", cex=2)

plot.new()
text(0.5, 0.5, paste(4478, "BP"), cex=2)

plot.new()
text(0.5, 0.5, "autosomal", cex=2, srt=90)

plot.new()
text(0.5, 0.5, "mitochondrial", cex=2, srt=90)

plot.new()
text(0.5, 0.5, "Y-chromosomal", cex=2, srt=90)

par(mar=c(0.5, 1, 0.5, 1))
Col = colorRampPalette(c("red", "blue"))
image(1, seq(0, 1, length=1000), matrix(1:1000, nrow=1), col=Col(1000), axes=F ,xlab="", ylab="")
box()
axis(4, at=c(0, 0.25, 0.5, 0.75, 1), las=2, cex.axis=1.3)

par(mar=c(0.5, 0.5, 0.5, 0.5))
map("world2",xlim=c(90,255),ylim=c(-50,35),fill=T)

map("world2",xlim=c(90,255),ylim=c(-50,35),fill=T)

map("world2",xlim=c(90,255),ylim=c(-50,35),fill=T)

map("world2",xlim=c(90,255),ylim=c(-50,35),fill=T)

map("world2",xlim=c(90,255),ylim=c(-50,35),fill=T)

map("world2",xlim=c(90,255),ylim=c(-50,35),fill=T)

par(mar=c(0, 0, 0, 0))
plot.new()

plot.new()

plot.new()

plot.new()
text(0.7, 0.5, "Asian ancestry", cex=2, srt=90)

enter image description here

How can I prevent that the first image overlaps its borders and is shown correctly (like the other map plots)?

Upvotes: 1

Views: 234

Answers (1)

jay.sf
jay.sf

Reputation: 73592

maps::map seems to internally override the par(mar(.)) you've defined. Defining in front of each map() works.

altered map part of your code:

par(mar=c(0.5, 0.5, 0.5, 0.5))
map("world2",xlim=c(90,255),ylim=c(-50,35),fill=T)

par(mar=c(0.5, 0.5, 0.5, 0.5))
map("world2",xlim=c(90,255),ylim=c(-50,35),fill=T)

par(mar=c(0.5, 0.5, 0.5, 0.5))
map("world2",xlim=c(90,255),ylim=c(-50,35),fill=T)

par(mar=c(0.5, 0.5, 0.5, 0.5))
map("world2",xlim=c(90,255),ylim=c(-50,35),fill=T)

par(mar=c(0.5, 0.5, 0.5, 0.5))
map("world2",xlim=c(90,255),ylim=c(-50,35),fill=T)

par(mar=c(0.5, 0.5, 0.5, 0.5))
map("world2",xlim=c(90,255),ylim=c(-50,35),fill=T)

Produces:

enter image description here

Upvotes: 1

Related Questions