【发布时间】:2012-02-05 17:34:52
【问题描述】:
在研究了 Hadley 的书并在 SO 上搜索后,我创建了一个由年月矩阵组成的热图,颜色根据时间序列变量的百分比变化而变化。热图和我用来获取它的代码如下所示。我还有几个问题我自己无法解决。
1) 如何对矩阵进行排序/排序,以便 y 轴上的年份从最早到最晚?在这种情况下,我希望年份从 1995 年的顶部开始,并在 y 轴的底部继续到 2011 年。
2) 我希望每年都显示在 y 轴上,但它只显示每 5 年一次。我认为我的代码中的 scale_y_date 会处理这个问题,但它似乎没有影响。如何强制每年显示 y 轴?
3) 我怀疑这仍然在 ggplot2“待办事项”列表中,但是有没有办法让 x 轴标签(在本例中为 Jan、Feb、Mar、Apr 等)显示在顶部情节而不是底部?
require(ggplot2)
mydf <- data.frame(date=seq(as.Date("1995/1/1"), by="month", length.out=203),yoy=runif(203, min=-1, max=1))
p_heat <- ggplot(mydf, aes(x=month(date,label=TRUE),y=year(date), fill = yoy, label = sprintf("%1.1f%%", 100*yoy),size=10)) +
scale_y_date(major="years", format="%Y") +
geom_tile() + geom_text(aes(size=10)) +
scale_fill_gradient2(low = "red", high = "green") +
scale_x_discrete(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0)) +
opts(title="Value (%)") +
opts(panel.grid.minor=theme_blank()) +
opts(axis.ticks = theme_blank()) +
opts(panel.grid.major=theme_blank()) +
opts(axis.title.y = theme_blank()) +
opts(axis.title.x = theme_blank()) +
opts(legend.position = "none")
p_heat
【问题讨论】:
-
请避免使用红绿色配色方案,因为约 10% 的男性很难区分正负值。