2012-05-22 36 views
1

我创建了一个简单的热图图ggplot2但我需要强制x轴刻度线出现在我的x变量的末尾,而不是其中心。例如,我希望1出现在现在1.5的位置。我相信在Base R完成的热图将做到这一点。如何强制X轴刻度线出现在热图图形的条尾?

library(car) #initialize libraries 
library(ggplot2) #initialize libraries 
library(reshape) 

df=read.table(text= "x y fill 
1 1 B 
2 1 A 
3 1 B 
1 2 A 
2 2 C 
3 2 A 
", header=TRUE, sep="" ) 

#plot data 
qplot(x=x, y=y, 
     fill=fill, 
     data=df, 
     geom="tile")+ 
     scale_x_continuous(breaks=seq(1:3)) 

enter image description here

的想法是创建一个简单的热图,看起来像这样: enter image description here

在此图中的刻度线放置在酒吧,而不是其中心

结束

回答

4

这是怎么回事?

object = qplot(x=x, y=y, 
     fill=fill, 
     data=df, 
     geom="tile")+ 
     scale_x_continuous(breaks=seq(1:3)) 

object + scale_x_continuous(breaks=seq(.5,3.5,1), labels=0:3) 

enter image description here

+0

@SandyMuspratt良好的渔获物。谢谢! – Alex

+0

谢谢!我很希望有一个选项可以在不调整x比例的情况下进行,但是我可能不太了解热图的工作方式。我很感谢你的帮助 –

+0

你的意思是不调整x尺度是什么意思?而不是从0开始,你想从适当的值0.5开始? – Alex

3

geom_tile中心每瓦,在给定的坐标。所以你会期望它给出的输出。

因此,如果你给ggplot的中心(不是右上角坐标),它将工作的每个单元格。

ggplot(df, aes(x = x-0.5, y = y-0.5, fill = fill)) + 
    geom_tile() + 
    scale_x_continuous(expand = c(0,0), breaks = 0:3) + 
    scale_y_continuous(expand = c(0,0), breaks = 0:3) + 
    ylab('y') + 
    xlab('x') 

或使用qplot

qplot(data = df, x= x-0.5, y = y-0.5, fill = fill, geom = 'tile') + 
    scale_x_continuous(expand = c(0,0), breaks = 0:3) + 
    scale_y_continuous(expand = c(0,0), breaks = 0:3) + 
    ylab('y') + 
    xlab('x') 
相关问题