在等高线图中绘制一个点 ggplot2

发布于 2024-09-27 21:28:53 字数 424 浏览 3 评论 0原文

我在 ggplot2 中有一个等高线图,我想将一个点映射到它。

我的等高线图如下所示:

v = ggplot(pts, aes(theta_1, theta_2, z = z))
v + stat_contour(aes(colour = ..level..),bins=50) 
+ xlab(expression(Theta[1])) + ylab(expression(Theta[2]))

我有一个点如下所示:

p = ggplot(ts,aes(x,y))
p + geom_point() 

不幸的是,第二个覆盖了第一个。

有没有办法让它们出现在同一个图上,类似于 MATLAB 的“hold on;”?

谢谢!

I have a contour plot in ggplot2 that I want to map one point to.

My contour plot looks like this:

v = ggplot(pts, aes(theta_1, theta_2, z = z))
v + stat_contour(aes(colour = ..level..),bins=50) 
+ xlab(expression(Theta[1])) + ylab(expression(Theta[2]))

and I have a point that looks like this:

p = ggplot(ts,aes(x,y))
p + geom_point() 

unfortunately the second overwrites the first.

Is there a way to get them to show up on the same plot, similar to MATLAB's "hold on;"?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

找回味觉 2024-10-04 21:28:53

您可以直接向 geom_point() 提供点:

set.seed(1000)
x = rnorm(1000)
g = ggplot(as.data.frame(x), aes(x = x))
g + stat_bin() + geom_point(data = data.frame(x = -1, y = 40), aes(x=x,y=y))

alt text

You can provide the points directly to geom_point():

set.seed(1000)
x = rnorm(1000)
g = ggplot(as.data.frame(x), aes(x = x))
g + stat_bin() + geom_point(data = data.frame(x = -1, y = 40), aes(x=x,y=y))

alt text

半世晨晓 2024-10-04 21:28:53

不确定这是否仍然令人感兴趣,但我认为您只需要保存更新的 v 对象,然后将点添加到该对象,而不是创建一个新的 ggplot2 对象。例如,

v <- ggplot(pts, aes(theta_1, theta_2, z = z))
v <- v + stat_contour(aes(colour = ..level..),bins=50) 
+ xlab(expression(Theta[1])) + ylab(expression(Theta[2]))
v <- v + geom_point(aes(x=ts$x, y=ts$y))
v # to display

ggplot2 非常擅长增量添加图层,并非所有图层都必须基于第一个 ggplot 调用中指定的相同数据集。

Not sure if this is still of interest, but I think you just needed to save the updated v object then add the point to that, rather than create a new ggplot2 object. For example

v <- ggplot(pts, aes(theta_1, theta_2, z = z))
v <- v + stat_contour(aes(colour = ..level..),bins=50) 
+ xlab(expression(Theta[1])) + ylab(expression(Theta[2]))
v <- v + geom_point(aes(x=ts$x, y=ts$y))
v # to display

ggplot2 is very good at adding layers incrementally, not all have to be based on the same dataset specified in the first ggplot call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文