我有一个看起来像这样的矩阵:
1 2 3 4 5 6 7 8 9
name1 0 0 1 0 1 0 0 0 0
name2 1 1 0 0 0 0 0 0 0
name3 0 0 0 2 0 0 0 0 0
name4 0 1 0 0 0 0 0 1 0
我想制作一种散点图,如果有' 1'有绿点,有' 2' ' 0'。姓名*是rownames。我怎样才能做到这一点? 我想替换热图。
答案 0 :(得分:1)
这将进行绘图:
plot(which(d==1, arr.ind=TRUE)[,'col'], which(d==1,arr.ind=TRUE)[,'row'], col="red",
xlim=c(1,ncol(d)), ylim=rev(c(1,nrow(d))))
points(which(d==2, arr.ind=TRUE)[,'col'], which(d==2,arr.ind=TRUE)[,'row'], col="green")
如果您不喜欢默认行为,则可以明确设置行号和列号:
plot(which(d==1,arr.ind=TRUE)[,'col'], which(d==1,arr.ind=TRUE)[,'row'], col="red",
xlim=c(1,ncol(d)), ylim=rev(c(1,nrow(d))), xaxt="n", yaxt="n")
points(which(d==2,arr.ind=TRUE)[,'col'], which(d==2,arr.ind=TRUE)[,'row'], col="green")
axis(1, at = seq(1, 9, by = 1))
axis(2, at = seq(1, 4, by = 1))
答案 1 :(得分:1)
这是一个ggplot2解决方案。
x = "0 0 1 0 1 0 0 0 0
1 1 0 0 0 0 0 0 0
0 0 0 2 0 0 0 0 0
0 1 0 0 0 0 0 1 0"
df = (t(matrix(scan(text= x), nrow = 9)))
library(reshape2)
df = melt(d[c(4:1),])
ggplot(df[df$value!=0,], aes(y = X1, x = X2)) +
geom_point(aes(col = factor(value)), size = 10) +
scale_color_manual(name="", values = c("red", "green")) +
scale_y_continuous(label = paste(rep("name", 4), 1:4)) +
scale_x_continuous(breaks = 1:8) +
theme_bw() + xlab("") + ylab("")