User Tools

Site Tools


documents:111012rtricks

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
documents:111012rtricks [2012/02/14 21:32] – added image plot kotadocuments:111012rtricks [2020/11/26 07:08] (current) kota
Line 4: Line 4:
  
 When you import data from the out put of other software, you might have trailing 0s at the end of each column: When you import data from the out put of other software, you might have trailing 0s at the end of each column:
-<code>+<code:R>
 > d0 > d0
  [1] 69.444 70.889 86.667 95.667 83.444 78.333 67.889  [1] 69.444 70.889 86.667 95.667 83.444 78.333 67.889
Line 18: Line 18:
 </code> </code>
 To remove them, there could be many ways, but here is my one liner.  To remove them, there could be many ways, but here is my one liner. 
-<code R>+<code:R>
 d0c <- d0[rev(cumsum(rev(d0)))>0] d0c <- d0[rev(cumsum(rev(d0)))>0]
 </code> </code>
Line 34: Line 34:
 ===== Plotting a matrix data as a color-coded image ===== ===== Plotting a matrix data as a color-coded image =====
  
-<sxh>+<code:R>
 #a 2Dplot, color coded.  #a 2Dplot, color coded. 
- +x <- c(1:20) 
-for(j in seq(0, 9)){ +y <- c(1:10) 
-  for(i in seq(0, 19)){+for(j in y){ 
 +  for(i in x){
     val <- i+j     val <- i+j
-    if((i == 0) & (j == 0)) +    if((i == 0) & (j == 0)) z <- val     
-      z <- val     +    elsez <- append(z, val)
-    } else { +
-      z <- append(z, val) +
-    }+
   }   }
 } }
-x <- c(1:20) +
-y <- c(1:10)  +
 z <- matrix(z, nrow=20, ncol=10) z <- matrix(z, nrow=20, ncol=10)
 mat <- list(x, y, z) mat <- list(x, y, z)
 image(x, y, z) image(x, y, z)
-</sxh>+</code>
  
 [{{:documents:rtips:imageplot.png| Example plotting of a matrix using base graphics function image()}}] [{{:documents:rtips:imageplot.png| Example plotting of a matrix using base graphics function image()}}]
 +
 +... a bit faster way calculation wise uses indexing. 
 +<code:R>
 +width <- 10
 +height <- 20
 +x <- c(1:width)
 +y <- c(1:height)
 +xindex <-rep(x, length = width*height)
 +yindex <- yindexing(x, y)
 +
 +z <- xindex + yindex
 +
 +z <- matrix(z, nrow=width, ncol=height)
 +mat <- list(x, y, z)
 +image(x, y, z)
 +
 +yindexing <- function (x, y) {
 +  for(i in y){
 +    cr <- rep(i, length=length(x))
 +    if (i==1) all <- cr
 +    else all <- append(all, cr)
 +  }
 +  return (all)
 +}
 +</code>
 +
 +===== Inserting math formula & symbols in label or title =====
 +
 +The easiest way is to use function //expression()//. For example,
 +<code>
 +plot(back$V2, type='l', xlab=expression(slice %.% time), ylab='intensity', xaxt="n", yaxt="n")
 +</code>
 +Inserts vertically centered dot in the x label between "slice" and "time". You could check other available expressions by
 +<code>
 +?plotmath
 +</code> 
 +
 +More recent, but under development is using [[http://cran.r-project.org/web/packages/tikzDevice/|tikzDevice]].
 +
 +===== Variable Name to String =====
 +
 +<code>
 +myfunc <- function(v1) {
 +  deparse(substitute(v1))
 +}
 +
 +myfunc(foo)
 +[1] "foo"
 +</code>
documents/111012rtricks.1329255172.txt.gz · Last modified: 2016/05/24 12:46 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki