Friday, May 2, 2014

Adding new points into an existing contour plot in ggplot2

A demonstration, first with the legend, then without:

A demonstration, first with the legend, then without (R code)
Double click to toggle code
require(ggplot2)
library(reshape2) # for melt
volcano3d <- melt(volcano)
names(volcano3d) <- c("x", "y", "z")
v <- ggplot(volcano3d, aes(x, y, z = z))
v = v + stat_contour()
print(v)
newdata = data.frame(x = runif(7)*60, y = runif(7)*60, size=nrow(newdata):1)
v = v + geom_point(data=newdata, aes(x, y, z=1, size=size), alpha=0.3, color="blue")
v = v + geom_path(data=newdata, aes(x, y, z=1), color="blue", alpha=0.5)
print(v)
v = v + opts(legend.position = "none")
print(v)

0 comments: