19

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

2 回答 2

29

使用 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")

在此处输入图像描述

于 2012-11-29T14:39:56.630 回答
2

这是一个已经回答的老问题,但我做了很多等高线图,我认为使用包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()
于 2019-05-24T16:01:41.713 回答