[英]How to plot a contour with directlabels using two different dataframe
I am new to ggplot2 and having trouble adding labels with direct labels on a contour plot. 我是ggplot2的新手,无法在等高线图上添加带有直接标签的标签。 I want to draw a plot with geom_point and stat_contour.
我想用geom_point和stat_contour绘制一个图。 The point plot and contour plot have different data respectively.
点图和等高线图分别具有不同的数据。 I want add labels on the contour with directlabels.
我想用直接标签在轮廓上添加标签。
Using following script, I get a plot without directlabels appropriately, but directlabel returns following error. 使用以下脚本,我得到一个没有直接标签的情节,但是直接标签会返回错误。 error: stat_contour requires the following missing aethtics: x, y, z
错误:stat_contour需要以下缺少的aethtics:x,y,z
library(ggplot2)
library(directlabels)
library(akima)
dat<- NULL
dat$x<- c(-1.0, 0.0, 1.0)
dat$y<- c(-0.5, 0.0, 0.5)
dat$z<- matrix(c(0.2,0.2,0.2,0.2,0.3,0.4,0.3,0.4,0.4),ncol=3)
dat0<-cbind( expand.grid(dat$x,dat$y),c(dat$z))
colnames(dat0) <- c("x", "y", "z")
dat0<-data.frame(dat0)
nDivX <- 6
nDivY <- 6
z.cubic <- with(dat, bicubic.grid(x=x,y=y,z=z, xlim=c(min(x),max(x)),ylim=c(min(y),max(y)),dx=(max(x)-min(x))/nDivX,dy=(max(y)-min(y))/nDivY) )
z.cubic<-cbind( expand.grid(z.cubic$x,z.cubic$y),c(z.cubic$z))
colnames(z.cubic) <- c("x", "y", "z")
z.cubic<-data.frame(z.cubic)
p0 <- ggplot(NULL)
p1 <- geom_point(data=dat0, aes(x=x,y=y) )
p <- p0 +p1 + stat_contour(data=z.cubic, aes(x=x, y=y, z=z , colour=..level..))
#without directlabels
dev.new()
print(p)
#with directlabels
dev.new()
direct.label(p)
Giving two different dataframes may causes the trouble, but I want to give separate data because data for contour in my work is relatively large comparing that for point plot. 提供两个不同的数据帧可能会造成麻烦,但我想提供单独的数据,因为我的工作中的轮廓数据与点图相比相对较大。
Thanks for your help! 谢谢你的帮助!
I am not 100% sure, but it seems that the problem is with how direct.label
treats your first ggplot(NULL)
call. 我不是100%肯定,但似乎问题在于
direct.label
如何处理你的第一个ggplot(NULL)
调用。 Most likely it looks there for the required aesthetics, but not able to find them. 很可能它看起来是为了所需的美学,但却无法找到它们。
Here's how you can fix that: 以下是解决这个问题的方法:
pp0 <- ggplot(dat0, aes(x=x, y=y, z=z)) + geom_point()
pp <- pp0 + stat_contour(data=z.cubic, aes(x=x, y=y, z=z, colour=..level..))
and now both print(pp)
and direct.label(pp)
work as expected. 现在
print(pp)
和direct.label(pp)
都按预期工作。
Browsing the source, I found a way to fix the issue by adding the following line right before the last one in direct.label.ggplot
: dlgeom$mapping <- c(dlgeom$mapping, L$mapping)
. 浏览源代码,我找到了一种解决问题的方法,在
direct.label.ggplot
: dlgeom$mapping <- c(dlgeom$mapping, L$mapping)
的最后一行之前添加以下行。
Toby Hocking (package maintainer) answered my message and, as it turns out, the package has indeed been updated to version directlabels_2014.6.13
on R-Forge, which is correctly pointed out by @user3357659. Toby Hocking(软件包维护者)回复了我的消息,事实证明,该软件包确实已经更新到R-Forge上的
directlabels_2014.6.13
版本,@ user3357659正确指出了该版本。 In fact, the idea I'm proposing here as a workaround for an outdated version directlabels_2013.6.15
has been already implemented there. 实际上,我在这里提出的想法是过时的版本
directlabels_2013.6.15
的解决方案已经在那里实现了。
将您的直接标签升级到R-Forge版本
install.packages("directlabels", repos="http://r-forge.r-project.org")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.