r - draw line across in a ggplot2 -
i've got plot i'd draw line (0,0) (15,15), legend. how achieve that? plot:
frame <- read.table('pastie_from_web', sep=",", header=true) colnames(frame) <- c("pos", "word.length") plot <- ggplot(frame, aes(x=pos, y=word.length)) + scale_x_continuous(limits=c(1,15)) + scale_y_continuous(limits=c(1,15))+ geom_density2d(aes(color=..level..)) + scale_color_gradient(low="black", high="red") + opts(legend.position="none") png(paste("graphs/", fname, ".png", sep=""), width=600, height=600) print(plot) data: http://sprunge.us/gkil or
structure(list(position = c(2, 2, 2, 2, 7, 8, 4, 5, 4, 9, 5, 2, 7, 9, 9, 6, 5, 6, 9, 2, 6, 5, 5, 7, 7, 5, 6, 5, 5, 3, 2, 4, 5, 2, 3, 2, 7, 5, 2, 5, 2, 6, 8, 7, 2, 8, 5, 4, 2, 5, 2, 2, 2, 6, 8, 2, 2, 9, 5, 2, 4, 7, 3, 4, 9, 5, 5, 5, 5, 4, 7, 2, 7, 2, 4, 4, 3, 2, 5, 6, 5, 5, 5, 5, 4, 4, 8, 7, 5, 7, 4, 3, 4, 5, 2, 6, 6, 4, 4, 2, 2, 3, 2, 2, 6, 2), word.length = c(5l, 5l, 6l, 4l, 9l, 11l, 5l, 8l, 8l, 10l, 8l, 9l, 8l, 10l, 10l, 7l, 9l, 10l, 11l, 10l, 10l, 8l, 13l, 11l, 11l, 13l, 7l, 9l, 6l, 4l, 9l, 8l, 9l, 6l, 4l, 5l, 11l, 13l, 13l, 13l, 10l, 9l, 11l, 8l, 4l, 10l, 8l, 16l, 3l, 5l, 4l, 12l, 12l, 15l, 9l, 12l, 12l, 11l, 11l, 8l, 16l, 9l, 8l, 7l, 10l, 11l, 6l, 13l, 5l, 8l, 8l, 5l, 8l, 5l, 6l, 6l, 7l, 10l, 13l, 7l, 6l, 13l, 9l, 6l, 7l, 8l, 11l, 8l, 8l, 8l, 8l, 8l, 7l, 6l, 5l, 9l, 9l, 5l, 5l, 6l, 7l, 8l, 8l, 10l, 8l, 10l)), .names = c("position", "word.length"), class = "data.frame", row.names = c(na, -106l))
here example data set illustrate:
set.seed(42) dat <- data.frame(x = runif(20, min = 0, max = 20), y = runif(20, min = 0, max = 20)) p <- ggplot(dat, aes(x = x, y = y)) p + geom_point() + geom_line(data = data.frame(x = c(0,15), y = c(0,15)), aes = aes(x = x, y = y), colour = "red") notice how can specify different data argument geoms, allows plot different data objects on same plot regions defined in original ggplot() call. note: if second data frame (in geom_line() call) has same x , y axis mapping original plot, don't need new aes() had code (see revision history of answer). may not have been clear , comment @justin has prompted me change geom_line() include new aes() call map data aesthetics; not needed in example may needed in real world use.
the above gives:

if want different arbitrary lines, consider geom_abline() draw lines give slope , intercept. geom_segment() alternative above geom_line() specify start , end x , y coordinates. see respective pages geoms determine prefer use of.
Comments
Post a Comment