Examples for 'ggplot2::geom_contour'


2D contours of a 3D surface

Aliases: geom_contour geom_contour_filled stat_contour stat_contour_filled

Keywords:

### ** Examples

# Basic plot
v <- ggplot(faithfuld, aes(waiting, eruptions, z = density))
v + geom_contour()
plot of chunk example-ggplot2-geom_contour-1
# Or compute from raw data
ggplot(faithful, aes(waiting, eruptions)) +
  geom_density_2d()
plot of chunk example-ggplot2-geom_contour-1
## No test: 
# use geom_contour_filled() for filled contours
v + geom_contour_filled()
plot of chunk example-ggplot2-geom_contour-1
# Setting bins creates evenly spaced contours in the range of the data
v + geom_contour(bins = 3)
plot of chunk example-ggplot2-geom_contour-1
v + geom_contour(bins = 5)
plot of chunk example-ggplot2-geom_contour-1
# Setting binwidth does the same thing, parameterised by the distance
# between contours
v + geom_contour(binwidth = 0.01)
plot of chunk example-ggplot2-geom_contour-1
v + geom_contour(binwidth = 0.001)
plot of chunk example-ggplot2-geom_contour-1
# Other parameters
v + geom_contour(aes(colour = after_stat(level)))
plot of chunk example-ggplot2-geom_contour-1
v + geom_contour(colour = "red")
plot of chunk example-ggplot2-geom_contour-1
v + geom_raster(aes(fill = density)) +
  geom_contour(colour = "white")
plot of chunk example-ggplot2-geom_contour-1
# 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")
Loading required namespace: interp
Irregular data requires the 'interp' package
## End(No test)

[Package ggplot2 version 3.3.6 Index]