# Basic plot
v <- ggplot(faithfuld, aes(waiting, eruptions, z = density))
v + geom_contour()
# Or compute from raw data
ggplot(faithful, aes(waiting, eruptions)) +
geom_density_2d()
# \donttest{
# use geom_contour_filled() for filled contours
v + geom_contour_filled()
# Setting bins creates evenly spaced contours in the range of the data
v + geom_contour(bins = 3)
v + geom_contour(bins = 5)
# Setting binwidth does the same thing, parameterised by the distance
# between contours
v + geom_contour(binwidth = 0.01)
v + geom_contour(binwidth = 0.001)
# Other parameters
v + geom_contour(aes(colour = after_stat(level)))
v + geom_contour(colour = "red")
v + geom_raster(aes(fill = density)) +
geom_contour(colour = "white")
# Irregular data
if (requireNamespace("interp")) {
# Use a dataset from the interp package
data(franke, package = "interp")
origdata <- as.data.frame(interp::franke.data(1, 1, franke))
grid <- with(origdata, interp::interp(x, y, z))
griddf <- subset(data.frame(x = rep(grid$x, nrow(grid$z)),
y = rep(grid$y, each = ncol(grid$z)),
z = as.numeric(grid$z)),
!is.na(z))
ggplot(griddf, aes(x, y, z = z)) +
geom_contour_filled() +
geom_point(data = origdata)
} else
message("Irregular data requires the 'interp' package")
# }
Run the code above in your browser using DataLab