I've got a plot where I'd like to draw a line from (0,0)
to
(15,15)
, with legend. How do I 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))
Example: To add the horizontal line on the plot, we simply add geom_hline() function to ggplot2() function and pass the yintercept, which basically has a location on the Y axis, where we actually want to create a vertical line.
The R function abline() can be used to add vertical, horizontal or regression lines to a graph. A simplified format of the abline() function is : abline(a=NULL, b=NULL, h=NULL, v=NULL, ...)
abline() function in R Language is used to add one or more straight lines to a graph. The abline() function can be used to add vertical, horizontal or regression lines to plot. Syntax: abline(a=NULL, b=NULL, h=NULL, v=NULL, …)
To create a vertical line using ggplot2, we can use geom_vline function of ggplot2 package and if we want to have a wide vertical line with different color then lwd and colour argument will be used. The lwd argument will increase the width of the line and obviously colour argument will change the color.
Here is an example data set to 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 we can specify different data
argument to geoms, which allows us to plot different data objects on the same plot regions defined in the original ggplot()
call. Note: If the second data frame (in the geom_line()
call) has the same x and y axis mapping as the original plot, then you don't need a new aes()
as I originally had the code (see revision history of the Answer). This may not have been clear and the comment from @Justin has prompted me to change the geom_line()
to include a new aes()
call to map the data to the aesthetics; it is not needed in my example but may well be needed in real world use.
The above gives:
If you want different arbitrary lines, consider geom_abline()
to draw lines with give slope and intercept. geom_segment()
is an alternative to the above geom_line()
where you specify the start and end x and y coordinates. See the respective help pages for the geoms to determine which you prefer the use of.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With