How to fill multiple areas with different colours in a map & adding labels so as not overlapping each other in R? -
this follow question earlier post.
i have been able of way answer help. still trying fill function work correctly. have each padd in data set different colour. padd made of number of states.
here data:
structure(list(state = structure(c(1l, 2l, 3l, 4l, 5l, 6l, 7l, 8l, 9l, 10l, 11l, 12l, 13l, 14l, 15l, 16l, 17l, 18l, 19l, 20l, 21l, 22l, 22l, 23l, 24l, 25l, 26l, 27l, 28l, 29l, 30l, 31l, 32l, 32l, 33l, 34l, 35l, 36l, 37l, 38l, 39l, 40l, 41l, 42l, 43l, 44l, 45l, 46l, 47l, 48l, 49l, 50l), .label = c("alabama", "alaska", "arizona", "arkansas", "california", "colorado", "connecticut", "delaware", "florida", "georgia", "hawaii", "idaho", "illinois", "indiana", "iowa", "kansas", "kentucky", "louisiana", "maine", "maryland", "massachusetts", "michigan", "minnesota", "mississippi", "missouri", "montana", "nebraska", "nevada", "new hampshire", "new jersey", "new mexico", "new york", "north carolina", "north dakota", "ohio", "oklahoma", "oregon", "pennsylvania", "rhode island", "south carolina", "south dakota", "tennessee", "texas", "utah", "vermont", "virginia", "washington", "west virginia", "wisconsin", "wyoming"), class = "factor"), state = structure(c(1l, 2l, 3l, 4l, 5l, 6l, 7l, 8l, 9l, 10l, 11l, 12l, 13l, 14l, 15l, 16l, 17l, 18l, 19l, 20l, 21l, 22l, 22l, 23l, 24l, 25l, 26l, 27l, 28l, 29l, 30l, 31l, 32l, 32l, 33l, 34l, 35l, 36l, 37l, 38l, 39l, 40l, 41l, 42l, 43l, 44l, 45l, 46l, 47l, 48l, 49l, 50l), .label = c("alabama", "alaska", "arizona", "arkansas", "california", "colorado", "connecticut", "delaware", "florida", "georgia", "hawaii", "idaho", "illinois", "indiana", "iowa", "kansas", "kentucky", "louisiana", "maine", "maryland", "massachusetts", "michigan", "minnesota", "mississippi", "missouri", "montana", "nebraska", "nevada", "new hampshire", "new jersey", "new mexico", "new york", "north carolina", "north dakota", "ohio", "oklahoma", "oregon", "pennsylvania", "rhode island", "south carolina", "south dakota", "tennessee", "texas", "utah", "vermont", "virginia", "washington", "west virginia", "wisconsin", "wyoming"), class = "factor"), stateabbreviation = structure(c(2l, 1l, 4l, 3l, 5l, 6l, 7l, 8l, 9l, 10l, 11l, 13l, 14l, 15l, 12l, 16l, 17l, 18l, 21l, 20l, 19l, 22l, 22l, 23l, 25l, 24l, 26l, 29l, 33l, 30l, 31l, 32l, 34l, 34l, 27l, 28l, 35l, 36l, 37l, 38l, 39l, 40l, 41l, 42l, 43l, 44l, 46l, 45l, 47l, 49l, 48l, 50l), .label = c("ak", "al", "ar", "az", "ca", "co", "ct", "de", "fl", "ga", "hi", "ia", "id", "il", "in", "ks", "ky", "la", "ma", "md", "me", "mi", "mn", "mo", "ms", "mt", "nc", "nd", "ne", "nh", "nj", "nm", "nv", "ny", "oh", "ok", "or", "pa", "ri", "sc", "sd", "tn", "tx", "ut", "va", "vt", "wa", "wi", "wv", "wy"), class = "factor"), padd = structure(c(4l, 5l, 5l, 4l, 5l, 1l, 2l, 2l, 2l, 2l, 5l, 1l, 3l, 3l, 3l, 3l, 3l, 4l, 2l, 2l, 2l, 3l, 3l, 3l, 4l, 3l, 1l, 3l, 5l, 2l, 2l, 4l, 2l, 2l, 2l, 3l, 3l, 3l, 5l, 2l, 2l, 2l, 3l, 3l, 4l, 1l, 2l, 2l, 5l, 2l, 3l, 1l), .label = c("pad iv", "padd i", "padd ii", "padd iii", "padd v"), class = "factor"), subareainpadd = structure(c(1l, 1l, 1l, 1l, 1l, 1l, 2l, 3l, 4l, 4l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 2l, 3l, 2l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 2l, 3l, 1l, 3l, 3l, 4l, 1l, 1l, 1l, 1l, 3l, 2l, 4l, 1l, 1l, 1l, 1l, 2l, 4l, 1l, 4l, 1l, 1l), .label = c("", "sub a", "sub b", "sub c"), class = "factor"), subareainpaddname = structure(c(1l, 1l, 1l, 1l, 1l, 1l, 4l, 2l, 3l, 3l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 4l, 2l, 4l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 4l, 2l, 1l, 2l, 2l, 3l, 1l, 1l, 1l, 1l, 2l, 4l, 3l, 1l, 1l, 1l, 1l, 4l, 3l, 1l, 3l, 1l, 1l ), .label = c("", "central atlantic", "lower atlantic", "new england" ), class = "factor"), paddname = structure(c(2l, 5l, 5l, 2l, 5l, 4l, 1l, 1l, 1l, 1l, 5l, 4l, 3l, 3l, 3l, 3l, 3l, 2l, 1l, 1l, 1l, 3l, 3l, 3l, 2l, 3l, 4l, 3l, 5l, 1l, 1l, 2l, 1l, 1l, 1l, 3l, 3l, 3l, 5l, 1l, 1l, 1l, 3l, 3l, 2l, 4l, 1l, 1l, 5l, 1l, 3l, 4l), .label = c("east coast", "gulf coast", "midwest", "rocky mountain", "west coast"), class = "factor"), rmapclassname = structure(c(2l, 1l, 3l, 4l, 5l, 6l, 7l, 8l, 9l, 10l, 1l, 11l, 12l, 13l, 14l, 15l, 16l, 17l, 18l, 19l, 20l, 21l, 22l, 23l, 24l, 25l, 26l, 27l, 28l, 29l, 30l, 31l, 33l, 32l, 34l, 35l, 36l, 37l, 38l, 39l, 40l, 41l, 42l, 43l, 44l, 45l, 46l, 47l, 48l, 49l, 50l, 51l), .label = c("", "alabama", "arizona", "arkansas", "california", "colorado", "connecticut", "delaware", "florida", "georgia", "idaho", "illinois", "indiana", "iowa", "kansas", "kentucky", "louisiana", "maine", "maryland", "massachusetts:main", "michigan:north", "michigan:south", "minnesota", "mississippi", "missouri", "montana", "nebraska", "nevada", "new hampshire", "new jersey", "new mexico", "new york:long island", "new york:main", "north carolina:main", "north dakota", "ohio", "oklahoma", "oregon", "pennsylvania", "rhode island", "south carolina", "south dakota", "tennessee", "texas", "utah", "vermont", "virginia:main", "washington:main", "west virginia", "wisconsin", "wyoming"), class = "factor")), .names = c("state", "state", "stateabbreviation", "padd", "subareainpadd", "subareainpaddname", "paddname", "rmapclassname"), class = "data.frame", row.names = c(na,-52l))
my current code:
location<- "c:/" filename<-"statespadds.csv" padds<- read.csv(file=paste(location, filename, sep="")) padds<-subset(padds, state != "alaska") padds<-subset(padds, state != "hawaii") padd_names<-unique(padds$paddname) map("state", interior = false) padd1<-subset(padds, padd=="padd i") padd2<-subset(padds, padd=="padd ii") padd3<-subset(padds, padd=="padd iii") padd4<-subset(padds, padd=="padd iv") padd5<-subset(padds, padd=="padd v") map('state', region = padd1$rmapclassname, fill=true, col="red", add=true, names=true) # map of 4 states map('state', region = padd2$rmapclassname, fill=true, col="green", add=true, names=true) # map of 4 states map('state', region = padd3$rmapclassname, fill=true, col="blue", add=true, names=true) # map of 4 states map('state', region = padd4$rmapclassname, fill=true, col="yellow", add=true, names=true) # map of 4 states map('state', region = padd5$rmapclassname, fill=true, col="purple", add=true, names=true) # map of 4 states map.text("state", regions=padds$rmapclassname, labels=as.character(padds$stateabbreviation), add=true) #add labels
there typo in data file causing error on fill. labels still unreadable.
padd4<-subset(padds, padd=="pad iv")
i did not know function below fill full map if padd4 had no data.
map('state', region = padd4$rmapclassname, fill=true, col="yellow", add=true, names=true)
now add labels:
map.text("state", regions=padds$rmapclassname, labels=as.character(padds$stateabbreviation), add=true) #add labels
Comments
Post a Comment