Ваш вопрос не очень понятно, но, возможно, что-то вроде это?
#some data
set.seed(42)
dat <- matrix(sample(c("A","B",NA), 25, TRUE), 5)
# [,1] [,2] [,3] [,4] [,5]
# [1,] NA "B" "B" NA NA
# [2,] NA NA NA NA "A"
# [3,] "A" "A" NA "A" NA
# [4,] NA "B" "A" "B" NA
# [5,] "B" NA "B" "B" "A"
#reshape, so ggplot likes it
library(reshape2)
df <- melt(t(dat))
df$value <- as.character(df$value)
#to be able to plot NA values
df$value[is.na(df$value)] <- "NA"
library(ggplot2)
ggplot(df, aes(x=Var1, y=-Var2, fill=value)) +
geom_tile() +
scale_x_continuous(expand=c(0,0), breaks=seq_len(max(df$Var1))) +
scale_y_continuous(expand=c(0,0), breaks=-seq_len(max(df$Var2)),
labels=seq_len(max(df$Var2))) +
scale_fill_manual(values=c("A"="red", "B"="black", "NA"="grey")) +
theme_bw() +
theme(axis.title=element_blank())
Если вы действительно предпочитаете точки, вы можете использовать geom_point
.
Edit:
Если матрица имеет dimnames:
rownames(dat) <- letters[1:5]
colnames(dat) <- letters[6:10]
# f g h i j
# a NA "B" "B" NA NA
# b NA NA NA NA "A"
# c "A" "A" NA "A" NA
# d NA "B" "A" "B" NA
# e "B" NA "B" "B" "A"
#reshape, so ggplot likes it
library(reshape2)
df <- melt(t(dat))
df$value <- as.character(df$value)
#to be able to plot NA values
df$value[is.na(df$value)] <- "NA"
#get the order of rows as in print(dat)
df$Var1 <- factor(as.character(df$Var1), levels=colnames(dat), ordered=TRUE)
df$Var2 <- factor(as.character(df$Var2), levels=rev(rownames(dat)), ordered=TRUE)
library(ggplot2)
ggplot(df, aes(x=Var1, y=Var2, fill=value)) +
geom_tile() +
scale_x_discrete(expand=c(0,0)) +
scale_y_discrete(expand=c(0,0)) +
scale_fill_manual(values=c("A"="red", "B"="black", "NA"="grey")) +
theme_bw() +
theme(axis.title=element_blank())
@ SeñorO Я не Коула сделать это, но я пытался что-то вроде: сюжет (матрица, xlim = с (1, nrow), ylim = C (1, Ncol), тип = "п") sapply (1 : nrow, функция (Х) {точки (1: Ncol, Rep (X, Ncol), PCH = IfElse (матрица [1,] == "НС", 19, 1))}) – EpiMan