在R中,如何绘制二进制表(Chessboard type)

时间:2015-09-13 23:22:26

标签: r plot

我有一个像这样的简单矩阵

 >df
           School1  School2 School3
 Program1        1        1       1
 Program2        1        0       1 
 Program3        1        1       0

数字1表示学校收到了该计划和零号,没有收到。我想绘制一个简单的方块,如一个cheesboard(黑色为1,白色为0)。首先,我融化了我的df

df<- melt(df)

head(df)
         X1       X2 value
1 Program 1 School 1     1
2 Program 2 School 1     1
3 Program 3 School 1     1
4 Program 1 School 2     1
5 Program 2 School 2     0
6 Program 3 School 2     0

names(df)[1]<- "Var1"
names(df)[2]<- "Var2"

现在,我创建了我的主板

ggplot(df, aes(x=Var2, y=Var1, fill=value)) + geom_tile() +
theme(panel.background = element_blank(),
      #panel.grid.major = element_line(colour = "orange", size=2),
      panel.grid.minor = element_line(colour = "gray" , size = 3)) 

产生以下图片enter image description here

但我想做一些事情:

1.-在图上添加一个灰色网格,表示每个正方形的分离

2.-操纵颜色方块(黑色和白色)

3.-操纵轴上标签的大小(程序1,程序2,...学校1,学校2,...)及其倾向(如条形图中的las = 2)

4.-编辑条形图例(带黑色小方块的是,带白色小方格的否)

5.-排除名称轴(Var1和Var)

2 个答案:

答案 0 :(得分:4)

library(ggplot2)

dat <- read.table(text="Program School1  School2 School3
 Program1        1        1       1
 Program2        1        0       1 
 Program3        1        1       0", header=TRUE, stringsAsFactors=FALSE)

dat_long <- reshape2::melt(dat)

# discrete vs continuous
dat_long$value <- factor(dat_long$value)

gg <- ggplot(dat_long)

# fill + legend, gray border
gg <- gg + geom_tile(aes(x=Program, y=variable, fill=value), color="#7f7f7f")

# custom fill colors
gg <- gg + scale_fill_manual(values=c("white", "black"))

# squares
gg <- gg + coord_equal()

# no labels
gg <- gg + labs(x=NULL, y=NULL)

# remove some chart junk
gg <- gg + theme_bw()
gg <- gg + theme(panel.grid=element_blank())
gg <- gg + theme(panel.border=element_blank())
gg

enter image description here

根据需要交换计划与学校。

答案 1 :(得分:1)

我尝试使用基本图形(使用heatmap())和(未经充分认可的)image方法执行此操作。两者都不像我想的那么容易。

构建数据:

dat <- matrix(c(1,1,1,1,0,1,1,1,0),byrow=TRUE,nrow=3,
              dimnames=list(paste0("Program",1:3),
                            paste0("School",1:3)))

使用Matrix

不幸的是image方法(否则非常好)并不处理dimnames,所以我们不得不破解一下。

library(Matrix)
library(lattice)
## getMethod("image",sig="dgTMatrix")  ## examine the function

黑客勾选标记标签设置以使其透明:

tp.orig <- trellis.par.get()
tp2 <- tp.orig
tp2$axis.text$alpha  <- 0
## optionally add space for vertical labels?
##  tp2$layout.heights$bottom.padding <- 20  
trellis.par.set(tp2)  

image(Matrix(dat),useRaster=TRUE,sub="",xlab="",ylab="")

添加标签和网格:

trellis.focus("panel",1,1,highlight=FALSE,clip.off=TRUE)
trellis.par.set(tp.orig)
dims <- dim(dat)
dn <- dimnames(dat)
panel.axis(side="bottom",at=1:dims[2],labels=dn[[2]],outside=TRUE,rot=0)
panel.axis(side="left",at=1:dims[1],labels=dn[[1]],outside=TRUE)
panel.grid(h=2,v=2)

使用基本图形

实际上我最终使用gplots。仍然可以使用一些调整。

library("gplots")
par(oma=c(5,0,0,5))
heatmap.2(dat,dendrogram="none",col=c("gray","white"),
          key=FALSE,trace="both",tracecol="black",
          hline=numeric(0),vline=numeric(0))