scatterPlot: Make an ionomics scatter plot with points in order run on...

Description Usage Arguments Value Author(s) Examples

Description

Takes a vector of ionomics data, plots values colored by RSD on scatter plot, with control values as triangles. RSDs are broken into categories. <2,2-4,4-6,6-8,8-10, >10.

Usage

1
scatterPlot(scatter, rsd = NA, shape = NA, main, xlab = "Sample No.", ylab = "Concentration", runBreaks = NA)

Arguments

scatter

Vector of points for y-axis

rsd

Vector of RSD values in same order as scatter column.

shape

Column specifying whether each data point is a control or a sample point.

main

String for main title of the plot

xlab

String for the plot x label.

ylab

String for the plot y label.

runBreaks

Vector of indices to drow vertical lines. Usually test tube or run breaks.

Value

Returns a ggplot object.

Author(s)

Greg Ziegler

Examples

 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
library(ggplot2)
set.seed(1)
x <- rnorm(100)
y <- rnorm(100)
x <- c(-10, x, 10)
y <- c(-20, y, 20)
df <- data.frame(id=sample(LETTERS[1:5],length(x),replace=TRUE),x,y)
df$type <- sapply(1:nrow(df),function(x){ifelse(x%%10,"Sample","Control")})
scatterPlot(scatter=df$y,rsd=abs(df$x*10),shape=df$type,main="Test data",ylab="CPS",runBreaks=NA)

## The function is currently defined as
function (scatter, rsd = NA, shape = NA, main, xlab = "Sample No.", 
    ylab = "Concentration", runBreaks = NA) 
{
    suppressPackageStartupMessages(require(ggplot2))
    suppressPackageStartupMessages(require(grid))
    data <- data.frame(scatter = scatter, rsd = rsd, shape = shape)
    smooth <- stats::lowess(na.omit(data[, 1]), f = 0.1)$y
    if (any(is.na(data$scatter))) {
        for (na.val in which(is.na(data$scatter))) {
            smooth <- append(smooth, NA, after = na.val - 1)
        }
    }
    data$smooth <- smooth
    if (!sum(is.na(data$rsd)) == length(data$rsd)) {
        data$colorCol <- cut(as.numeric(data$rsd), breaks = c(2 * 
            (0:5), Inf), labels = c("<2", "2-4", "4-6", "6-8", 
            "8-10", ">10"), include.lowest = TRUE)
    }
    data$x <- 1:nrow(data)
    p1 <- ggplot(data = data, aes(x = x, y = scatter))
    if (!sum(is.na(data$rsd)) == length(data$rsd) & !sum(is.na(data$shape)) == 
        length(data$shape)) {
        p1 <- p1 + geom_point(aes(colour = colorCol, shape = shape))
        p1 <- p1 + scale_colour_manual(values = colorRampPalette(c("#00007F", 
            "blue", "#007FFF", "cyan", "#7FFF7F", "yellow", "#FF7F00", 
            "red", "#7F0000"))(7), name = "RSD")
        p1 <- p1 + scale_shape_manual(name = "Type", values = c(17, 
            16))
    }
    else if (!sum(is.na(data$rsd)) == length(data$rsd)) {
        p1 <- p1 + geom_point(aes(colour = colorCol))
        p1 <- p1 + scale_colour_manual(values = colorRampPalette(c("#00007F", 
            "blue", "#007FFF", "cyan", "#7FFF7F", "yellow", "#FF7F00", 
            "red", "#7F0000"))(7), name = "RSD")
    }
    else if (!sum(is.na(data$shape)) == length(data$shape)) {
        p1 <- p1 + geom_point(aes(shape = shape))
        p1 <- p1 + scale_shape_manual(name = "Type", values = c(17, 
            16))
    }
    else {
        p1 <- p1 + geom_point()
    }
    if (nrow(data) > 50) {
        p1 <- p1 + geom_line(aes(y = smooth), size = 1.3, colour = "blue")
    }
    p1 <- p1 + labs(x = xlab, y = ylab, title = main)
    p1 <- p1 + theme(legend.background = element_rect(), legend.position = "bottom", 
        legend.box = "horizontal", legend.direction = "horizontal")
    p1 <- p1 + scale_x_continuous(limits = c(0, nrow(data)), 
        expand = c(0, 0))
    if (!is.na(runBreaks)) {
        p1 <- p1 + geom_vline(xintercept = runBreaks)
    }
    p1 <- p1 + geom_hline(yintercept = mean(data[, 1], na.rm = TRUE), 
        colour = "red", size = 1)
    return(p1)
  }

gziegler/ionomicsUtils documentation built on June 20, 2019, 8:04 p.m.