stat_contour,行上有数据标签

MYa*_*208 18 r ggplot2

我想知道如何在线上获取数据标签以获得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()
Run Code Online (Sandbox Code Playgroud)

ags*_*udy 26

使用directlabels包和从采摘溶液

# 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")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 使用ggplot> 2.0.0,您需要将`method ="bottom.pieces"`(或`top.pieces`)添加到`direct.label`调用中 (6认同)

San*_*ado 6

这是一个已经回答的老问题,但我做了很多等高线图,我认为使用包 metR 有一种更简单、更通用的方法来做到这一点(https://rdrr.io/github/eliocamp/metR/ f/vignettes/Visualization-tools.Rmd)。该包具有函数 geom_label_contour() ,它提供了一种绘制等高线标签的简单方法。还提供了很多绘制地图的功能。

library(ggplot2)
library(reshape2)
library(metR)
volcano3d <- melt(volcano)
colnames(volcano3d) <- c('x','y','z')

ggplot(data = volcano3d, aes(x=x,y=y,z=z)) + geom_contour() +
  geom_label_contour()
Run Code Online (Sandbox Code Playgroud)