2015-03-30 58 views
2

採取以下作爲一個簡單的例子:Y與X長的不一樣,散點圖[R

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 

我想創建一個散點圖,看起來像這樣: 沒有藍色和紅色的寄宿生,他們在那裏作爲explanitary導向

enter image description here

所以我要繪製: 每次B = 1,我想使用它的C值的水平刻度和繪製C值,其中B =沿垂直刻度0 。

因此,例如;其中X=6,我們在x=3 and 2

其中X=4,我們在x=1

其中X=2有個有幾點,我們必須在x=6

一個地步,X=8,我們在x=4 and 3

有一個點我必須以某種方式操縱/融化/重塑我的數據嗎?

+0

不確定是否理解。 「X = 4」不應該在'X = 1'處有點,但不在2? – Pewi 2015-03-30 18:00:19

+0

是真的,那是一個錯誤 – lukeg 2015-03-30 19:20:02

回答

0

使用zoo包裝中的na.locf無需重新整形。

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)