[R] North Arrow (.png file) on a Map

Yihui Xie xieyihui at gmail.com
Wed Mar 11 06:47:38 CET 2009


Is this "arrow" satisfactory for you?

north.arrow = function(x, y, h) {
    polygon(c(x, x, x + h/2), c(y - h, y, y - (1 + sqrt(3)/2) * h),
col = "black", border = NA)
    polygon(c(x, x + h/2, x, x - h/2), c(y - h, y - (1 + sqrt(3)/2) *
h, y, y - (1 + sqrt(3)/2) * h))
    text(x, y, "N", adj = c(0.5, 0), cex = 4)
}
plot(1, type = "n", ylim = c(0, 1))
north.arrow(1, 0.8, 0.3)

Regards,
Yihui
--
Yihui Xie <xieyihui at gmail.com>
Phone: +86-(0)10-82509086 Fax: +86-(0)10-82509086
Mobile: +86-15810805877
Homepage: http://www.yihui.name
School of Statistics, Room 1037, Mingde Main Building,
Renmin University of China, Beijing, 100872, China



On Tue, Mar 10, 2009 at 7:21 PM, Rodrigo Aluizio <r.aluizio at gmail.com> wrote:
> Hi list.
>
> I would like to know how do I insert a North arrow, stored as a png file in
> my computer, in a map? I found lots of post asking similar things, one of
> them mentioned the pixmap package.  The map was done using map() and
> shapefiles (the code is below). I’m using the pixmap () and addlogo()
> functions. Well I can import the png with pixmap() function (I guess, once
> there’s no error message), but I can’t put It on the map, I got an error
> message telling me that:
>
>
>
> “Error at t(x at index[nrow(x at index):1, , drop = FALSE]) :
>
>  index out of limits”
>
>
>
> Well I tried changing coordinates but I always got the same result. How do I
> do this correctly? Is there a better way?
>
>
>
> Thanks for the help and attention.
>
>
>
> Here is the complete map script:
>
>
>
> library(RODBC)
>
> library(maps)
>
> library(mapdata)
>
> library(maptools)
>
> library(pixmap)
>
> #Carregar Coordenadas e dados dos Pontos Amostrais
>
> Dados<-odbcConnectExcel('Campos.xls',readOnly=T)
>
> Coord<-sqlFetch(Dados,'CoordMed',colnames=F,rownames='Ponto')
>
> odbcClose(Dados)
>
> N<-pixmap('Norte.png',nrow=166,ncol=113)
>
> # Carregar pontos e shapes
>
> Batimetria<-readShapeSpatial('C:/Users/Rodrigo/Documents/UFPR/Micropaleontol
> ogia/Campos/ShapeFiles/Batimetria_BC.shp')
>
> Estados<-readShapeSpatial('C:/Users/Rodrigo/Documents/UFPR/Micropaleontologi
> a/Campos/ShapeFiles/Estados_Sudeste.shp')
>
> Faciologia<-readShapeSpatial('C:/Users/Rodrigo/Documents/UFPR/Micropaleontol
> ogia/Campos/ShapeFiles/Faciologia_BC.shp')
>
> # Mapa com os Pontos da Bacia
>
> postscript('MapaCampos.eps',paper='special',onefile=F,horizontal=F,width=3.5
> ,height=4.5,bg='white',pointsize=3)
>
> par(mar=c(3,2,2,0))
>
> map('worldHires','brazil',ylim=c(23.9,20.3),xlim=c(42.1,39.2),type='n')
>
> plot(Faciologia,ylab='',xlab='',col=c('lightgreen','lightgreen','lightgreen'
> ,'lightgreen','lightgreen','lightgray','lightgray','lightgray','lightgray','
> lightgray','lightgray','lightgray','lightgray','lightgray','lightgray','ligh
> tgray','lightgray','lightgray','lightgray','lightgray','lightgray','lightyel
> low','lightyellow','lightyellow'),add=T,lwd=0.5,border=0)
>
> plot(Batimetria,ylab='',xlab='',col='darkgray',lty='solid',lwd=0.2,add=T)
>
> plot(Estados,ylab='',xlab='',lty='solid',add=T,lwd=0.8)
>
> text(Coord$Longitude[Coord$Réplicas=='1'],Coord$Latitude[Coord$Réplicas=='1'
> ],rownames(Coord)[Coord$Réplicas=='1'],col='red',cex=0.5,font=2)
>
> text(Coord$Longitude[Coord$Réplicas=='2'],Coord$Latitude[Coord$Réplicas=='2'
> ],rownames(Coord)[Coord$Réplicas=='2'],col='yellow',cex=0.5,font=2)
>
> text(Coord$Longitude[Coord$Réplicas=='3'],Coord$Latitude[Coord$Réplicas=='3'
> ],rownames(Coord)[Coord$Réplicas=='3'],col='blue',cex=0.5,font=2)
>
> points(Coord$Longitude,Coord$Latitude-0.045,pch=20,cex=0.7)
>
> text(c(41.5,41.3),c(21.7,20.6),c('RJ','ES'))
>
> axis(1,xaxp=c(42.1,39.2,2),cex.axis=1)
>
> axis(2,yaxp=c(23.9,20.3,4),cex.axis=1)
>
> title(main='Bacia')
>
> legend(40.2,23.5,c('Uma','Duas','Três'),pch=21,cex=1,pt.bg=c('red','yellow',
> 'blue'),bty='n',pt.cex=2,pt.lwd=0.6,title='Réplicas')
>
> legend(39.8,23.5,c('Areia','Calcário','Lama'),pch=21,cex=1,pt.bg=c('lightyel
> low','lightgray','lightgreen'),bty='n',pt.cex=2,pt.lwd=0.6,title='Faciologia
> ')
>
> addlogo(N,px=c(40,39.8),py=c(21,20.8))
>
> dev.off()
>
> q('no')
>
>
>
> -------------------------------------------------------------
>
> MSc. Rodrigo Aluizio <mailto:r.aluizio at gmail.com>
>
> Centro de Estudos do Mar/UFPR
> Laboratório de Micropaleontologia
>
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>




More information about the R-help mailing list