How to draw a path with directlabels using two different dataframes
I am new to ggplot2 and am unable to add straight decal labels to contour plot. I want to draw a plot with geom_point and stat_contour. A scatter plot and a contour plot have different data, respectively. I want to add labels to a path with directlabels.
Using the following script, I am getting the graph without directlabels appropriately, but directlabel is returning the following error. error: stat_contour requires the following missing aefici: 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)
Providing two different dataframes can cause problems, but I want to give separate data because the data for the outline in my work is relatively large comparing it for the storyline.
Thank you for your help!
source to share
I'm not 100% sure, but it seems like the problem is how direct.label
your first call is handled ggplot(NULL)
. Most likely, he is looking for the necessary aesthetics, but cannot find them.
You can fix it here:
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 both now print(pp)
and direct.label(pp)
work as expected.
Update
By viewing the source code, I found a way to fix the problem by adding the following line directly in front of the latter in direct.label.ggplot
: dlgeom$mapping <- c(dlgeom$mapping, L$mapping)
.
Update 2
Toby Hawking (a proponent of the package) responded to my post and as it turns out the package has indeed been updated to the version directlabels_2014.6.13
in R-Forge that @ user3357659 correctly pointed out. Actually the idea I propose here as a workaround for the deprecated version directlabels_2013.6.15
is already implemented there.
source to share