简体   繁体   English

使用带有ggplot2的直接标签来标记具有与主层不同的数据的第二层

[英]Using directlabels with ggplot2 to label a second layer with data different from the primary layer

I'm using ggplot2 to make contour plots. 我正在使用ggplot2制作等高线图。 I'm trying to make a contour plot with split data because I have data from different time periods for different regions of the plot. 我正在尝试用分割数据制作等高线图,因为我有不同时间段的数据用于绘图的不同区域。 I'd like to show them on the same plot, but not have the contours line up. 我想在同一个地块上展示它们,但没有轮廓排列。

I can produce the contours I want simply enough; 我可以简单地生成我想要的轮廓; here's a similar example: 这是一个类似的例子:

library(ggplot2)
library(directlabels)
data(volcano)

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

#Splitting the data into 2 halves
volcano3d_1<-volcano3d[volcano3d$x<50,]
volcano3d_2<-volcano3d[volcano3d$x>=50,]

#Changing the z values of the 2nd half
volcano3d_2$z<-volcano3d_2$z+30

#Plotting
v <- ggplot(volcano3d_1,aes(x, y, z = z))+
stat_contour(aes(colour = ..level..))+
stat_contour(data=volcano3d_2,aes(x,y,z=z,colour=..level..))
direct.label(v)

However, I can't get direct.label to add labels to the second part of the map. 但是,我无法direct.label将标签添加到地图的第二部分。 It seems like the difficulty is with getting it to label a layer that has data different from the primary ggplot layer. 似乎难以将其标记为具有与主要ggplot层不同的数据的图层。 Does anyone know of a way I can make it label both layers? 有谁知道我可以让它标记两个图层的方式?

Use geom_dl to label additional layers: 使用geom_dl标记其他图层:

WithLegend <- ggplot(volcano3d_1,aes(x, y, z=z, colour=..level..))+
  stat_contour()+
  stat_contour(data=volcano3d_2)
## direct.label labels the first colored layer.
SomeLabels <- direct.label(v)
## Additional labels can be added using geom_dl layers.
MoreLabels <- SomeLabels+
  geom_dl(aes(label=..level.., colour=..level..),
          data=volcano3d_2, method="top.pieces", stat="contour")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM