R使用geom_tile()的ggplot热图:如何按年份排序并在y轴中显示所有年份? [英] R ggplot heatmap using geom_tile(): how to sort by year and show all years in y-axis?

查看:5137
本文介绍了R使用geom_tile()的ggplot热图:如何按年份排序并在y轴中显示所有年份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在研究哈德利的书并在这里搜索后,我创建了一个由年和月的矩阵组成的热图,颜色根据时间序列变量的百分比变化而变化。下面显示了热图和我用来获取它的代码。我还有一些我自己无法解决的问题。

1)如何对矩阵进行排序/排序,以便在y轴从最早到最新运行?在这种情况下,我希望这些年从1995年的顶部开始,到2011年继续在Y轴的底部。



2)我希望每一年都是显示在y轴上,而是仅显示每5年一次。我认为我的代码中的scale_y_date会处理这个问题,但它似乎没有任何影响。我怎么能强迫y轴显示每年?

3)我怀疑这仍然是在ggplot2做列表,但有没有办法使x轴标签(在这种情况下是1月,2月,3月,4月等)显示在剧情的顶部而不是底部?

  require(ggplot2)
mydf< - data.frame(date = seq(as.Date(1995/1/1),by =month,length.out = (日期,标签= TRUE),y =年(日期),y =年(日期) ,fill = year,label = sprintf(%1.1f %%,100 * year),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 = t heme_blank())+
opts(axis.title.x = theme_blank())+
opts(legend.position =none)
p_heat

解决方案

对于前两点,您可以将 scale_y_continuous 替换为 scale_y_reverse 将最早的年份放在首位,并明确指定中断和标签。

  p_heat + scale_y_reverse(breaks = 1995:2011,labels = 1995:2011,expand = c(0,0))


After studying Hadley's book and searching here on SO I have created a heatmap consisting of a matrix of years and months, with the colour varying according the percentage change in a time series variable. The heatmap and the code I used to get it are shown below. I have a few remaining questions that I haven't been able to resolve on my own.

1) How can I sort/order the matrix so that the years in the y-axis run from the earliest to the latest? In this case I want the years to start from the top at 1995 and continue to 2011 at the bottom of the y-axis.

2) I want every year to be shown on the y-axis but instead it is showing only every 5th year. I thought that the scale_y_date in my code would deal with this but it seems to have no affect. How can I force the y-axis to display each year?

3) I suspect this is still on the ggplot2 "to do" list, but is there any way to make the x-axis labels (in this case the Jan, Feb, Mar, Apr etc) display at the top of the plot rather than at the bottom?

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

解决方案

For the first two points, you can replace scale_y_continuous with scale_y_reverse to have the earliest years on top, and explicitly specify the breaks and labels.

p_heat +  scale_y_reverse(breaks=1995:2011, labels=1995:2011, expand=c(0,0) )

这篇关于R使用geom_tile()的ggplot热图:如何按年份排序并在y轴中显示所有年份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆