2015-04-02 159 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