Contour levels corresponding to variable levels in ggplot2

最后都变了- 提交于 2019-12-05 02:02:00

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()

Try factorizing the fill in stat_density2d()

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

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