Skip to content

Plotting legend outside of chart area

4 messages · Samantha Allcock, Peter Alspach, Antonio Serrano

#
Hello,

I am trying to add a legend to my PCA plot so that it looks neat. I think plotting this outside of the chart area would be good but I cannot seem to fathom the correct code for this. I wondered if anyone could help please?

The code I am using is as follows:

grp<- with(Matan, cut(R_category_no,14, labels=1:14))
cols <- c("grey0", "wheat", "red", "cyan", "orange", "darkolivegreen2", "purple3",
"royalblue", "burlywood4", "orchid", "forestgreen", "green",
"gray", "yellow1")
plot(geopca, display="sites", scaling=3, type="n")
points(geopca, display="sites", scaling=3, col=cols[grp], pch=16)

legend("bottomright", col=c("grey0", "wheat", "red", "cyan", "orange", "darkolivegreen2",
"purple3", "royalblue", "burlywood4", "orchid", "forestgreen", "green",
"gray", "yellow1"), c("Control type 1", "Control type 2",
"External/Courtyard", "Midden", "Animal Occupation",
"External fire installations and ashy deposits",
"Internal fire installations and ashy deposits", "Hearth make-up",
"Floors and surfaces", "Plasters and clay features", "Storage features",
"Platforms and benches", "Mortars", "Roofs and roofing materials"), pch=16,
cex=0.75, bty="n")

Thank you for your time in advance


Dr Samantha Lee Allcock
Faculty of Science and Technology
Department of Archaeology, Anthropology and Forensic Science
Christchurch House Rm: C133
Bournemouth University
Talbot Campus
Poole
BH12 5BB
Tel: 01202 9(62474)

sallcock at bournemouth.ac.uk<mailto:sallcock at bournemouth.ac.uk>
research.bournemouth.ac.uk/2014/07/inea-project-2


BU is a Disability Two Ticks Employer and has signed up to the Mindful Employer charter. Information about the accessibility of University buildings can be found on the BU DisabledGo webpages This email is intended only for the person to whom it is addressed and may contain confidential information. If you have received this email in error, please notify the sender and delete this email, which must not be copied, distributed or disclosed to any other person. Any views or opinions presented are solely those of the author and do not necessarily represent those of Bournemouth University or its subsidiary companies. Nor can any contract be formed on behalf of the University or its subsidiary companies via email.
#
Tena koe Samantha

You probably need to set some graphics parameters such as xpd and mar (see ?par), and then give the ? and y location of the legend rather than 'bottomright' (see ?legend).

HTH ....

Peter Alspach

PS Please don't post in html (see the posting guide) ... P 

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Samantha Allcock
Sent: Thursday, 25 June 2015 4:35 a.m.
To: 'r-help at r-project.org'
Subject: [R] Plotting legend outside of chart area

Hello,

I am trying to add a legend to my PCA plot so that it looks neat. I think plotting this outside of the chart area would be good but I cannot seem to fathom the correct code for this. I wondered if anyone could help please?

The code I am using is as follows:

grp<- with(Matan, cut(R_category_no,14, labels=1:14)) cols <- c("grey0", "wheat", "red", "cyan", "orange", "darkolivegreen2", "purple3", "royalblue", "burlywood4", "orchid", "forestgreen", "green", "gray", "yellow1") plot(geopca, display="sites", scaling=3, type="n") points(geopca, display="sites", scaling=3, col=cols[grp], pch=16)

legend("bottomright", col=c("grey0", "wheat", "red", "cyan", "orange", "darkolivegreen2", "purple3", "royalblue", "burlywood4", "orchid", "forestgreen", "green", "gray", "yellow1"), c("Control type 1", "Control type 2", "External/Courtyard", "Midden", "Animal Occupation", "External fire installations and ashy deposits", "Internal fire installations and ashy deposits", "Hearth make-up", "Floors and surfaces", "Plasters and clay features", "Storage features", "Platforms and benches", "Mortars", "Roofs and roofing materials"), pch=16, cex=0.75, bty="n")

Thank you for your time in advance


Dr Samantha Lee Allcock
Faculty of Science and Technology
Department of Archaeology, Anthropology and Forensic Science Christchurch House Rm: C133 Bournemouth University Talbot Campus Poole
BH12 5BB
Tel: 01202 9(62474)

sallcock at bournemouth.ac.uk<mailto:sallcock at bournemouth.ac.uk>
research.bournemouth.ac.uk/2014/07/inea-project-2


BU is a Disability Two Ticks Employer and has signed up to the Mindful Employer charter. Information about the accessibility of University buildings can be found on the BU DisabledGo webpages This email is intended only for the person to whom it is addressed and may contain confidential information. If you have received this email in error, please notify the sender and delete this email, which must not be copied, distributed or disclosed to any other person. Any views or opinions presented are solely those of the author and do not necessarily represent those of Bournemouth University or its subsidiary companies. Nor can any contract be formed on behalf of the University or its subsidiary companies via email.


______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
The contents of this e-mail are confidential and may be ...{{dropped:14}}
#
First, you have to increase the bottom margin to have enough space for the legend.
You do it like this:
par(
    mar = c(6,4,4,2)
)
?mar? A numerical vector of the form ?c(bottom, left, top, right)?
          which gives the number of lines of margin to be specified on
          the four sides of the plot.  The default is ?c(5, 4, 4, 2) +
          0.1?.
So, we have increased the first number from 5 to 6 to have mor space at the bottom.

Then, in the legend(), you have to add the option:
inset = c(0, -0.2),
inset: inset distance(s) from the margins as a fraction of the plot
          region when legend is placed by keyword.
Change the -0.2 and the 6 in the par(mar) until you get a nice chart

 

 

Antonio Serrano
aasdelat at aim.com
?

 

 

-----Original Message-----
From: Samantha Allcock <sallcock at bournemouth.ac.uk>
To: 'r-help at r-project.org' <r-help at r-project.org>
Sent: Wed, Jun 24, 2015 10:08 pm
Subject: [R] Plotting legend outside of chart area


Hello,

I am trying to add a legend to my PCA plot so that it looks neat. I
think plotting this outside of the chart area would be good but I cannot seem to
fathom the correct code for this. I wondered if anyone could help please?

The
code I am using is as follows:

grp<- with(Matan, cut(R_category_no,14,
labels=1:14))
cols <- c("grey0", "wheat", "red", "cyan", "orange",
"darkolivegreen2", "purple3",
"royalblue", "burlywood4", "orchid",
"forestgreen", "green",
"gray", "yellow1")
plot(geopca, display="sites",
scaling=3, type="n")
points(geopca, display="sites", scaling=3, col=cols[grp],
pch=16)

legend("bottomright", col=c("grey0", "wheat", "red", "cyan",
"orange", "darkolivegreen2",
"purple3", "royalblue", "burlywood4", "orchid",
"forestgreen", "green",
"gray", "yellow1"), c("Control type 1", "Control type
2",
"External/Courtyard", "Midden", "Animal Occupation",
"External fire
installations and ashy deposits",
"Internal fire installations and ashy
deposits", "Hearth make-up",
"Floors and surfaces", "Plasters and clay
features", "Storage features",
"Platforms and benches", "Mortars", "Roofs and
roofing materials"), pch=16,
cex=0.75, bty="n")

Thank you for your time in
advance


Dr Samantha Lee Allcock
Faculty of Science and
Technology
Department of Archaeology, Anthropology and Forensic
Science
Christchurch House Rm: C133
Bournemouth University
Talbot
Campus
Poole
BH12 5BB
Tel: 01202
9(62474)

sallcock at bournemouth.ac.uk<mailto:sallcock at bournemouth.ac.uk>
research.bournemouth.ac.uk/2014/07/inea-project-2


BU
is a Disability Two Ticks Employer and has signed up to the Mindful Employer
charter. Information about the accessibility of University buildings can be
found on the BU DisabledGo webpages This email is intended only for the person
to whom it is addressed and may contain confidential information. If you have
received this email in error, please notify the sender and delete this email,
which must not be copied, distributed or disclosed to any other person. Any
views or opinions presented are solely those of the author and do not
necessarily represent those of Bournemouth University or its subsidiary
companies. Nor can any contract be formed on behalf of the University or its
subsidiary companies via email.

	[[alternative HTML version
deleted]]

______________________________________________
R-help at r-project.org
mailing list -- To UNSUBSCRIBE and more,
see
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.
1 day later
#
First, you have to increase the bottom margin to have enough space for the legend.
 You do it like this:
 par(
     mar = c(6,4,4,2)
 )
 
 From R help:
      ?mar? A numerical vector of the form ?c(bottom, left, top, right)?
           which gives the number of lines of margin to be specified on
           the four sides of the plot.  The default is ?c(5, 4, 4, 2) +
           0.1?.
 So, we have increased the first number from 5 to 6 to have mor space at the bottom.
 
 Then, in the legend(), you have to add the option:
 inset = c(0, -0.2),
 
 From R help:
    inset: inset distance(s) from the margins as a fraction of the plot
           region when legend is placed by keyword.
 Change the -0.2 and the 6 in the par(mar) until you get a nice chart
    
    
   
   
    
 

 

Antonio Serrano
aasdelat at aim.com
?

 

 

-----Original Message-----
From: Samantha Allcock <sallcock at bournemouth.ac.uk>
To: 'r-help at r-project.org' <r-help at r-project.org>
Sent: Wed, Jun 24, 2015 10:08 pm
Subject: [R] Plotting legend outside of chart area


Hello,

I am trying to add a legend to my PCA plot so that it looks neat. I
think plotting this outside of the chart area would be good but I cannot seem to
fathom the correct code for this. I wondered if anyone could help please?

The
code I am using is as follows:

grp<- with(Matan, cut(R_category_no,14,
labels=1:14))
cols <- c("grey0", "wheat", "red", "cyan", "orange",
"darkolivegreen2", "purple3",
"royalblue", "burlywood4", "orchid",
"forestgreen", "green",
"gray", "yellow1")
plot(geopca, display="sites",
scaling=3, type="n")
points(geopca, display="sites", scaling=3, col=cols[grp],
pch=16)

legend("bottomright", col=c("grey0", "wheat", "red", "cyan",
"orange", "darkolivegreen2",
"purple3", "royalblue", "burlywood4", "orchid",
"forestgreen", "green",
"gray", "yellow1"), c("Control type 1", "Control type
2",
"External/Courtyard", "Midden", "Animal Occupation",
"External fire
installations and ashy deposits",
"Internal fire installations and ashy
deposits", "Hearth make-up",
"Floors and surfaces", "Plasters and clay
features", "Storage features",
"Platforms and benches", "Mortars", "Roofs and
roofing materials"), pch=16,
cex=0.75, bty="n")

Thank you for your time in
advance


Dr Samantha Lee Allcock
Faculty of Science and
Technology
Department of Archaeology, Anthropology and Forensic
Science
Christchurch House Rm: C133
Bournemouth University
Talbot
Campus
Poole
BH12 5BB
Tel: 01202
9(62474)

sallcock at bournemouth.ac.uk<mailto:sallcock at bournemouth.ac.uk>
research.bournemouth.ac.uk/2014/07/inea-project-2


BU
is a Disability Two Ticks Employer and has signed up to the Mindful Employer
charter. Information about the accessibility of University buildings can be
found on the BU DisabledGo webpages This email is intended only for the person
to whom it is addressed and may contain confidential information. If you have
received this email in error, please notify the sender and delete this email,
which must not be copied, distributed or disclosed to any other person. Any
views or opinions presented are solely those of the author and do not
necessarily represent those of Bournemouth University or its subsidiary
companies. Nor can any contract be formed on behalf of the University or its
subsidiary companies via email.

	[[alternative HTML version
deleted]]

______________________________________________
R-help at r-project.org
mailing list -- To UNSUBSCRIBE and more,
see
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.