확인, 여기에 (내가 참조 할 수 ggplot
솔루션을 떠나) ggplot
를 사용하지 않는 대체 솔루션입니다. 이 코드는 간단하지만 자신의 데이터에 어떻게 적용 할 수 있는지에 대한 아이디어 만 있으면 충분합니다.
# UK shapefile found via http://www.gadm.org/download
uk.url <- "http://www.filefactory.com/file/s3dz3jt3vr/n/GBR_adm_zip"
# replace following with your working directory - no trailing slash
work.dir <- "C:/Temp/r.temp/gb_map"
# the full file path for storing file
file.loc <- paste0(work.dir, "/uk.zip")
download.file (uk.url, destfile = file.loc, mode = "wb")
unzip(file.loc, exdir = work.dir)
# open the shapefile
require(rgdal)
uk <- readOGR(work.dir, layer = "GBR_adm2")
# make some fake data to plot
[email protected]$count <- round(runif(nrow([email protected]), 0, 2500), 0)
[email protected]$count <- as.numeric([email protected]$count)
# and plot it
plot(uk, col = gray([email protected]$count/2500))
결과 코드는 다음과 같습니다. 범례를 포함하도록 요청을 다음
편집, 내가 코드를 조금 쥐게했지만 모든 정직에서 나는 생산 품질의 무언가를 얻을 충분히 기본 R의 legend
기능을 이해하지 않고, 내가 더이 그것을 더 연구하고 싶다. (덧붙여서 모자 팁은 아이디어로는 this question입니다.) 코드 밑의 줄거리를 보면, 범례 색상 등을 재정렬해야한다는 것을 알았지 만,이를 연습용으로 원래 포스터에 남기거나 다른 질문으로 게시 할 것입니다.
# UK shapefile found via http://www.gadm.org/download
uk.url <- "http://www.filefactory.com/file/s3dz3jt3vr/n/GBR_adm_zip"
# replace following with your working directory - no trailing slash
work.dir <- "C:/Temp/r.temp/gb_map"
# the full file path for storing file
file.loc <- paste0(work.dir, "/uk.zip")
download.file (uk.url, destfile = file.loc, mode = "wb")
unzip(file.loc, exdir = work.dir)
# open the shapefile
require(rgdal)
uk <- readOGR(work.dir, layer = "GBR_adm2")
# make some fake data to plot
[email protected]$count <- as.numeric(round(runif(nrow([email protected]), 0, 2500), 0))
[email protected]$bin <- cut([email protected]$count, seq(0, 2500, by = 250),
include.lowest = TRUE, dig.lab = 4)
# labels for the legend
lev = levels([email protected]$bin)
lev2 <- gsub("\\,", " to ", lev)
lev3 <- gsub("\\]$", "", lev2)
lev4 <- gsub("\\(|\\)", " ", lev3)
lev5 <- gsub("^\\[", " ", lev4)
my.levels <- lev5
# Create a function to generate a continuous color palette
rbPal <- colorRampPalette(c('red','blue'))
[email protected]$Col <- rbPal(10)[as.numeric(cut([email protected]$count, seq(0, 2500, by = 250)))]
# Plot
plot(uk, col = [email protected]$Col)
legend("topleft", fill = [email protected]$Col, legend = my.levels, col = [email protected]$Col)
그게 훌륭합니다. 고마워요! 이것에 해당하는 범례를 만드는 법을 알고 있습니까? – userk
짧은 대답은 '정말로'아닙니다. 위의 편집 된 답변을 참조하십시오. – SlowLearner