2014-11-03 84 views
0

我產生這樣一個情節:添加多個協變量ggplot散點圖

library(ggplot2) 

data.dist = matrix(
    c(10, -10, 10, -10, 10, -10, 10, -10, 10), 
    nrow=3, 
    ncol=3, 
    byrow = TRUE) 

hc <- agnes(dist(data.dist), method = "ward", diss = TRUE) 
cluster <- cutree(hc, k=2) 
xy <- data.frame(cmdscale(dist(data.dist)), factor(cluster)) 
names(xy) <- c("x", "y", "cluster") 
xy$model <- rownames(xy) 

ggplot(xy, aes(x, y)) + geom_point(aes(colour=cluster), size=3) 

這給了我:

enter image description here

然而,假設我想將另一個協,說一個二進制變量c(1, 0, 1)添加到數據中,並使用一個符號(比如一個X)和全部0使用另一個符號(比如一個點)顯示所有的1。我怎樣才能做到這一點?

+1

將形狀美學映射到您的協變量。我建議你學習一些ggplot2教程。 – Roland 2014-11-03 15:50:14

回答

1
xy<-data.frame(x=rnorm(3),y=rnorm(3),cluster=as.factor(c(1,0,1)),another=as.factor(c(1,1,0))) 
ggplot(xy, aes(x, y,shape=another)) + geom_point(aes(colour=cluster), size=3)