【问题标题】:Hide some heatmap axis-text隐藏一些热图轴文本
【发布时间】:2013-05-07 22:00:29
【问题描述】:

我有一张包含很多行(数千个城市)的热图,为了清楚起见,我只想显示其中几个的名称。我仍然想显示整个热图,因为颜色可以说明情况(城市名称并不重要,但出于教学目的,我想显示其中的一些)。

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)

在仅包含五个城市的示例图中,很容易看到所有五个城市名称,但在包含数千个城市的实际示例中,它们模糊在一起。

我怎样才能看到完全相同的热图,但只显示大约三分之一的城市名称?我包括了有序因子,因为顺序与绘图可视化相关(分解可能是我遇到问题的原因,但因子顺序必须存在)。

【问题讨论】:

    标签: r ggplot2 heatmap


    【解决方案1】:

    如果您通过从城市变量的级别抽样来创建包含要标记的城市的向量:

    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 的回答)

    【讨论】:

    • 谢谢,成功了。因素的顺序没问题。
    猜你喜欢
    • 2011-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多