使用`readShapeSpatial`函数的.shp的背景颜色

时间:2013-07-24 01:22:44

标签: r map background

我希望使用readShapeSpatial函数在.shp上使用背景颜色。任何人都可以帮我这个吗?是否有任何类似的函数(除了readShapePoly)可以用来为.shp文件添加背景颜色?

这是我的代码的一部分......

library(raster)
library(maptools)
library(mapdata)
library(maps)
library(scales)

pcontorta <- readShapeSpatial("PER_water_areas_dcw.shp")
pcontorta2 <- readShapeSpatial("BOL_water_areas_dcw.shp")
pcontorta3 <- readShapeSpatial("ECU_water_areas_dcw.shp")
pcontorta4 <- readShapeSpatial("PRY_water_areas_dcw.shp")
pcontorta5 <- readShapeSpatial("PER_adm2.shp")
pcontorta6 <- readShapeSpatial("BOL_adm2.shp")
pcontorta7 <- readShapeSpatial("ECU_adm2.shp")
pcontorta8 <- readShapeSpatial("PRY_adm2.shp")

read.csv("coord.csv") -> data
Data <- data.frame(data[,1:4])

Data[Data$species=="B._nn",] -> primera
Data[Data$species=="B._ghn",] -> segunda
Data[Data$species=="B._bolivianus",] -> tercera
Data[Data$species=="B._brevirostris",] -> cuarta
Data[Data$species=="B._diasphanus",] -> quinta
Data[Data$species=="B._osgoodi",] -> sexta
Data[Data$species=="B._pachacuti",] -> septima
Data[Data$species=="B._phoenicoteru",] -> octava
Data[Data$species=="B._pectinatus",] -> novena
Data[Data$species=="B._peruanus",] -> decima
Data[Data$species=="B._thomasi",] -> once
rownames(Data) <- data[,1]
attach(Data)
na.omit(Data) -> Data
alt <- raster("alt.bil")
extent_all <-extent (-85, -50, -30, 5)
crop(alt, extent_all)->alt
tiff(filename = "Mapa.tiff",res = 800, pointsize = 6, width = 3200, height = 3200, units = "px")

#...
#Continues a little bit...plotting...

box()
dev.off()

谢谢,祝你有愉快的一天!

1 个答案:

答案 0 :(得分:1)

一般情况下,一个shp文件不具有背景,它将取决于如何创建绘图的背景颜色。

使用?readShapeSpatial

中的示例
xx <- readShapeSpatial(system.file("shapes/sids.shp", package="maptools")[1],
                   IDvar="FIPSNO", proj4string=CRS("+proj=longlat +ellps=clrk66"))

(1)使用bg

进行绘图时设置

 plot(xx, col = 'red', bg = 'blue')

(2)将绘图背景设置为"transparent"并在输出设备中定义

tiff(filename = "Mapa.png",bg = 'green')
plot(xx, bg = 'transparent', col = 'white')
dev.off()
# which gives (converting to `png` to allow this to be uploaded to SO)

enter image description here