Reputation: 132
Using pointLabel in the maptools package in R will plot text labels for points to avoid overlap of text.
But is there a way to avoid/minimize overlap of text labels with the outline of an underlying polygon created from a shape file?
For example, when plotting location of census blocks, one would like the text labels not to fall on top of nearby census block borders, etc.
The data I am using was acquired from the 2000 Census Blocks Version 12A located at: http://www.nyc.gov/html/dcp/download/bytes/nycd_12aav.zip
and unpacks to the following 5 files:
nycd.dbf
nycd.prj
nycd.shp
nycd.shp.xml
nycd.shx
I intended to label the various blocks from my own text file that contains the vertical list:
Zone 1
Zone 2
Zone 3
Zone 4
etc.
I loaded the following libraries:
library(gpclib)
library(maptools)
library(RColorBrewer)
library(classInt)
library(maps)
I then tried:
zip=readShapePoly(file.choose())
and selected the nycd.shp
file above. Then:
plot(zip, col="lightgray", border="black", axes=TRUE, pbg="white")
And if it won't cause other conflicts with the labeling, I would prefer to color it:
zip@data$noise <- rnorm(nrow(zip@data))
colors=brewer.pal(9, "YlOrRd")
cols[is.na(cols)] <- "#D9D9D9"
brks=classIntervals(zip$noise, n=9, style="quantile")$brks
plot(zip, col=colors[findInterval(zip$noise, brks,all.inside=TRUE)], axes=F)
How would I label the various regions by Zone 1, Zone 2 etc, without/minimizing the label from extending outside each polygon? My questions implies I know how to label shape files with text. I don't know how to label shapes with words, only points with their xy value, or as an expression like alpha/beta with pointLabel
. I can figure out some functionality with the maps
tool if the text is contained within the original file and can therefore be accessed with a $name
extension. Similar to code I saw from http://geography.uoregon.edu/GeogR/examples/maps_examples02.htm:
# map of large cities
data(world.cities) # make the world cities location data set from the maps package available
# match the large cities with those in the database
m <- match(paste(tolower(as.character(cities$City)),tolower(as.character(cities$Country))),
paste(tolower(world.cities$name),tolower(world.cities$country.etc)))
# assign the world.cities location information to the large cities
big.cities <- NULL
big.cities$name <- cities2$City
big.cities$long <- world.cities$long[m]
big.cities$lat <- world.cities$lat[m]
big.cities
# plot the map
map("world")
map.axes()
points(big.cities$long,big.cities$lat, col="blue")
text(big.cities$long, big.cities$lat, big.cities$name, col="red", cex=.5
But unfortunately this is not a solution for me as there is no $name
extension with the labels I would like to use. All I can say is that I didn't post until I went through this site with the internal search, and the last few days online googling. From what I have seen is that this site and this community helps beginners [relative to the very advanced skills of the people on here] like me.
Thank you in advance.
Upvotes: 1
Views: 4183
Reputation: 263451
In the absence of an example I will offer what is readily available in the plotrix package
> require(plotrix)
> x<-rnorm(10)
> y<-rnorm(10)
> plot(x,y); thigmophobe.labels(x,y,labels=paste(round(x,2),round(y,2)))
Upvotes: 1