CRP
CRP

Reputation: 435

Background color of a .shp using `readShapeSpatial` function

I was looking to use a background color on a .shp using readShapeSpatial function. Anyone can help me with this?. Is there any function similar (except: readShapePoly) that can be used to add a background color to the .shp files?

This is part of my code...

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()

Thanks and have a nice day!

Upvotes: 0

Views: 688

Answers (1)

mnel
mnel

Reputation: 115392

In general a shp file will not a have a background, it will be up to how the plots are created what the background colour will be.

Using the example from ?readShapeSpatial

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

(1) Set while plotting using bg

i.e.

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

(2) set he plot background as "transparent" and define in the output device

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

Upvotes: 1

Related Questions