(ggplot) facet_grid an implicit subset not respected in geom_text (?)

In the geom_text(...)default dataset, only subsets are based on faceted variables. The easiest way to explain is with an example.

This example tries to simulate pairs (...) with ggplot (and yes, I know about the grid, and plotmatrix and ggpairs are the point to understand how ggplot works).

require(data.table)
require(reshape2)     # for melt(…)
require(plyr)         # for .(…)
require(ggplot2)

Extract mgp, hp, disp and wt from mtcars, use loops as a grouping factor

xx <- data.table(mtcars)
xx <- data.table(id=rownames(mtcars),xx[,list(group=cyl, mpg, hp, disp, wt)])

Change the shape so that we can use the ggplot faces.

yy <- melt(xx,id=1:2, variable.name="H", value.name="xval")
yy <- data.table(yy,key="id,group")
ww <- yy[,list(V=H,yval=xval), key="id,group"]
zz <- yy[ww,allow.cartesian=T]

In zz,

H: facet variable for horizontal direction
V: facet variable for vertical direction
xval: x-value for a given facet (given value of H and V)
yval: y-value for a given facet

Now you must create something close to that pairs(…),

ggp <- ggplot(zz, aes(x=xval, y=yval))
ggp <- ggp + geom_point(subset =.(H!=V), size=3, shape=1)
ggp <- ggp + facet_grid(V~H, scales="free")
ggp <- ggp + labs(x="",y="")
ggp

[]

, xvar yvar, geom_point, ; H V. , :

ggp + geom_text(subset = .(H==V),aes(label=factor(H), 
                                     x=min(xval)+0.5*diff(range(xval)),
                                     y=min(yval)+0.5*diff(range(yval))), 
                                 size=10)

:

, H (, ), xvar yvar, , zz, , H V .

: , xvar yvar , H aes? ? {: , .]

+1
1

, :

ggp + geom_text(subset = .(H==V), aes(label=factor(H),
                                     x=min(xval)+0.5*diff(range(xval)) 
                                       + runif(length(xval), max=10),
                                     y=min(yval)+0.5*diff(range(yval))
                                       + runif(length(yval), max=20)), size=10)

, , zz .

: ggplot, , , . , , , - ggplot - , , , , . , , ( ). .

0

All Articles