Hi the list I would like to add a legend under a graph but at a fixed distance from the graphe. Is it possible ? More precisely, here is my code : --- 8< ---- symboles <- c(3,4,5,6) dn <- rbind(matrix(rnorm(20),,5),matrix(rnorm(20,2),,5)) listSymboles <- rep(symboles,each=2) matplot(t(dn),pch=listSymboles,type="b") legend("bottom", pch = unique(listSymboles), legend = c("ane", "cheval", "poney", "mule"), inset = c(0,-0.175), horiz = TRUE, xpd = NA) --- 8< ---- But when I change the size of the graph, the legend is misplaced. Instead, I try to put some text in xlab, but I do not know how to get the +, x , V and other symbol. Does anyone got a solution ? Thanks a lot. Christophe
It is best to create the graphics device at the final size desired, then do the plotting and add the legend. For getting a fixed distance, look at the function grconvertY for one possibility. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Christophe Genolini > Sent: Friday, December 05, 2008 6:40 AM > To: r-help at r-project.org > Subject: [R] legend at fixed distance form the bottom > > Hi the list > > I would like to add a legend under a graph but at a fixed distance from > the graphe. Is it possible ? > More precisely, here is my code : > > --- 8< ---- > symboles <- c(3,4,5,6) > dn <- rbind(matrix(rnorm(20),,5),matrix(rnorm(20,2),,5)) > listSymboles <- rep(symboles,each=2) > matplot(t(dn),pch=listSymboles,type="b") > legend("bottom", pch = unique(listSymboles), legend = c("ane", > "cheval", > "poney", "mule"), inset = c(0,-0.175), horiz = TRUE, xpd = NA) > --- 8< ---- > > But when I change the size of the graph, the legend is misplaced. > > Instead, I try to put some text in xlab, but I do not know how to get > the +, x , V and other symbol. > Does anyone got a solution ? > > Thanks a lot. > > Christophe > > ______________________________________________ > 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.
Thanks for your answer. Unfortunatly, I can not create the graphice with the final size since I am writing a package in wich the user will have to chose between several graphics, and then he will have to export one. And they might be one graph, or 2x2, or 3x3... I check the grconvertY but I did not understand what you suggest. To me, the use of legend can not work since every length in the legend box (xlength, ylength, distance to axes) will change when resizing the graph. I was more expecting something like introduce the symbols used in the graph *in* the xlab. Is it possible ? Christophe> It is best to create the graphics device at the final size desired, then do the plotting and add the legend. For getting a fixed distance, look at the function grconvertY for one possibility. > > -- > Gregory (Greg) L. Snow Ph.D. > Statistical Data Center > Intermountain Healthcare > greg.snow at imail.org > 801.408.8111 > > > >> -----Original Message----- >> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- >> project.org] On Behalf Of Christophe Genolini >> Sent: Friday, December 05, 2008 6:40 AM >> To: r-help at r-project.org >> Subject: [R] legend at fixed distance form the bottom >> >> Hi the list >> >> I would like to add a legend under a graph but at a fixed distance from >> the graphe. Is it possible ? >> More precisely, here is my code : >> >> --- 8< ---- >> symboles <- c(3,4,5,6) >> dn <- rbind(matrix(rnorm(20),,5),matrix(rnorm(20,2),,5)) >> listSymboles <- rep(symboles,each=2) >> matplot(t(dn),pch=listSymboles,type="b") >> legend("bottom", pch = unique(listSymboles), legend = c("ane", >> "cheval", >> "poney", "mule"), inset = c(0,-0.175), horiz = TRUE, xpd = NA) >> --- 8< ---- >> >> But when I change the size of the graph, the legend is misplaced. >> >> Instead, I try to put some text in xlab, but I do not know how to get >> the +, x , V and other symbol. >> Does anyone got a solution ? >> >> Thanks a lot. >> >> Christophe >> >> ______________________________________________ >> 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. >> > >
inset= is measured from the margin so the trick is to inset it relative to the top even if you want it at the bottom: legend("top", pch = unique(listSymboles), legend = c("ane", "cheval", "poney", "mule"), inset = c(0,1.1), horiz = TRUE, xpd = NA) On Fri, Dec 5, 2008 at 8:39 AM, Christophe Genolini <cgenolin at u-paris10.fr> wrote:> Hi the list > > I would like to add a legend under a graph but at a fixed distance from the > graphe. Is it possible ? > More precisely, here is my code : > > --- 8< ---- > symboles <- c(3,4,5,6) > dn <- rbind(matrix(rnorm(20),,5),matrix(rnorm(20,2),,5)) > listSymboles <- rep(symboles,each=2) > matplot(t(dn),pch=listSymboles,type="b") > legend("bottom", pch = unique(listSymboles), legend = c("ane", "cheval", > "poney", "mule"), inset = c(0,-0.175), horiz = TRUE, xpd = NA) > --- 8< ---- > > But when I change the size of the graph, the legend is misplaced. > > Instead, I try to put some text in xlab, but I do not know how to get the +, > x , V and other symbol. > Does anyone got a solution ? > > Thanks a lot. > > Christophe > > ______________________________________________ > 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. >
Thanks to answering me Gabor I am not sur I understand : I had your line to my graph. Then I resize my graph but the legend is still moving. In "inset = c(0,1.1)", 1.1 means "110% of the y length of the plot region". So when the plot region change, the distance between the legend and the top axe is sitll 110%, that means that the distance between the legend and the bottom axe is 10%... Christophe> inset= is measured from the margin so the trick is to inset it relative to the > top even if you want it at the bottom: > > legend("top", pch = unique(listSymboles), legend = c("ane", "cheval", > "poney", "mule"), > inset = c(0,1.1), horiz = TRUE, xpd = NA) > > On Fri, Dec 5, 2008 at 8:39 AM, Christophe Genolini > <cgenolin at u-paris10.fr> wrote: > >> Hi the list >> >> I would like to add a legend under a graph but at a fixed distance from the >> graphe. Is it possible ? >> More precisely, here is my code : >> >> --- 8< ---- >> symboles <- c(3,4,5,6) >> dn <- rbind(matrix(rnorm(20),,5),matrix(rnorm(20,2),,5)) >> listSymboles <- rep(symboles,each=2) >> matplot(t(dn),pch=listSymboles,type="b") >> legend("bottom", pch = unique(listSymboles), legend = c("ane", "cheval", >> "poney", "mule"), inset = c(0,-0.175), horiz = TRUE, xpd = NA) >> --- 8< ---- >> >> But when I change the size of the graph, the legend is misplaced. >> >> Instead, I try to put some text in xlab, but I do not know how to get the +, >> x , V and other symbol. >> Does anyone got a solution ? >> >> Thanks a lot. >> >> Christophe >> >> ______________________________________________ >> 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. >> >> > >
> I would like to add a legend under a graph but at a fixed distance from the graphe"The top of the legend is always a fixed amount below the graph (which is what you asked for).
Lol. To me, a "fixed amount" that depends on the size of the graph is not much fixed... --- 8< --- symboles <- c(3,4,5,6) dn <- rbind(matrix(rnorm(20),,5),matrix(rnorm(20,2),,5)) listSymboles <- rep(symboles,each=2) layout(matrix(c(1,1,1,2,2,3),3)) for(i in 1:3){ plot(dn) legend("top", pch = unique(listSymboles), legend = c("a", "b","c","d"),inset = c(0,1.1), horiz = TRUE, xpd = NA) } --- 8< --- On this example, it is easy to see that index is at a fixed distance from the bottom but that Legend is at a distance "porportional">> I would like to add a legend under a graph but at a fixed distance from the graphe" >> > > The top of the legend is always a fixed amount below the graph (which is > what you asked for). > >
Its independent of the scale of the graph. On Sat, Dec 6, 2008 at 12:07 PM, Christophe Genolini <cgenolin at u-paris10.fr> wrote:> Lol. > To me, a "fixed amount" that depends on the size of the graph is not much > fixed... > > --- 8< --- > > symboles <- c(3,4,5,6) > dn <- rbind(matrix(rnorm(20),,5),matrix(rnorm(20,2),,5)) > listSymboles <- rep(symboles,each=2) > > layout(matrix(c(1,1,1,2,2,3),3)) > for(i in 1:3){ > plot(dn) > legend("top", pch = unique(listSymboles), legend = c("a", > "b","c","d"),inset = c(0,1.1), horiz = TRUE, xpd = NA) > } > > --- 8< --- > On this example, it is easy to see that index is at a fixed distance from > the bottom but that Legend is at a distance "porportional" > > >>> I would like to add a legend under a graph but at a fixed distance from >>> the graphe" >>> >> >> The top of the legend is always a fixed amount below the graph (which is >> what you asked for). >> >> > >
I suggested the use of grconvertY if you want to place something exactly n inches from the bottom, then grconvertY will give the user (or other) coordinates that match, or if you want to place something exactly 10% of the total device height from the bottom, etc. What to do in replotting a graph afte a resize is usually tricky and requires information that the program does not have and therefore has to guess at, that is why it is safest to not depend on the computer doing the correct thing in a resize and just tell the users to resize/set size before calling your function. If you give a bit more detail on what you are trying to accomplish and what you mean by fixed distance (number of inches, proportion of device height, margin row, etc.) we may be better able to help. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Christophe Genolini > Sent: Saturday, December 06, 2008 7:08 AM > To: Greg Snow > Cc: r-help at r-project.org > Subject: Re: [R] legend at fixed distance form the bottom > > Thanks for your answer. > > Unfortunatly, I can not create the graphice with the final size since I > am writing a package in wich the user will have to chose between > several > graphics, and then he will have to export one. And they might be one > graph, or 2x2, or 3x3... > > I check the grconvertY but I did not understand what you suggest. To > me, > the use of legend can not work since every length in the legend box > (xlength, ylength, distance to axes) will change when resizing the > graph. I was more expecting something like introduce the symbols used > in > the graph *in* the xlab. Is it possible ? > > Christophe > > > It is best to create the graphics device at the final size desired, > then do the plotting and add the legend. For getting a fixed distance, > look at the function grconvertY for one possibility. > > > > -- > > Gregory (Greg) L. Snow Ph.D. > > Statistical Data Center > > Intermountain Healthcare > > greg.snow at imail.org > > 801.408.8111 > > > > > > > >> -----Original Message----- > >> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > >> project.org] On Behalf Of Christophe Genolini > >> Sent: Friday, December 05, 2008 6:40 AM > >> To: r-help at r-project.org > >> Subject: [R] legend at fixed distance form the bottom > >> > >> Hi the list > >> > >> I would like to add a legend under a graph but at a fixed distance > from > >> the graphe. Is it possible ? > >> More precisely, here is my code : > >> > >> --- 8< ---- > >> symboles <- c(3,4,5,6) > >> dn <- rbind(matrix(rnorm(20),,5),matrix(rnorm(20,2),,5)) > >> listSymboles <- rep(symboles,each=2) > >> matplot(t(dn),pch=listSymboles,type="b") > >> legend("bottom", pch = unique(listSymboles), legend = c("ane", > >> "cheval", > >> "poney", "mule"), inset = c(0,-0.175), horiz = TRUE, xpd = NA) > >> --- 8< ---- > >> > >> But when I change the size of the graph, the legend is misplaced. > >> > >> Instead, I try to put some text in xlab, but I do not know how to > get > >> the +, x , V and other symbol. > >> Does anyone got a solution ? > >> > >> Thanks a lot. > >> > >> Christophe > >> > >> ______________________________________________ > >> 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. > >> > > > > > > ______________________________________________ > 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.
Sorry, I will be more precise. Here is an example (simplified) of graph I want : ---- 8< ---- symboles <- c(3,4,5,6) dn <- rbind(matrix(rnorm(20),5)) layout(matrix(c(1,1,1,2,2,3),3)) for(i in 1:3) matplot(dn,type="b",xlab="+: a x: b ???: c ???: d",pch=symboles) ---- 8< ---- But instead of ???, I want the "triangle" and the "losange". So I try to use legend in order to get the losange and triangle : --- 8< ---- for(i in 1:3){ matplot(dn,type="b",xlab="",pch=symboles) legend("top", pch = unique(listSymboles), legend = c("a","b","c","d"), inset = c(0,1.1), horiz = TRUE, xpd = NA) } --- 8< ---- On the first plot, the legend is down, on the second, the legend is on a correct position, on the third one, the legend in *on* the graduation. So my problem is to plot either a legend at a fixed distance from the graph or to put some symbol ("triangle", "losange" and all the other since I might have more than 4 curves) in the xlab. Any solution ? Thanks Christophe> I suggested the use of grconvertY if you want to place something exactly n inches from the bottom, then grconvertY will give the user (or other) coordinates that match, or if you want to place something exactly 10% of the total device height from the bottom, etc. > > What to do in replotting a graph afte a resize is usually tricky and requires information that the program does not have and therefore has to guess at, that is why it is safest to not depend on the computer doing the correct thing in a resize and just tell the users to resize/set size before calling your function. > > If you give a bit more detail on what you are trying to accomplish and what you mean by fixed distance (number of inches, proportion of device height, margin row, etc.) we may be better able to help. > > -- > Gregory (Greg) L. Snow Ph.D. > Statistical Data Center > Intermountain Healthcare > greg.snow at imail.org > 801.408.8111 > > > >> -----Original Message----- >> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- >> project.org] On Behalf Of Christophe Genolini >> Sent: Saturday, December 06, 2008 7:08 AM >> To: Greg Snow >> Cc: r-help at r-project.org >> Subject: Re: [R] legend at fixed distance form the bottom >> >> Thanks for your answer. >> >> Unfortunatly, I can not create the graphice with the final size since I >> am writing a package in wich the user will have to chose between >> several >> graphics, and then he will have to export one. And they might be one >> graph, or 2x2, or 3x3... >> >> I check the grconvertY but I did not understand what you suggest. To >> me, >> the use of legend can not work since every length in the legend box >> (xlength, ylength, distance to axes) will change when resizing the >> graph. I was more expecting something like introduce the symbols used >> in >> the graph *in* the xlab. Is it possible ? >> >> Christophe >> >> >>> It is best to create the graphics device at the final size desired, >>> >> then do the plotting and add the legend. For getting a fixed distance, >> look at the function grconvertY for one possibility. >> >>> -- >>> Gregory (Greg) L. Snow Ph.D. >>> Statistical Data Center >>> Intermountain Healthcare >>> greg.snow at imail.org >>> 801.408.8111 >>> >>> >>> >>> >>>> -----Original Message----- >>>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- >>>> project.org] On Behalf Of Christophe Genolini >>>> Sent: Friday, December 05, 2008 6:40 AM >>>> To: r-help at r-project.org >>>> Subject: [R] legend at fixed distance form the bottom >>>> >>>> Hi the list >>>> >>>> I would like to add a legend under a graph but at a fixed distance >>>> >> from >> >>>> the graphe. Is it possible ? >>>> More precisely, here is my code : >>>> >>>> --- 8< ---- >>>> symboles <- c(3,4,5,6) >>>> dn <- rbind(matrix(rnorm(20),,5),matrix(rnorm(20,2),,5)) >>>> listSymboles <- rep(symboles,each=2) >>>> matplot(t(dn),pch=listSymboles,type="b") >>>> legend("bottom", pch = unique(listSymboles), legend = c("ane", >>>> "cheval", >>>> "poney", "mule"), inset = c(0,-0.175), horiz = TRUE, xpd = NA) >>>> --- 8< ---- >>>> >>>> But when I change the size of the graph, the legend is misplaced. >>>> >>>> Instead, I try to put some text in xlab, but I do not know how to >>>> >> get >> >>>> the +, x , V and other symbol. >>>> Does anyone got a solution ? >>>> >>>> Thanks a lot. >>>> >>>> Christophe >>>> >>>> ______________________________________________ >>>> 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. >>>> >>>> >>> >> ______________________________________________ >> 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. >> > >