如何强制x轴的刻度线出现在热图图表栏的末尾?(How to force the x-axis tic

2019-06-24 17:03发布

我创建了一个GGPLOT2简单的热图图形,但我需要强制x轴的刻度线出现在我的变量x的结束,而不是在它的中心。 例如,我希望1出现在哪里1.5现在的位置。 我beleive在基础R做会做一个热图。

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) ) 

我们的想法是创建一个简单的热图,看起来像这样:

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

Answer 1:

那这个呢?

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)



Answer 2:

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')


文章来源: How to force the x-axis tick marks to appear at the end of bar in heatmap graph?
标签: r ggplot2