2012-11-29 14 views
13

我想知道如何获得轮廓线上的数据标签ggplot2。由于stat_contour与行上的数据标签

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() 

回答

20

使用directlabels包,并从this

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

enter image description here

+6

采摘的解决方案与ggplot> 2.0.0你需要添加'方法= “bottom.pieces”'(或'top.pieces')到'direct.label'调用 – jaimedash