-
Notifications
You must be signed in to change notification settings - Fork 0
/
ezplot.R
86 lines (77 loc) · 2.08 KB
/
ezplot.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# author: zh(mt1022)
# color pallete
library(RColorBrewer)
brewer.pal.set1 <- brewer.pal(9, name = 'Set1')
# graphic parameter for publication
op <- par(las = 1, family = 'Liberation Sans')
# plot multiple ecdf in a plot
PlotEcdf <-function(ll, line.col = 1:length(ll), axes = T, ...){
plot(ecdf(ll[[1]]), col = line.col[1], axes = axes, ...)
for(i in 2:length(ll)){
plot(ecdf(ll[[i]]), col = line.col[i], add = T, ...)
}
}
# general plot
Plot <- function(x, y = NULL, ...){
plot(x, y, main = NA, xlab = NA, ylab = NA, axes = F, lwd = 4, cex = 2, ...)
axis(1, lwd = 4, cex.axis = 2)
axis(1, lwd = 4, cex.axis = 2)
}
# plot reversed ecdf
# example:
# x <- 1:10
# y = rcdf(x)
# plot(y[, 1], y[, 2], type = 's')
Rcdf <- function(x){
n <- length(x)
x.min <- min(x)
x.max <- max(x)
y <- sapply(0:n/n * (x.max - x.min) + x.min, function(i) sum(x >= i)/n)
res <- data.frame(x1 = 0:n/n * (x.max - x.min) + x.min, x2 = y)
return(res)
}
# given vector of p.value, return significance code
SigCode <- function(x){
res <- sapply(x, function(i){
if(i < 0.001){
return('***')
}else if(i < 0.01){
return('**')
}else if(i < 0.05){
return('*')
}else{
return('')
}
})
return(res)
}
SigCode.2 <- function(x){
res <- sapply(x, function(i){
if(i < 0.001){
return('***')
}else if(i < 0.01){
return('**')
}else if(i < 0.05){
return('')
}else{
return('')
}
})
return(res)
}
# given relative coordiante, return actual coordiante;
RelPos <- function(x, y, r = par('usr')){
res <- c(
x = r[1] + (r[2] - r[1]) * x,
y = r[3] + (r[4] - r[3]) * y
)
}
# return plot expression in scientific format for p.value
# @param x input p.value
# @return res expression
SciExp <- function(x, prefix = 'P = '){
sci <- strsplit(sprintf('%.2e', x), 'e')[[1]]
sci.text <- paste("'", prefix, sci[1], "x10'^", as.integer(sci[2]), sep = '')
res <- parse(text = sci.text)
return(res)
}