Skip to content

scatter plot of numerical variables against different sample ids

4 messages · Maria Lathouri, Ivan Calandra, Jim Lemon +1 more

#
Dear all
I know that my question is very simple but although I tried to find an answer online, I couldn't and I am stuck.?
I have a dataset of three numerical variables measured in different samples ID. Something like this:
Sample ? ? ? ?Cu ? ? ? ?Zn ? ? ? ?MnM1 ? ? ? ? ? ? ? 1 ? ? ? ? ?5 ? ? ? ? ?10M2 ? ? ? ? ? ? ? 2.5 ? ? ? 11 ? ? ? ? ?8M3 ? ? ? ? ? ? ?1.15 ? ? ?11 ? ? ? ? 12 ?M4 ? ? ? ? ? ? ? ?2 ? ? ? ? 4 ? ? ? ? ?30M5 ? ? ? ? ? ? ? ?8 ? ? ? ?15 ? ? ? ? 35
I would like to plot these variables (Cu, Zn, Mn) (in y-axis) against the Sample ID (in x-axis) in a scatter plot with lines as I want to see how they change in the samples.
I tried using the command>plot(Sample, Cu, type="l", lty=1, col="red")
but I wouldn't get a line. Actually, I would get some small horizontal lines in each point. I tried to use type="p" but I would get the same thing.?
I would very much appreciate if you could help me on that. Apparently, I am missing something.?
Thank you in advance.?
Kind regards,Maria
#
Hi Maria,

What happens is that R plots with boxplots (explaining the horizontal 
lines, i.e. boxes with n=1) when the x-variable is a factor.
What you can do is transform your Sample column into numeric and then 
plot it, with some adjustment of axis labels.

For example:
datf <- data.frame(Sample=c("M1","M2","M3","M4","M5"), 
Cu=c(1,2.5,1.15,2,8), Zn=c(5,11,11,4,15), Mn=c(10,8,12,30,35))
datf$Sample2 <- as.numeric(datf$Sample)
with(datf, plot(Sample2, Cu, type="l", lty=1, col="red", xaxt="n"))
axis(1, at=datf$Sample2, labels=datf$Sample)

It might not be the easiest/best approach though... Someone here might 
have a better idea.

HTH,
Ivan

--
Ivan Calandra, PhD
MONREPOS Archaeological Research Centre and
Museum for Human Behavioural Evolution
Schloss Monrepos
56567 Neuwied, Germany
calandra at rgzm.de
+49 (0) 2631 9772-287
--
https://www.researchgate.net/profile/Ivan_Calandra
https://publons.com/author/705639/

Le 05/12/2016 ? 13:25, Maria Lathouri via R-help a ?crit :
#
Hi Maria,
Perhaps something like this:

 mldf<-read.table(text="Sample  Cu  Zn Mn
 M1 1  5  10
 M2  2.5  11   8
 M3 1.15  11  12
 M4  2  4  30
 M5  8  15 35",
 header=TRUE)
 matplot(mldf,type="b",pch=c("C","Z","M"))

Jim


On Mon, Dec 5, 2016 at 11:25 PM, Maria Lathouri via R-help
<r-help at r-project.org> wrote:
#
You can also try using ggplot2 to generate the plot:
Shawn Way, PE

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Jim Lemon
Sent: Monday, December 05, 2016 3:02 PM
To: Maria Lathouri <mlathouri at yahoo.gr>
Cc: R-help Mailing List <r-help at r-project.org>
Subject: Re: [R] scatter plot of numerical variables against different sample ids

Hi Maria,
Perhaps something like this:

 mldf<-read.table(text="Sample  Cu  Zn Mn
 M1 1  5  10
 M2  2.5  11   8
 M3 1.15  11  12
 M4  2  4  30
 M5  8  15 35",
 header=TRUE)
 matplot(mldf,type="b",pch=c("C","Z","M"))

Jim
On Mon, Dec 5, 2016 at 11:25 PM, Maria Lathouri via R-help <r-help at r-project.org> wrote:
______________________________________________
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.