2

take the following as a simple example:

A <- c(1,1,1,2,2,3,3,4,4,4)
B <- c(1,0,0,1,0,1,0,1,0,0)
C <- c(6,3,2,4,1,2,6,8,4,3)

data <- data.frame(A,B,C)
data

I want to create a scatterplot that looks like so: without the blue and red boarders, they are there as an explanitary guide

enter image description here

So I want to plot: Each time B=1, I want to use its C value for the horizontal scale and plot the C value where B=0 along the vertical scale.

So for example; where X=6, we have points at x=3 and 2

where X=4, we have points at x=1

where X=2, we have a point at x=6

where X=8, we have a points at x=4 and 3

Must i manipuulate/melt/reshape my data somehow?

Pewi
  • 1,134
  • 13
  • 14
lukeg
  • 1,327
  • 3
  • 10
  • 27

1 Answers1

0

Using na.locf from the zoo package there is no need for reshaping.

library(zoo)

#extract the part of C that we need for mapping x
data$D = ifelse(data$B==1,data$C,NA)

#fill in the blanks
data$D = na.locf(data$D)

#Extract from C what we need for y
data$E = ifelse(data$B==1,NA,data$C)

#Done!
plot(data$D,data$E)
Pewi
  • 1,134
  • 13
  • 14