Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Contour levels corresponding to variable levels in ggplot2

Tags:

r

ggplot2

contour

I am trying to draw contour plot with ggplot2 and it is proving to be a little harder than I imagined. Using the iris dataset I am able to produce this plot:

ggplot(iris, aes(x=Petal.Width, y=Petal.Length, fill=Sepal.Width)) +
  stat_density2d(geom="polygon", aes(fill=..level..))

enter image description here

My issue is that I can't seem to figure out how to display- rather than the density values -the raw Sepal.Width values. Here is what I've tried:

ggplot(iris, aes(x=Petal.Width, y=Petal.Length, z=Sepal.Width)) +
  geom_tile(aes(fill=Sepal.Width))+
  stat_contour(aes(colour=..level..)) 

This produces an especially odd error message:

 Warning message:
 Computation failed in `stat_contour()`:
 (list) object cannot be coerced to type 'double' 

I also tried this:

ggplot(iris, aes(x=Petal.Width, y=Petal.Length, fill=Sepal.Width)) +
  stat_density2d(geom="polygon", aes(fill=Sepal.Width))

And lastly this:

ggplot(iris, aes(x=Petal.Width, y=Petal.Length, fill=Sepal.Width)) +
  geom_tile()

Can anyone recommend a good way to produce a contour plot in ggplot2 with the values of the variable itself producing the levels of the contour?

UPDATED

From the stat_contour example:

# Generate data
library(reshape2) # for melt
volcano3d <- melt(volcano)
names(volcano3d) <- c("x", "y", "z")

# Basic plot
ggplot(volcano3d, aes(x, y, z = z)) +
 stat_contour(geom="polygon", aes(fill=..level..))

Work great and looks great. But if I apply this exactly to the iris example like so:

ggplot(iris, aes(x=Petal.Width, y=Petal.Length, fill=Sepal.Width)) +
  stat_contour(geom="polygon", aes(fill=..level..))

I get this error message:

Warning message:
Computation failed in `stat_contour()`:
(list) object cannot be coerced to type 'double'

These are both dataframes with similar structure so I can't figure out what is different between the two causing this issue.

like image 869
boshek Avatar asked Feb 24 '16 05:02

boshek


People also ask

How do you do a contour plot in ggplot2?

The geom_density_2d function A 2D density contour plot can be created in ggplot2 with geom_density_2d . You just need to pass your data frame and indicate the x and y variable inside aes . It is possible to increase or decrease the number of levels with bins .

What is levels in contour plot?

levels: Determines the number and positions of the contour lines / regions.

What is contour variable?

Contour plots (sometimes called Level Plots) are a way to show a three-dimensional surface on a two-dimensional plane. It graphs two predictor variables X Y on the y-axis and a response variable Z as contours. These contours are sometimes called z-slices or iso-response values.

What does contour function do in R?

The function cf is a quick function that can take in grid data, a function, or any data, and give a contour plot showing the function or data. By default, plots are made using base graphics, but they can also be done using ggplot2.


1 Answers

The ultimate solution to this way to use the akima package for interpolation then the ggplot2 for final plotting. This is the method I used:

library(ggplot2)
library(akima)
library(dplyr)

interpdf <-interp2xyz(interp(x=iris$Petal.Width, y=iris$Petal.Length, z=iris$Sepal.Width, duplicate="mean"), data.frame=TRUE)

interpdf %>%
  filter(!is.na(z)) %>%
  tbl_df() %>%
  ggplot(aes(x = x, y = y, z = z, fill = z)) + 
  geom_tile() + 
  geom_contour(color = "white", alpha = 0.05) + 
  scale_fill_distiller(palette="Spectral", na.value="white") + 
  theme_bw()

enter image description here

like image 91
boshek Avatar answered Sep 28 '22 05:09

boshek