I want to plot this figure created with filled.contour(), but in ggplot2, how do I do this?
I want to use ggplot2 because the graphing conventions are easier. The r
You can tweak the colors as you need:
gdat <- interp2xyz(fld, data.frame=TRUE)
ggplot(gdat) +
aes(x = x, y = y, z = z, fill = z) +
geom_tile() +
coord_equal() +
geom_contour(color = "white", alpha = 0.5) +
scale_fill_distiller(palette="Spectral", na.value="white") +
theme_bw()
You can reduce the pixelation at the cost of some processing time by increasing the density of the interpolation:
fld <- with(df, interp(x = longitude,
y = latitude,
z = d13C,
xo = seq(min(longitude), max(longitude), length=400),
duplicate="mean"))
and also reducing the bin width:
ggplot(gdat) +
aes(x = x, y = y, z = z) +
geom_tile(aes(fill=z)) +
coord_equal() +
stat_contour(aes(fill=..level..), geom="polygon", binwidth=0.005) +
geom_contour(color="white", alpha=0.5) +
scale_fill_distiller(palette="Spectral", na.value="white") +
theme_bw()
NOTE: that is going to crunch for a noticeable few seconds on a decent desktop system. On my fairly beefy MacBook Pro it was:
user system elapsed
6.931 0.655 8.153
I took the example from the ggplot2 website.
# Generate data
library(reshape2) # for melt
volcano3d <- melt(volcano)
names(volcano3d) <- c("x", "y", "z")
# Basic plot
v <- ggplot(volcano3d, aes(x, y, z = z)) +
stat_contour(geom="polygon", aes(fill=..level..))
Where x and y are your Long and Lat and z is d13C
To follow up on @hrbrmstr's minimal example, you can also have ggplot2 compute "z" for you:
library(ggplot2)
ggplot(data = faithful, aes(x = eruptions, y = waiting)) +
stat_density2d(aes(colour = ..level.., fill = ..level..), geom = "polygon")