Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stat_contour with data labels on lines

Tags:

r

ggplot2

I wonder how to get data labels on lines in ggplot2 for contours. Thanks

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()
like image 952
MYaseen208 Avatar asked Nov 29 '12 14:11

MYaseen208


1 Answers

using directlabels package and picking solution from 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")

enter image description here

like image 178
agstudy Avatar answered Sep 22 '22 04:09

agstudy