2

I am trying to create random plots in a polygon shapefile and then to plot it with index no as 1,2,3,... along with a legend comprising XY coordinates corresponding to each index no.

Code example:

data(meuse.grid)
gridded(meuse.grid) = ~x+y
image(meuse.grid)
plot(meuse.grid, main="Inventory Region")
plots <- points(spsample(meuse.grid, n=10, type='regular'), col='red', pch=20, cex=2)

How do I assign an index for each point created and then plot it with the XY coordinates?

5
  • What do you mean by "index"? A number from 1 to np? What is np? Is it a small number, otherwise a legend with each point will be too big. Do you have to use the sp package (which you've not included with library) or could you use sf instead? Do the points have to be "regular" sampled?
    – Spacedman
    Sep 6 '19 at 7:57
  • Searching for "R add table to plot" finds this which might help your legend: stackoverflow.com/questions/15406969/…
    – Spacedman
    Sep 6 '19 at 7:59
  • @Spacedman hi thank you for your comment. By index I meant a serial no. from 1 to the nth point. np is the object I created based on the sampling intensity, sorry for not clarifying in the question. In my case np = 10. The idea is to create 10 random points (regular) in a polygon shapefile and then create a plot showing the polygon boundary with all random points with a serial no. assign to each point. I would also like to display a legend with all these 10 points XY Coordinates Sep 6 '19 at 9:41
  • Note you don't always get exactly 10 points when sampling regularly with n=10 because its not always possible to place exactly 10 points on a regular grid within your region.
    – Spacedman
    Sep 6 '19 at 10:59
  • There is another solution provided by @zx8754 Creating random points in a polygon shapefile Sep 10 '19 at 6:53
1

Like this:

First, get sample points:

> pts = spsample(meuse.grid, n=10, type='regular')

plot the grid and add the points as small red dots (looks awful against the background but adjust colour for your application):

> plot(meuse.grid)
> points(pts,pch=19,cex=0.5,col="red")

Next we add the text at an offset from the point as a label:

> offs = 50
> text(
   coordinates(pts)+cbind(
     rep(offs,length(pts)),
     rep(offs,length(pts))),
   labels=as.character(1:length(pts)),col="red")

also in red. Yuck.

Now add a table of coordinates using the method in the Q I linked in comments. Build a table by column:

> legendtable = c(
   "ID",rep(1:length(pts)),
   "x",coordinates(pts)[,1],
   "y", coordinates(pts)[,2])

and feed to legend:

> legend("topleft",ncol=3,title="ID",legend=legendtable)

enter image description here

You may want to use format or sprintf to adjust the number of decimals. I'll not bother since your actual application might have different numbers and that's a separate question answered elsewhere.

5
  • Thank you for the answer. In adding the text to the point as label, Im getting an error as: Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘coordinates’ for signature ‘"NULL"’ Sep 9 '19 at 2:43
  • What has spsample returned as points? "signature "NULL"" sounds like it returned zero points.
    – Spacedman
    Sep 9 '19 at 6:18
  • It worked finally but still few issues remaining. first is How todisplay index number on each points in the map and secondly the placement of the legend. At the moment the legend is intersecting the map. Sep 9 '19 at 6:55
  • What about them? Can you fix that for your self? I see no point in me spending time making this plot look any different since unless you are actually working with this data (and not just using it as an example) then you'll need to adjust the legend position etc for your data. See the help for legend for more.
    – Spacedman
    Sep 9 '19 at 7:00
  • There is another solution provided by @zx8754 Link Sep 10 '19 at 6:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.