Legend Function Doesn't Make a Legend Appear - r

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!

Related

visjs graph2d showMinorLines example

I want to hide the horizontal major/minor lines that appear on Graph2D Y-axis values of a scatter plot/timeline.
I have tried to make use of showMinorLines and showMajorLines without much success. The syntax am using is:
graph2d.setOptions({showMinorLines: false})
However, it generates an error that states:
Unknown option detected: "showMinorLines". Did you mean
"showMinorLabels"?
Problem value found at: options = { showMinorLines }
vis.js:23607 Errors have been found in the supplied options object.
Any help would be much appreciated?
Update: Attaching image.
Thanks for bringing this up. This is an fault in the documentation: in v4, this option has been renamed to get a more consistent API. You now have the following four related options:
{
showMinorLabels: true | false, // minor labels and lines on horizontal axis
showMajorLabels: true | false, // major labels and lines on horizontal axis
dataAxis: {
showMinorLabels: true | false, // minor labels and lines on vertical axis
showMajorLabels: true | false, // major labels and lines on vertical axis
}
}
We will fix the docs, sorry for the inconvenience.

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

R + xyplot + key with multiple references

I have a plot that is similar to this:
w=rnorm(9)
z=rnorm(9)
A=as.factor(c(rep(c("A1","A2","A3"),3)))
B=as.factor(c(rep("B1",3),rep("B2",3),rep("B3",3)))
C=as.factor(c("C1","C1","C2","C2","C3","C3","C1","C2","C3"))
xyplot(w~z,type="p",cex=1.4,
panel=function(x, y, ...) {
panel.xyplot(x=z[1], y=w[1],pch=15,col="red",...);
panel.xyplot(x=z[2], y=w[2],pch=15,col="green",...);
panel.xyplot(x=z[3], y=w[3],pch=15,col="blue",...);
panel.xyplot(x=z[4], y=w[4],pch=16,col="red",...);
panel.xyplot(x=z[5], y=w[5],pch=16,col="green",...);
panel.xyplot(x=z[6], y=w[6],pch=16,col="blue",...);
panel.xyplot(x=z[7], y=w[7],pch=17,col="red",...);
panel.xyplot(x=z[8], y=w[8],pch=17,col="green",...);
panel.xyplot(x=z[9], y=w[9],pch=17,col="blue",...);
ltext(x=x, y=y+0.1, labels=C)
})
And now I have been trying a lot without success to get a key like this:
I tried with key function, with legend function, trying to create more than one key in the same graph… I’m lost!!
I know you are doing this in lattice, but ggplot makes this kind of thing pretty easy.
my.data<-data.frame(w,z,A,B,C)
ggplot(my.data,aes(x=w,y=z,colour=A,shape=B,label=C)) +
geom_point(size=3) +
geom_text(hjust=-0.2,vjust=-0.2)
I had a hard time figuring out how to get a guide for the labels. But then I realized that if you have the labels, why do you need a guide?

Small issue with R plot - lines() stop before end value

I am in the process of making a nice graph but I encountered an issue with the extremities of the lines I drew. I do not know why R does not plot the lines all the way until maxxx (10 here). You can see on the graph from the code below what I am talking about (I cannot put picture as my reputation is too low), the extremities of the lines stop before 10:
maxxx<-10
plot(0,type="n",axes=FALSE,xlim=c(0,10),ylim=c(0,10),ylab="",xlab="")
mtext("TROLOLOL",side=3,cex=3)
axis(1,pos=0,at=c(0,10),labels=FALSE)
mtext("R1 ",side=1,line=0,cex=3)
axis(2,pos=0,at=c(0,10),labels=FALSE)
mtext("R2",side=2,line=0,cex=3)
Given_growth_rates<-c(0,0.3,0.5,1,2,5);
K1<-7
g1<-5.5
m<-0.2
R1_isoclines<-numeric(length(Given_growth_rates))
for (i in 1:length(Given_growth_rates)){
R1_isoclines[i]<-((Given_growth_rates[i]+m)*K1)/(g1-Given_growth_rates[i]-m)
}
R1_isoclines
K2<-10
g2<-7
R2_isoclines<-numeric(length(Given_growth_rates))
for (i in 1:length(Given_growth_rates)){
R2_isoclines[i]<-((Given_growth_rates[i]+m)*K2)/(g2-Given_growth_rates[i]-m)
}
R2_isoclines
for (i in 1:length(R1_isoclines)){
lines(rep(R1_isoclines[i],times=length(R2_isoclines[i]:maxxx)), R2_isoclines[i]:maxxx, col=i+1, type="l")
lines(R1_isoclines[i]:maxxx,rep(R2_isoclines[i],times=length(R1_isoclines[i]:maxxx)),col=i+1,type="l")
}
Try to replace the last four lines of your code by:
for (i in 1:length(R1_isoclines)){
lines(rep(R1_isoclines[i],times=2), c(R2_isoclines[i],maxxx), col=i+1, type="l")
lines(c(R1_isoclines[i], maxxx), rep(R2_isoclines[i],times=2),col=i+1,type="l")
}
Maybe that does what you expect.

Google charts tools Stacked chart

I'm new to Google Charts tools and I was wondering what I'am doing wrong. I want to make the BarChart (or ColumnChart) stacked by adding 'isStacked':true but then the chart gives me wrong data.
You can try it yourself on the Google playground with this code
(Just add 'isStacked':true to the options to see the wrong results)
function drawVisualization() {
// Create and populate the data table.
var data = new google.visualization.DataTable({"cols":[{"id":"","label":"Date","type":"string"},
{"id":"","label":"Complaints","type":"number"},
{"id":"","label":"Compliments","type":"number"},
{"id":"","label":"Questions","type":"number"},
{"id":"","label":"Suggestions","type":"number"}],
"rows":[{"c":[{"v":"12\/2011"},{"v":30},{"v":0},{"v":0},{"v":0}]},
{"c":[{"v":"1\/2012"},{"v":93},{"v":"5"},{"v":0},{"v":0}]},
{"c":[{"v":"2\/2012"},{"v":82},{"v":"5"},{"v":0},{"v":0}]},
{"c":[{"v":"3\/2012"},{"v":72},{"v":"10"},{"v":0},{"v":0}]},
{"c":[{"v":"4\/2012"},{"v":68},{"v":"8"},{"v":0},{"v":0}]},
{"c":[{"v":"5\/2012"},{"v":59},{"v":"7"},{"v":0},{"v":0}]},
{"c":[{"v":"6\/2012"},{"v":30},{"v":"3"},{"v":"3"},{"v":0}]},
{"c":[{"v":"7\/2012"},{"v":37},{"v":"3"},{"v":"4"},{"v":"3"}]},
{"c":[{"v":"8\/2012"},{"v":31},{"v":"2"},{"v":"5"},{"v":0}]},
{"c":[{"v":"9\/2012"},{"v":47},{"v":"2"},{"v":"1"},{"v":"1"}]},
{"c":[{"v":"10\/2012"},{"v":67},{"v":0},{"v":"5"},{"v":"1"}]},
{"c":[{"v":"11\/2012"},{"v":38},{"v":"1"},{"v":"4"},{"v":0}]},
{"c":[{"v":"12\/2012"},{"v":14},{"v":"1"},{"v":"1"},{"v":"1"}]}
]});
// Create and draw the visualization.
new google.visualization.BarChart(document.getElementById('visualization')).
draw(data,
{title:"Yearly Coffee Consumption by Country",
width:1000, height:400,
vAxis: {title: "Year"},
hAxis: {title: "Cups"}}
);
}
I hope somebody can help me...
Thanks!
I just had the very same problem. The solution is, numeric values should NOT be between quotation marks, otherwise "3"+"4" becomes 34 instead of 7.
Just remove the " marks if there's a numeric value there.
cheers, greg
UPDATE: if you use 'f:' values as well, you will need the quotation marks again, otherwise mouseover might not work on the chart.
{"c":[{"v":"12\/2012"},{"v":14, "f":"14"},{"v":1, "f":"1"},{"v":1, "f":"1"},{"v":1, "f":"1"}]}

Resources