Skip to content

Commit

Permalink
Add ggplot plotting method
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Apr 20, 2008
1 parent 361f6cf commit 0719f4f
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions R/output.r
Expand Up @@ -26,9 +26,20 @@ summary.stopwatch <- function(x, ...) {
# @keyword hplot # @keyword hplot
#X plot(glm_ex) #X plot(glm_ex)
#X plot(subset(glm_ex, level < 5)) #X plot(subset(glm_ex, level < 5))
plot.stopwatch <- function(x, ..., minlabel = 10) { plot.stopwatch <- function(x, ..., minlabel = 0.1, angle = 0) {
plot(1,1, xlim=range(x$start, x$end), ylim=range(x$level)+c(-0.5, 0.5), type="n", ..., xlab="time", ylab="level") plot(1,1, xlim=range(x$start, x$end), ylim=range(x$level)+c(-0.5, 0.5), type="n", ..., xlab="time", ylab="level")
rect(x$start, x$level - 0.5, x$end, x$level +0.5, ...) rect(x$start, x$level - 0.5, x$end, x$level +0.5, ...)
labels <- subset(x, end - start >= minlabel) labels <- subset(x, time > max(time) * minlabel)
text(labels$start, labels$level, labels$f, pos=4, srt=30, ...) if (nrow(labels) > 0)
text(labels$start, labels$level, labels$f, pos=4, angle=30, ...)
}

ggplot.stopwatch <- function(data, ..., minlabel = 0.1, angle=0) {
if (!require("ggplot2", quiet=TRUE)) stop("Please install ggplot2 to use this plotting method")

ggplot(as.data.frame(data), aes(x = factor(level))) +
geom_bar(aes(min = start, y = end, fill = source), position="identity", stat = "identity", width = 1) +
geom_text(aes(label=f, y=start), hjust=0, data=subset(data, time > max(time) * minlabel), size=5, angle=angle) +
scale_y_continuous("time") + scale_x_discrete("level") +
coord_flip()
} }

0 comments on commit 0719f4f

Please sign in to comment.