gpt4 book ai didi

r - 用 direct.labels 标记两个轮廓

转载 作者:行者123 更新时间:2023-12-02 01:20:33 29 4
gpt4 key购买 nike

我正在尝试比较两个标量场,并希望使用等值线将它们绘制在同一个图中,并使用 directlabels 标记它们的值。问题是,我无法在同一个图中使用两个直接标签。

例子:

library(ggplot2)
library(data.table)
library(directlabels)
grid <- expand.grid(lon = seq(0, 360, by = 2), lat = seq(-90, 0, by = 2))
grid$z <- with(grid, cos(lat*pi/180))
grid$z2 <- with(grid, sin(lat*pi/180))
grid.long <- melt(grid, id.vars = c("lon", "lat"))

# Manually adding two geom_dl's
ggplot(grid, aes(lon, lat)) +
geom_contour(aes(z = z), color = "black") +
geom_contour(aes(z = z2), color = "red") +
geom_dl(aes(z = z2, label = ..level..), stat = "contour", method = "top.pieces", color = "red") +
geom_dl(aes(z = z, label = ..level..), stat = "contour", method = "top.pieces", color = "black")

只有一个变量被标记。

另一种方式:

ggplot(grid.long, aes(lon, lat)) +
geom_contour(aes(z = value, color = variable)) +
geom_dl(aes(z = value, label = ..level.., color = variable),
stat = "contour", method = "top.pieces")

有什么解决办法吗?

谢谢!

最佳答案

一种解决方案是为第二次 geom_dl() 调用提供不同的 method= 参数。

ggplot(grid, aes(lon, lat)) +
geom_contour(aes(z = z), color = "black") +
geom_contour(aes(z = z2), color = "red") +
geom_dl(aes(z = z2, label = ..level..), stat = "contour", method = "top.pieces", color = "red") +
geom_dl(aes(z = z, label = ..level..), stat = "contour", method = "bottom.pieces", color = "black")

enter image description here

关于r - 用 direct.labels 标记两个轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40498220/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com