2013-05-07 66 views
1

我有一个包含很多行(成千上万个城市)的热图,为了清楚起见,我只想显示其中几个的名称。我仍然想要展示整个热图,因为颜色可以让人了解这种情况(城市名称并不重要,但我想为了教学目的展示其中的一些)。隐藏一些热图贴图文本

library(ggplot2) 
n <- 15 
c.1 <- c(rep(x="Summer", times=n), rep(x="Winter", times=n)) 
c.2 <- c(rep(x="Dallas", times=n/5), rep(x="Seattle", times=n/5), rep(x="Atlanta", times=n/5), rep(x="Chicago", times=n/5), rep(x="Boston", times=n/5)) 
c.3 <- c("Morning", "Midday", "Evening") 
to.plot <- data.frame(cbind(c.1, c.2, c.3)) 
to.plot$value <- sample(rep(x=c("Bad", "Average", "Good"), times=100), 10) 
colnames(to.plot) <- c("Season", "City", "Time", "value") 
to.plot$City <- factor(to.plot$City, levels=c("Seattle", "Chicago", "Dallas", "Atlanta", "Boston"), ordered=TRUE) 

p <- ggplot(to.plot, aes(x=Season, y=City)) 
p <- p + geom_tile(aes(fill=value), colour="white") 
p <- p + scale_fill_manual(values=c("red", "green", "yellow")) 
p <- p + theme(legend.position = "none", axis.text.x=element_text(angle=90, 8)) 
p <- p + facet_grid(. ~ Time) 
p <- p + theme(legend.position = "none") 
print(p) 

在这个例子中,情节,只有五个城市很容易看到所有五个城市的名字,但与上千个城市的真实的例子,他们模糊起来。

我怎样才能看到完全相同的热图,但只显示三分之一左右的城市名称?我包括有序的因素,因为顺序与情节可视化相关(因式分解可能是为什么我有问题,但因子顺序必须在那里)。

回答

2

如果你创建一个包含要通过取样从城市变量的水平来标记城市矢量:

breakpoints <- levels(to.plot$City)[seq(1, length(levels(to.plot$City)), 2)] 

调整为“2”决定有多少个标签,你可能要摆弄,直到你你喜欢的东西。

然后在你的代码的末尾添加:

p <- p + scale_y_discrete(breaks = breakpoints) 
print(p) 

告诉ggplot放在哪里使用新的向量y轴断裂。我认为这仍然保留了这个因素的顺序呢?

这有帮助吗?

(部分感谢nico对Extracting every nth element of a vector的回答)

+0

谢谢,就是这样。因素顺序好。 – Chris 2013-05-07 22:41:08