Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

create rotated axis labels

The R FAQ states:

par(mar = c(7, 4, 4, 2) + 0.1)
plot(1 : 30, xaxt = "n",  xlab = "")
labels <- paste("Label", 1:30, sep = " ")
text(1:30, par("usr")[3] - 0.25, srt = 90, adj = 1,labels = labels, xpd = TRUE)
mtext(1, text = "X Axis Label", line = 6)


Also see Figure 1 and associated code in Paul Murrell (2003), “Integrating grid Graphics Output with Base Graphics Output�, R News, 3/2, 7–12.

sort a data.frame by a given column

data <- sort.data.frame(data, key = "LOC")


where the function "sort.data.frame" is as follows:

sort.data.frame <- function(x, key, ...) {
    if (missing(key)) {
        rn <- rownames(x)
        if (all(rn %in% 1:nrow(x))) rn <- as.numeric(rn)
        x[order(rn, ...), , drop=FALSE]
    } else {
        x[do.call("order", c(x[key], ...)), , drop=FALSE]
    }
}

barplot the lines of code by module

The CSV file "loc.csv" contains the following:

Module LOC
a.rb 100
b.rb 120
c.rb 54


The following R code creates the barplot image "loc.png":

data <- read.csv(file="loc.csv", sep = " ", header = TRUE, row.names = "Module")
png("loc.png", width = 640, height = 480)
barplot(data$LOC, names = rownames(data), ylim = c(0, 250), main = "Lines of code by module", ylab = "Lines of code", xlab = "Module")
dev.off()
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS