Plot a matrix

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Plot a matrix

Myriam Croze
Hello!

I need your help to plot my data. I have a file .csv which looks like that:

41540 41540 41442 41599 41709 41823 41806 41837 41898 41848
41442 0.001
41599 0.002 0.001
41709 0.004 0.003 0.003
41823 0.002 0.001 0.002 0.001
41806 0.004 0.004 0.005 0.006 0.005
41837 0.004 0.004 0.005 0.006 0.005 0.001
41898 0.004 0.004 0.005 0.006 0.005 0.001 0.001
41848 0.005 0.004 0.005 0.007 0.005 0.001 0.001 0.001

It is a matrix of distance with in the 1st column and row the days of
sampling and then the distance values.
I would like to do a scatterplot of the data with for the y axis the
distance values and for the x axis the difference between the days of
sampling (e.g. x = |41442-41540| and y = 0.001).
Do you know how I could do that with r?
Thanks in advance for your help.

Best regards,
Myriam

--
Myriam Croze
Post-doctorante
Division of EcoScience,
Ewha Womans University
Seoul, South Korea

Email: [hidden email]

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Plot a matrix

Jim Lemon-4
Hi Myriam,
This may not be the ideal way to do this, but I think it works:

mcdf<-read.table(text="41540 41540 41442 41599 41709 41823 41806 41837
41898 41848
41442 0.001
41599 0.002 0.001
41709 0.004 0.003 0.003
41823 0.002 0.001 0.002 0.001
41806 0.004 0.004 0.005 0.006 0.005
41837 0.004 0.004 0.005 0.006 0.005 0.001
41898 0.004 0.004 0.005 0.006 0.005 0.001 0.001
41848 0.005 0.004 0.005 0.007 0.005 0.001 0.001 0.001",
fill=TRUE)
nrows<-nrow(mcdf)
ncols<-ncol(mcdf)
mcdf2<-mcdf
for(row in 2:nrows) {
 for(col in 2:ncols) {
  if(!is.na(mcdf2[row,col])) mcdf2[row,col]<-mcdf[row,1]-mcdf[1,col]
 }
}
plot(0,xlim=range(as.numeric(unlist(mcdf2[2:nrows,2:ncols])),na.rm=TRUE),
 ylim=range(as.numeric(unlist(mcdf[2:nrows,2:ncols])),na.rm=TRUE),
 xlab="Difference in days",ylab="Distance",type="n")
for(row in 2:nrows) {
 for(col in 2:ncols) {
  if(!is.na(mcdf2[row,col])) points(mcdf2[row,col],mcdf[row,col])
 }
}

Jim
On Thu, Nov 1, 2018 at 5:21 PM Myriam Croze <[hidden email]> wrote:

>
> Hello!
>
> I need your help to plot my data. I have a file .csv which looks like that:
>
> 41540 41540 41442 41599 41709 41823 41806 41837 41898 41848
> 41442 0.001
> 41599 0.002 0.001
> 41709 0.004 0.003 0.003
> 41823 0.002 0.001 0.002 0.001
> 41806 0.004 0.004 0.005 0.006 0.005
> 41837 0.004 0.004 0.005 0.006 0.005 0.001
> 41898 0.004 0.004 0.005 0.006 0.005 0.001 0.001
> 41848 0.005 0.004 0.005 0.007 0.005 0.001 0.001 0.001
>
> It is a matrix of distance with in the 1st column and row the days of
> sampling and then the distance values.
> I would like to do a scatterplot of the data with for the y axis the
> distance values and for the x axis the difference between the days of
> sampling (e.g. x = |41442-41540| and y = 0.001).
> Do you know how I could do that with r?
> Thanks in advance for your help.
>
> Best regards,
> Myriam
>
> --
> Myriam Croze
> Post-doctorante
> Division of EcoScience,
> Ewha Womans University
> Seoul, South Korea
>
> Email: [hidden email]
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Plot a matrix

David Carlson
It would be much better if you had pasted the original .csv file into your message or read the file in R and used dput() to send us the result. It looks like you have a lower triangular matrix and I had to do a little editing to get it to work. I am showing the steps, but commenting them out since you will only need the command following dput(mcdf2). The first row should end with 41848 and every row show begin with #.

# dta <- "rows 41540 41442 41599 41709 41823 41806 41837 41898 41848
#  41540 NA
#  41442 0.001
#  41599 0.002 0.001
#  41709 0.004 0.003 0.003
#  41823 0.002 0.001 0.002 0.001
#  41806 0.004 0.004 0.005 0.006 0.005
#  41837 0.004 0.004 0.005 0.006 0.005 0.001
#  41898 0.004 0.004 0.005 0.006 0.005 0.001 0.001
#  41848 0.005 0.004 0.005 0.007 0.005 0.001 0.001 0.001 NA"
# mcdf<-read.table(text=dta, header=TRUE, fill=TRUE)
# mcdf
# rownames(mcdf) <- mcdf$rows
# mcdf <- mcdf[, -1]
# mcdf2 <- as.dist(mcdf)
# mcdf2
# dput(mcdf2)

mcdf2 <- structure(c(0.001, 0.002, 0.004, 0.002, 0.004, 0.004, 0.004,
0.005, 0.001, 0.003, 0.001, 0.004, 0.004, 0.004, 0.004, 0.003,
0.002, 0.005, 0.005, 0.005, 0.005, 0.001, 0.006, 0.006, 0.006,
0.007, 0.005, 0.005, 0.005, 0.005, 0.001, 0.001, 0.001, 0.001,
0.001, 0.001), Labels = c("41540", "41442", "41599", "41709",
"41823", "41806", "41837", "41898", "41848"), Size = 9L,
call = as.dist.default(m = mcdf), class = "dist", Diag = FALSE,
Upper = FALSE)

Now mcdf2 is a lower triangular distance matrix:

str(mcdf2)
#  'dist' num [1:36] 0.001 0.002 0.004 0.002 0.004 0.004 0.004 0.005 0.001 0.003 ...
#  - attr(*, "Labels")= chr [1:9] "41540" "41442" "41599" "41709" ...
#  - attr(*, "Size")= int 9
#  - attr(*, "call")= language as.dist.default(m = mcdf)
#  - attr(*, "Diag")= logi FALSE
# - attr(*, "Upper")= logi FALSE

The days are stored as an attribute called "Labels" so we need to extract them and compute the differences between them:

days <- as.numeric(attributes(mcdf2)$Labels)
daydiff <- dist(days, method="manhattan")
daydiff
#     1   2   3   4   5   6   7   8
# 2  98                            
# 3  59 157                        
# 4 169 267 110                    
# 5 283 381 224 114                
# 6 266 364 207  97  17            
# 7 297 395 238 128  14  31        
# 8 358 456 299 189  75  92  61    
# 9 308 406 249 139  25  42  11  50

Now we can plot. I've attached a copy:

plot(daydiff, mcdf2)

You need to read the following manual pages to understand what we are doing:

?read.table
?rownames
?as.dist
?dput
?as.numeric
?attributes
?dist
?plot
?Extract (to understand what the "$" is all about.

----------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77843-4352

----Original Message-----
From: R-help <[hidden email]> On Behalf Of Jim Lemon
Sent: Thursday, November 1, 2018 1:56 AM
To: [hidden email]
Cc: r-help mailing list <[hidden email]>
Subject: Re: [R] Plot a matrix

Hi Myriam,
This may not be the ideal way to do this, but I think it works:

mcdf<-read.table(text="41540 41540 41442 41599 41709 41823 41806 41837
41898 41848
41442 0.001
41599 0.002 0.001
41709 0.004 0.003 0.003
41823 0.002 0.001 0.002 0.001
41806 0.004 0.004 0.005 0.006 0.005
41837 0.004 0.004 0.005 0.006 0.005 0.001
41898 0.004 0.004 0.005 0.006 0.005 0.001 0.001
41848 0.005 0.004 0.005 0.007 0.005 0.001 0.001 0.001",
fill=TRUE)
nrows<-nrow(mcdf)
ncols<-ncol(mcdf)
mcdf2<-mcdf
for(row in 2:nrows) {
 for(col in 2:ncols) {
  if(!is.na(mcdf2[row,col])) mcdf2[row,col]<-mcdf[row,1]-mcdf[1,col]
 }
}
plot(0,xlim=range(as.numeric(unlist(mcdf2[2:nrows,2:ncols])),na.rm=TRUE),
 ylim=range(as.numeric(unlist(mcdf[2:nrows,2:ncols])),na.rm=TRUE),
 xlab="Difference in days",ylab="Distance",type="n")
for(row in 2:nrows) {
 for(col in 2:ncols) {
  if(!is.na(mcdf2[row,col])) points(mcdf2[row,col],mcdf[row,col])
 }
}

Jim
On Thu, Nov 1, 2018 at 5:21 PM Myriam Croze <[hidden email]> wrote:

>
> Hello!
>
> I need your help to plot my data. I have a file .csv which looks like that:
>
> 41540 41540 41442 41599 41709 41823 41806 41837 41898 41848
> 41442 0.001
> 41599 0.002 0.001
> 41709 0.004 0.003 0.003
> 41823 0.002 0.001 0.002 0.001
> 41806 0.004 0.004 0.005 0.006 0.005
> 41837 0.004 0.004 0.005 0.006 0.005 0.001
> 41898 0.004 0.004 0.005 0.006 0.005 0.001 0.001
> 41848 0.005 0.004 0.005 0.007 0.005 0.001 0.001 0.001
>
> It is a matrix of distance with in the 1st column and row the days of
> sampling and then the distance values.
> I would like to do a scatterplot of the data with for the y axis the
> distance values and for the x axis the difference between the days of
> sampling (e.g. x = |41442-41540| and y = 0.001).
> Do you know how I could do that with r?
> Thanks in advance for your help.
>
> Best regards,
> Myriam
>
> --
> Myriam Croze
> Post-doctorante
> Division of EcoScience,
> Ewha Womans University
> Seoul, South Korea
>
> Email: [hidden email]
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.

Plot.png (5K) Download Attachment