R plot filled.contour() output in ggpplot2

寵の児 提交于 2019-11-29 00:06:57

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 

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")
efrem

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!