ggplot图例删除值

时间:2014-08-22 21:13:10

标签: r ggplot2

要在ggplot中更改以下热图的y轴上的文本大小,theme下面的代码可以正常工作,但如果我还需要删除一些轴标签,例如每三个只显示一个他们(在下面的图中我需要显示“Ej3”,“Ej6”和“Ej9”)如何才能显示这些标签?enter image description here

我的数据:

"people","1","2","3","4","5","6","7","8","9"
"Ej1",0,0,0,1,0,1,1,1,0
"Ej2",0,1,1,0,0,0,1,1,0
"Ej3",0,1,1,1,0,0,0,1,1
"Ej4",0,1,0,0,1,1,0,0,1
"Ej5",1,0,1,1,0,1,1,1,1
"Ej6",1,1,0,1,1,1,0,0,0
"Ej7",0,1,1,0,0,0,0,1,1
"Ej8",0,0,1,1,1,1,1,0,0
"Ej9",1,1,0,0,1,0,0,1,1
到目前为止

代码:

library(reshape2)
library(ggplot2)
library(scales)
library(plyr)
data <- read.csv("fruits2.txt", head=TRUE, sep=",")
data$people <- factor(data$people,levels=rev(data$people))
data.m = melt(data)
#data.m <- ddply(data.m, .(variable), transform, rescale = rescale(value))
data.m[,"rescale"]<-rescale(data.m[,"value"],to=c(0,1))
p <- ggplot(data.m, aes(variable, people)) +
     geom_tile(aes(fill = rescale), colour = "white") +
     scale_fill_gradient(low = "red", high = "green") +
     theme(axis.text=element_text(size=8))

p

1 个答案:

答案 0 :(得分:1)

我能想到的最简单的方法是定义新的标签,并留下你想要消除的空白:

fewer.labels <- c("Ej9","","","Ej6","","","Ej3","","")
p <- ggplot(data.m, aes(variable, people)) +
     geom_tile(aes(fill = rescale), colour = "white") +
     scale_y_discrete(labels=fewer.labels) +
     scale_fill_gradient(low = "red", high = "green") +
     theme(axis.text=element_text(size=8))

p

enter image description here