zoneparser
zoneparser

Reputation: 110

Displaying a subset of features in a facet of a multi-layer plot

I'm trying to generate a multi-layered plot where the points in one layer gets displayed only in a fraction of the facets created using data from another layer. In the code below, the points in red are either x1 or x2 (just like the row labels of the facet).

library(ggplot2)

set.seed(1000)

#generate first df
df1 = data.frame(x=rep(rep(seq(2,8,2),4),4), 
                 y=rep(rep(seq(2,8,2),each=4),4),
                 v1=rep(c("x1","x2"),each=32),
                 v2=rep(rep(c("t1","t2"),each=16),2),
                 v3=rbinom(64,1,0.5)) 

# generate second df
df2 = data.frame(x=runif(20)*10,
                 y=runif(20)*10,
                 v4=sample(c("x1","x2"),20,T))

# create theme
t1=theme(panel.grid.major = element_blank(), text = element_text(size=18),
         panel.grid.minor = element_blank(), strip.background= element_blank(),
         axis.title.x = element_blank(), axis.title.y = element_blank())

# plot
ggplot() + 
  geom_point(data=df1, aes(x=x, y=y, colour = factor(v3)), shape=15,  size=5) + 
  scale_colour_manual(values = c(NA,"black")) + facet_grid(v1~v2) +
  geom_point(data=df2, aes(x=x,y=y, shape=v4), colour="red", size=4) + 
  coord_equal(ratio=1) + xlim(0, 10) + ylim(0, 10) + t1

EDIT: The black squares are generated by manually setting the colour of df1$v3 = 1 to black and df1$v3 = 0 to NA. /EDIT

But what I actually want is to display only those points from df2 with df2$v4 = x1 in the first row of facets, and df2$v4 = x2 in the second row of facets (corresponding to the values of df1$v1 and the row labels of the facet).

I've done this by generating two separate graphs...

ggplot() + 
  geom_point(data=df1[df1$v1=="x1",], shape=15,  size=5, 
  aes(x=x, y=y, colour = factor(v3)), ) + 
  scale_colour_manual(values = c(NA,"black")) + facet_grid(~v2) +
  geom_point(data=df2[df2$v4=="x1",], aes(x=x,y=y), colour="red", size=4) + 
  coord_equal(ratio=1) + xlim(0, 10) + ylim(0, 10) + t1

ggplot() + 
  geom_point(data=df1[df1$v1=="x2",], shape=15,  size=5, 
  aes(x=x, y=y, colour = factor(v3)), ) + 
  scale_colour_manual(values = c(NA,"black")) + facet_grid(~v2) +
  geom_point(data=df2[df2$v4=="x2",], aes(x=x,y=y), colour="red", size=4) + 
  coord_equal(ratio=1) + xlim(0, 10) + ylim(0, 10) + t1

... but I'm curious if a single plot can be generated because with my actual data set I have several x's and it is time consuming to piece the graphs together.

Upvotes: 0

Views: 467

Answers (1)

Nate
Nate

Reputation: 10671

does it help if we just rename df2$v4 or make a new column called df2$v1, for faceting purposes:

df2 <- dplyr::rename(df2, v1 = v4)
df2$v1 <- df2$v4
# either works

then ggplot will distribute the data points as you would like, with this:

ggplot() + 
    geom_point(data=df1, aes(x=x, y=y, colour = factor(v3)), shape=15,  size=5) + 
    scale_colour_manual(values = c(NA,"black")) +
    facet_grid(v1~v2) +
    geom_point(data=df2, aes(x=x,y=y), colour="red", size=4) + 
    coord_equal(ratio=1) + xlim(0, 10) + ylim(0, 10) + 
    t1

enter image description here

not 100% sure I grasp your problem...

Upvotes: 4

Related Questions