我想知道如何在ggplot2中的轮廓线上获取数据标签。谢谢

require(grDevices) # for colours

x <- seq(-4*pi, 4*pi, len = 27)
y <- seq(-4*pi, 4*pi, len = 27)
r <- sqrt(outer(x^2, y^2, "+"))

rx <- range(x <- 10*1:nrow(volcano))
ry <- range(y <- 10*1:ncol(volcano))
ry <- ry + c(-1, 1) * (diff(rx) - diff(ry))/2

plot(
    x = 0
  , y = 0
  , type = "n"
  , xlim = rx
  , ylim = ry
  , xlab = ""
  , ylab = ""
  )

contour(
    x = x
  , y = y
  , z = volcano
  , add = TRUE
  )

library(ggplot2)
library(reshape2)
volcano3d <- melt(volcano)
names(volcano3d) <- c("x", "y", "z")

# Basic plot
v <- ggplot(volcano3d, aes(x, y, z = z))
v + stat_contour()

最佳答案

使用directlabels程序包并从this中选择解决方案

# Basic plot
v <- ggplot(volcano3d, aes(x, y, z = z))
library(directlabels)
v2 <- v + stat_contour(aes(colour = ..level..))
direct.label(v2, method="bottom.pieces")

关于r - 带有数据标签的stat_contour,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13627735/

10-09 13:11