提供此地图但我没有数据的代码:
library(RColorBrewer)
library(classInt)
library(maptools)
plotvar <- fecondite$fecond1524
nclr <- 4
plotclr <- brewer.pal(nclr,"PuOr")
plotclr <- plotclr[nclr:1] # r´eordonne les couleurs
class <- classIntervals(plotvar, nclr, style="equal")
colcode <- findColours(class, plotclr)
plot(departements,col=colcode)
locator(n=1) #sert `a trouver les coordonn´ees
du point o`u vous souhaitez placer la l´egende
legend(166963,6561753,legend=names(attr(colcode,"table")),
fill=attr(colcode, "palette"), cex=0.6, bty="n")
实际上我想知道:例如,一个包含1到1000之间数字的向量与部门有关如何为每个间隔分配不同的颜色[1,100],[100,500],[500 ,1000]?
答案 0 :(得分:1)
这就是你要找的东西吗?
library(raster)
fr <- getData(country='France',level=2)
#plot(fr)
# random terrain colors
fr$col <- terrain.colors(256)[floor(runif(length(fr),0,255))]
# less random
#fr$col <- terrain.colors(length(fr))
plot(fr,col=fr$col)
您可以访问fr$NAME_2
下的部门名称,然后相应地指定颜色:
color_df <- data.frame(Dep=fr$NAME_2,color=sample(colors(distinct=T),length(fr),replace = F))
fr$col <- color_df$color
plot(fr,col=fr$col)
print(color_df)
这个例子有点傻,因为它又是随机颜色。但您可以为每个部门指定一个渐变或任何其他颜色值。
library(raster)
fr <- getData(country='France',level=2)
fr$col[fr$NAME_2 == 'Hautes-Alpes'] <- 'red'
plot(fr,col=fr$col)
library(raster)
fr <- getData(country='France',level=2)
# fertility classes
classes <- c('0-1.4','1.4-2.4','2.5-3')
#fake data
fr$fertility <- sample(seq(1,3,0.1),length(fr),replace = T)
# assign groups
fr$fertgroups <- round(fr$fertility)
# assign corresponding colors (red = low, yellow = mid, green = high)
fr$cols <- c('red','yellow','green')[fr$fertgroups]
#plot
plot(fr,col=fr$cols)
或者有4个班级:
library(raster)
fr <- getData(country='France',level=2)
classes <- c('0-1','1-1.5','1.5-2','2-3')
fr$fertility <- sample(seq(1,3,0.1),length(fr),replace = T)
fr$fertgroups[fr$fertility <=1] <-1
fr$fertgroups[fr$fertility > 1 & fr$fertility <=1.5] <-2
fr$fertgroups[fr$fertility > 1.5 & fr$fertility <=2] <-3
fr$fertgroups[fr$fertility > 2 & fr$fertility <=3] <- 4
fr$cols <- c('red','orange','yellow','green')[fr$fertgroups]
plot(fr,col=fr$cols)