如何用ggplot2将点和线叠加到轮廓图上? [英] How can I overlay points and lines onto a contour plot with ggplot2?

查看:255
本文介绍了如何用ggplot2将点和线叠加到轮廓图上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用我想突出显示的特定点(这些点存储在不同的数据集中)标注等值线图。当我尝试时,出现错误:
$ b


错误:美学必须是长度为1或与dataProblems的长度相同:z

但是,当我尝试制作一个可重复的示例时,出现了另一个错误:


eval(expr,envir,enclos)中的错误:找不到对象'z'

可重现的例子的代码如下:

  library(mnormt)
library(dplyr)
库(ggplot2)

f< - 函数(x,y){
dmnorm(x = c(x,y),
mean = c(0,0),
varcov = diag(2))
}
f < - Vectorize(f)


xmesh < - seq(from = -3, to = 3,length.out = 100)
ymesh < - seq(from = -3,to = 3,length.out = 100)
dummy< - expand.grid(x = xmesh ,y = ymesh)
dummy $ z <-f(dummy $ x,dummy $ y)

stuff< - data_frame(x = c(0,0,1),
y = c(0,-1,-1),
point = c(O,P,Q))

dummy%>%
ggplot (aes(x = x,y = y,z = z))+
stat_contour(aes(color = ..level ..))+
labs(color =density)+
geom_point(data = stuff,mapping = aes(x = x,y = y,color = point))


ggplot 调用传递给geoms的其余部分。所以这个错误告诉你它不能找到z内部的东西,它仍然认为z应该是从最初的调用z.

有很多种方法为了解决这个问题,我认为解决这个问题的最简单方法是分别给每个geom数据:

  ggplot()+ 
stat_contour(data = dummy,aes(x = x,y = y,z = z,color = ..level ..))+
labs(color =density)+
geom_point(data = stuff,aes(x = x,y = y,fill = factor(point)),pch = 21)



注意。您还有一个问题,即颜色无法映射到两个不同的几何元素,所以我使用pch和fill填充了它。

I want to annotate a contour plot with particular points that I want to highlight (where these points are stored in a different data set). When I try, I get an error:

Error: Aesthetics must either be length one, or the same length as the dataProblems:z

However, when I tried to make a reproducible example, I get a different error:

Error in eval(expr, envir, enclos) : object 'z' not found

The code for the reproducible example is below:

library(mnormt)
library(dplyr)
library(ggplot2)

f <- function(x, y) {
    dmnorm(x = c(x, y),
            mean = c(0, 0),
            varcov = diag(2))
}
f <- Vectorize(f)


xmesh <- seq(from = -3, to = 3, length.out = 100)
ymesh <- seq(from = -3, to = 3, length.out = 100)
dummy <- expand.grid(x = xmesh, y = ymesh)
dummy$z <- f(dummy$x, dummy$y)

stuff <- data_frame(x = c(0, 0, 1),
                    y = c(0, -1, -1),
                    point = c("O", "P", "Q"))

dummy %>%
    ggplot(aes(x = x, y = y, z = z)) +
    stat_contour(aes(color = ..level..)) +
    labs(color = "density") + 
    geom_point(data = stuff, mapping = aes(x = x, y = y, color = point))

解决方案

ggplot passes the aes from the first ggplot call to the rest of the geoms, unless told otherwise. So the error is telling you that it cannot find z inside stuff, and it still thinks that the z should be z from the initial call.

There are a range of ways to fix this, I think the easiest way to fix it is to give each geom its data separately:

ggplot() +
  stat_contour(data = dummy, aes(x = x, y = y, z = z, color = ..level..)) +
  labs(color = "density") + 
  geom_point(data = stuff, aes(x = x, y = y, fill = factor(point)), pch = 21)

NB. you also have a problem where colour cannot be mapped in two different geoms, so I've fixed it using pch and fill.

这篇关于如何用ggplot2将点和线叠加到轮廓图上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆