2015-04-02 170 views
2

我有200k * 2的矩陣,第一個矢量具有值,第二個具有-1或+1。 我想將第一個矢量繪製爲藍色點,當第二個值爲+1時,紅色點指示第二個值爲-1。根據矢量值繪製矢量

感謝,

回答

1

您可以嘗試

## Create some data 
## set.seed(1) ## To make the plot reproducible 
dat <- data.frame(a = rnorm(1000), b = sample(c(-1,1), 1000, TRUE)) 
## Plot the first column dat$a, 
## color blue if dat$b == -1 and red otherwise 
plot(dat$a, col = ifelse(dat$b == -1, "blue", "red")) 

enter image description here

而且隨着GGPLOT2

library(ggplot2) 
ggplot(dat, aes(x = seq(a), y = a, col = factor(b))) + 
    geom_point() + 
    scale_color_manual(values=c("blue", "red")) 

enter image description here

個並與ggvis

library(ggvis) 
dat$color <- c("blue", "red")[factor(dat$b)] 
dat %>% ggvis(~seq(a), ~a, fill := ~color) 

enter image description here

+0

謝謝你,它的工作原理 – Abrag 2015-04-02 11:51:35