2

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)
     })
raghav
  • 533
  • 2
  • 11
Hitesh
  • 19
  • 3
  • 2
    Do you really need 14 libraries for your 10-line example? – G5W Dec 23 '21 at 20:28
  • I think only libraries rastervis, raster, colorRamps, and lattice will be required. – raghav Dec 23 '21 at 20:42
  • 1
    [This package](https://github.com/m-jahn/lattice-tools) has a `panel.repellabels` function which may be worth trying . Otherwise you could port your code to ggplot2 and use ggrepel [example here](https://stackoverflow.com/questions/40542685/how-to-jitter-remove-overlap-for-geom-text-labels?answertab=active#tab-top) – user20650 Dec 23 '21 at 20:42
  • ... https://github.com/m-jahn/lattice-tools#paneldirectlabels looks like it should work – user20650 Dec 23 '21 at 20:59
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Jan 02 '22 at 14:12

0 Answers0