Reputation: 29
I am making an xyplot in R. Everything went well except the labels positions. The labels are overlapping each other. Is there any way to separate the labels clearly e.g., using arrows, etc.? See my code below:
library(rasterVis)
library(raster)
library(colorRamps)
library(lattice)
df <- structure(list(x = c(-97.8198, -97.5489, -116.6933, -99.42, -99.5975, -98.0402, -97.4888, -96.8362, -103.65, -93.6906, -84.3324, -109.39, -106.1187, -101.8466, -111.7273, -105.1019, -111.762, -111.7718, -89.8735, -97.5684, -96.5603, -86.4131, -92.2, -96.4766, -96.4701, -96.4397, -105.5464, -116.7356, -116.7486, -116.7132, -96.902, -84.2874, -106.2618, -110.0522, -96.855), y = c(36.8193, 36.8085, 36.7653, 36.4267, 36.6358, 35.5497, 36.6058, 44.3453, 44.158, 41.9749, 35.9311, 38.09, 41.068, 43.95, 35.1426, 48.3077, 35.089, 35.4454, 34.2547, 38.7745, 39.0824, 39.3232, 38.7441, 41.1651, 41.1649, 41.1797, 40.0329, 43.1439, 43.0645, 43.1675, 43.2408, 35.9588, 40.7838, 31.7438, 37.5208), z = c(0.6, 1.2, 1.3, 1.2, 1.3, 0.7, 1, 0.6, 0.8, 0.5, 1.7, 1.2, 0.7, 1.3, 1.2, 0.8, 0.9, 1.1, 0.8, 1.1, 0.6, 1, 1.5, 0.7, 0.7, 0.7, 0.8, 0.8, 0.5, 0.8, 0.9, 2.9, 0.8, 2.4, 1), ID = c("A32", "A74", "ADR", "AR1", "AR2", "ARb", "ARM", "Bkg", "Blk","Br1","ChR", "Cop", "CPk", "Ctn", "Fmf", "FPe", "Fuf", "Fwf", "Goo", "KLS", "Kon","MMS","MOz", "Ne1", "Ne2", "Ne3", "NR1", "Rls", "Rms", "Rws", "SFP", "WBW", "Wdn", "Whs","Wlr")), row.names = c(NA, -35L), class = "data.frame")
df$z <- round(df$z,1)
x <- df$x
y <- df$y
d <- data.frame(lon=x, lat=y)
coordinates(d) <- c("lon", "lat")
proj4string(d) <- CRS("+init=epsg:4326") # WGS 84
z <- df$z
col=colorRampPalette(rev(palette(matlab.like2(10))))
pp <- levelplot(z ~ x+y, xlim=c(-125,-65), ylim=c(25,50 ), panel = panel.levelplot.points, cex= 1.5, contour = T, margin = T, col.regions = col, colorkey=list( at=seq(0.2, 3, length=10), col=col ), at=seq(0.2, 3, length=10),xlab = list("Longitude", cex = 1.5), ylab = list("Latitude", cex=1.5), scales = list(tck = c(1,0), cex=1)) + xyplot(y ~ x, data = df,panel = function(y, x, ...) {ltext(x = x, y = y, labels = df$ID, cex = 1.2, font = 2,
fontfamily = "serif",pos=2)
})
Upvotes: 2
Views: 136