如何使用ggplot2在等高线图中添加标签?

时间:2019-05-03 19:21:59

标签: r ggplot2 label contour

我正在用ggplot2做一个等高线图,并且试图添加像this tutorial这样的标签。但是,始终会出现以下错误:

Error in geom_text_contour(aes(z = z)) : could not find function "geom_text_contour"

library(ggplot2)

x <- c(0,0,0,1,1,1,2,2,2,3,3,3)
y <- c(0,1,2,0,1,2,0,1,2,0,1,2)
z <- seq(0,11,1)

df <- data.frame(x,y,z)

ggplot(df, aes(x, y, z = z)) +
  geom_tile(aes(fill = z)) +
  geom_contour(colour = "white") +
  geom_text_contour(aes(z = z))

enter image description here

1 个答案:

答案 0 :(得分:0)

Daniel,只需要安装和加载metR软件包。

library(ggplot2)
library(metR)
v <- data.table::melt(volcano)
g <- ggplot(v, aes(Var1, Var2)) +
  ggplot2::geom_contour(aes(z = value))
g + metR::geom_text_contour(aes(z = value)) + ggplot2::ggtitle("03/05/19 - Daniel")

enter image description here