How to change the font of the label's contours in R? - 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

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

Related

Legend Function Doesn't Make a Legend Appear

I am trying to add a legend to my R plot that has five lines which I built with the following code:
predictors=c(1,3,5,7,10,13)
meanvec25=c(23.642465, 14.590552, 11.480690, 10.919103, 9.365745, 10.265116)
meanvec100=c(22.668256, 12.220893, 10.598733, 9.768388, 9.710492, 10.139874)
meanvec500=c(21.864226, 12.290780, 9.879924, 9.598233, 9.856007, 9.629011)
meanvec2000=c(22.072814, 11.658274, 10.208657, 9.789607, 9.720389, 9.781965)
meanvec5000=c(22.006622, 11.535754, 10.172841, 9.664394, 9.722084, 9.814400)
plot(predictors,meanvec25,col="red",type="l")
lines(predictors,meanvec100,col="blue",type="l")
lines(predictors,meanvec500,col="green",type="l")
lines(predictors,meanvec2000,col="purple",type="l")
lines(predictors,meanvec5000,col="orange",type="l")
Then I typed this line to add a legend:
legend(1,95,legend=c("25 Trees","100 Trees","500 Trees","2000 Trees","5000 Trees"),col=c("red", "blue","green","purple","orange"), lty=c(1,1,1,1,1), cex=0.8)
Nothing is added to my graph, but no error messages appear either. I tried fiddling with the location numbers at the beginning of the function, but it doesn't seem to help. Why won't the legend appear?
Thanks!

Axis scaling is overridden in IDL

The scaling for axes X,Y and Z that I specify in the procedure scale3 is overridden. I don't understand why? Could someone point out what is wrong with my code?
The width of a box is n1/2-1(~128 points) and it is more than the height(98 points).
n1=257
n2=257
n3=98
file='test.ps'
set_plot,'Z'
erase,255
scale3,XRange=[0,n1-1],YRange=[0,n2-1],ZRange=[0,n3-1],az=-30,ax=30
PLOTS,[0,n1-1],[0,0],[0,0],/T3D,color=0,thick=2
PLOTS,[0,0],[0,n2-1],[0,0],/T3D,color=0,thick=2
PLOTS,[0,n1-1],[n2-1,n2-1],[0,0],/T3D,color=0,thick=2
PLOTS,[n1-1,n1-1],[0,n2-1],[0,0],/T3D,color=0,thick=2
PLOTS,[0,n1-1],[n2-1,n2-1],[n3-1,n3-1],/T3D,color=0,thick=2
PLOTS,[n1-1,n1-1],[0,n2-1],[n3-1,n3-1],/T3D,color=0,thick=2
PLOTS,[0,n1-1],[0,0],[n3-1,n3-1],/T3D,color=0,thick=2
PLOTS,[0,0],[0,n2-1],[n3-1,n3-1],/T3D,color=0,thick=2
PLOTS,[0,0],[0,0],[0,n3-1],/T3D,color=0,thick=2
PLOTS,[n1/2-1,n1/2-1],[0,0],[0,n3-1],/T3D,color=0,thick=2
PLOTS,[n1-1,n1-1],[0,0],[0,n3-1],/T3D,color=0,thick=2
PLOTS,[n1-1,n1-1],[n2-1,n2-1],[0,n3-1],/T3D,color=0,thick=2
PLOTS,[0,0],[n2-1,n2-1],[0,n3-1],/T3D,color=0,thick=2
PLOTS,[n1/2-1,n1/2-1],[0,n2-1],[n3-1,n3-1],/T3D,color=0,thick=2
PLOTS,[0,n1-1],[n2/2-1,n2/2-1],[n3-1,n3-1],/T3D,color=0,thick=2
PLOTS,[n1-1,n1-1],[n2/2-1,n2/2-1],[0,n3-1],/T3D,color=0,thick=2
PLOTS,[n1/2-1,n1/2-1],[0,n2-1],[0,0],/T3D,color=0,thick=2
PLOTS,[0,0],[n2/2-1,n2/2-1],[0,n3-1],/T3D,color=0,thick=2
PLOTS,[n1/2-1,n1/2-1],[n2-1,n2-1],[0,n3-1],/T3D,color=0,thick=2
PLOTS,[0,n1-1],[n2/2-1,n2/2-1],[0,0],/T3D,color=0,thick=2
g=TVRD()
set_plot,'PS'
device,filename=file,xsize=8,ysize=6,/encapsulated
tv,g
device,/close
end
Do you mean mean the display is anisotropic? Just add:
device, set_resolution=[500, 500]
after you change to the Z-buffer device. This is what I get:

Octave: Change color in drawArrow

I found this answer
[Octave : How to change line color and width in drawRect
how to change the color in drawRect.
When I try the same for drawArrow, I get:
h=drawArrow(0,0,3,1,1,1,1,1);
h =
scalar structure containing the fields:
body = -12.469
head = -11.925
set(h,'color','r');
error: octave_base_value::array_value():
wrong type argument 'scalar struct'
error: set: H must be a graphics handle
What I am doing wrong here? For me it looks the same.
Thanks
Karl
You really should mention that you are using the octave-forge geometry package. drawArrow returns a struct with handles for the arrow body and head (as shown by your code) so you can and have to set them separately:
pkg load geometry
h = drawArrow (0,0,3,1,1,1,1,1);
set (h.body, "color", "r")
set (h.head, "facecolor", get(h.body, "color"))
You can also set a border around the head with "edgecolor"

gvisScatterChart - hAxis.title and vAxis.title not appearing on the plot

hAxis.title and vAxis.title options don't seem to set the axis titles for gvisScatterChart in the package googleVis.
plot(gvisScatterChart(data.frame(x = 3, y = 4), options=list(hAxis.title = 'foo')))
"foo" doesn't appear as the horizontal axis title.
You're supposed to pass options to hAxis in a dictionary-looking JSON object format:
plot(gvisScatterChart(data.frame(x = 3, y = 4), options=list(hAxis="{title:'foo'}")))
See the examples at the bottom of the help page and this under hAxis:
a JSON object. Default null. An object with members to configure various horizontal axis elements. To specify properties of this object, you can use object literal notation, as shown here:
{title: 'Hello', titleTextStyle: {color: '#FF0000'}}

Scilab : add legend for surface plot

I would like to add a legend to a surface plot.
I have tried, doing like this :
X=[0:0.3:2*%pi]; //example data
[x,y]=ndgrid(X,X);
z1=sin(X')*cos(X);
z2=z1/2;
z3=z1/3;
figure=scf();
surf(x,y,z1);
surf(x,y,z2);
surf(x,y,z3);
axes=figure.children(1);
axes.children(1).foreground=color(0,0,0);
axes.children(2).foreground=color(0,0,0);
axes.children(3).foreground=color(255,0,0);
axes.children(1).color_flag=0;
axes.children(2).color_flag=0;
axes.children(3).color_flag=0;
axes.children(1).color_mode=color(0,255,0);
axes.children(2).color_mode=color(0,0,255);
axes.children(3).color_mode=0;
legend(['z1','z1 divided by 2','z1 divided by 3'],opt=2,font_size=2);
I get the following error message:
!--error 10000
legend : Neither handle of type 'Polyline' can be found.
If it's not possible to do this with the basic version of Scilab, could you please advise to me some libraries Scilab permitting to do this.
Thanks for your help.
Legend not possible for surface plots
Legends is only for plot2dx graphs as stated in the documentation of legend properties:
This entity defines the parameters for legends drawn below plot2dx
graphs or created by the captions function.
Alternatives
You could simply add a title using xtitle
xtitle('z1,z1 div by2, z2 div by 3');
You could draw a box using uicontrol and style it using uicontrol_properties:
fig = gcf();
uicontrol(fig, "style", "text", ...
"string", "<html>z1<br>z1/2<br>z1/3</html>", ...
"position",[100 100 100 100], ...
"fontsize",15);

Resources