首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >filled.contour与ggplot2 + stat_contour

filled.contour与ggplot2 + stat_contour
EN

Stack Overflow用户
提问于 2014-09-11 21:27:41
回答 1查看 3.8K关注 0票数 8

我是ggplot2的新手,我正在尝试复制我使用filled.contour和ggplot2创建的图形。

下面是我的代码:

代码语言:javascript
复制
require(ggplot2)
require(reshape2)

#data prep
scale <- 10

xs <- scale * c(0, 0.5, 0.8, 0.9, 0.95, 0.99, 1)
ys <- scale * c(0, 0.01, 0.05, 0.1, 0.2, 0.5, 1)

df <- data.frame(expand.grid(xs,ys))
colnames(df) <- c('x','y')
df$z <- ((scale-df$x) * df$y) / ((scale-df$x) * df$y + 1)

#filled contour looks good
filled.contour(xs, ys, acast(df, x~y, value.var='z'))

#ggplot contour looks bad
p <- ggplot(df, aes(x=x, y=y, z=z))

p + stat_contour(geom='polygon', aes(fill=..level..))

我想不出如何让ggplot等值线填充一直到左上角的多边形(在z=为0.99的(0,10)处有一个点)我得到的是这些奇怪的三角形

EN

回答 1

Stack Overflow用户

发布于 2014-12-14 15:10:02

要创建ggplot版本的filled.contour图,您需要有一个比示例中的df对象更大的data.frame,使用geom_tile将生成您正在寻找的图。请考虑以下几点:

代码语言:javascript
复制
# a larger data set
scl <- 10
dat <- expand.grid(x = scl * seq(0, 1, by = 0.01), 
                   y = scl * seq(0, 1, by = 0.01))
dat$z <- ((scl - dat$x) * dat$y) / ((scl - dat$x) * dat$y + 1)

# create the plot, the geom_contour may not be needed, but I find it helpful
ggplot(dat) + 
aes(x = x, y = y, z = z, fill = z) + 
geom_tile() + 
geom_contour(color = "white", alpha = 0.5) + 
scale_fill_gradient(low = "lightblue", high = "magenta") + 
theme_bw()

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25788727

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档