Freddy López
Freddy López

Reputation: 35

How to change the font of the label's contours in R?

I want to change the font of the labels of the contours to black italic. I have tried using "font",values 1,2,3 or 4, but it doesn't work, with any of those value I keep getting the same plot. I think I'm missing something.

This is the code:

library("ncdf4");library("maps")

file<-"http://iridl.ldeo.columbia.edu/SOURCES/.NOAA/.NCEP/.EMC/.CMB/.GLOBAL/.Reyn_SmithOIv2/.weekly/.sst/T/%2819-25%20Feb%202017%29VALUES/data.nc"
try(download.file(url =file ,"data.nc",quiet = F,mode="wb"))
data<-nc_open("data.nc")
latGb<-ncvar_get(data,"Y")
lonGb<-ncvar_get(data,"X")
latCb<-latGb[30:121]
lonCb<-lonGb[240:301]
dat<-ncvar_get(data,"sst")
dat<-dat[240:301,30:121]

filled.contour(lonCb, latCb, dat,zlim =c(5:35),nlevels=80,
           plot.axes={
                     contour(lonCb, latCb, dat,nlevels=10,add=T,font=4,labcex=1);
                     map('world2',col="black",fill = TRUE, add=TRUE);
                     grid()})

This is want i keep getting: Pic

Thanks for the help

Upvotes: 2

Views: 1510

Answers (1)

Djork
Djork

Reputation: 3369

In contour help file, vfont is a parameter you can set by providing a vector of length 2 where the first element is the typeface, and the second element is the font face. Note, font is not a parameter within contour that you can set, that is why nothing happens when you use this. Always check the help file on what parameters can be set.

I set vfont to bold italic so it appears more solid black than grey. You select the typeface you like, I used the default sans serif.

filled.contour(lonCb, latCb, dat, zlim =c(5:35), nlevels=80,
               plot.axes={
                 contour(lonCb, latCb, dat, nlevels=10, add=T, 
                         vfont=c("sans serif", "bold italic"), labcex=1);
                 map('world2', col="black", fill = TRUE, add=TRUE);
                 grid()})

enter image description here

Upvotes: 4

Related Questions