用 direct.labels 标记两个轮廓
Labeling two contours with direct.labels
我正在尝试比较两个标量场,并希望使用等值线将它们绘制在同一个图中,并使用 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")
我正在尝试比较两个标量场,并希望使用等值线将它们绘制在同一个图中,并使用 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")