减少y标签和绘图区域ggplot之间的距离

时间:2016-08-09 07:47:27

标签: r ggplot2

我有以下代码:

df=data.frame(time=as.factor(rep(0.5:9.5,each=10)),
          roi=rep(1:10,10),
          area=runif(100, 5.0, 7.5))
df$time=factor(df$time, levels=rev(levels(df$time)))
ggplot(data=df, aes(x=factor(roi), y=time, fill = area)) + 
theme_minimal() +coord_fixed(ratio=1) +
geom_tile(colour = NA, width = 1.5, height = 1)+
scale_fill_gradient(low="black",high="white") +
scale_y_discrete(name="Time (min)",
expand =c(0,0),breaks=c(0.5,2.5,5.5,7.5,9.5),
labels=c(0,15,30,45,60))

如何让y标签靠近绘图区域。

非常感谢任何回复!

1 个答案:

答案 0 :(得分:4)

axis.ticks.length中将theme设置为零以删除此空格

ggplot(data=df, aes(x=factor(roi), y=time, fill = area)) + 
  theme_minimal() +
  coord_fixed(ratio=1) +
  geom_tile(colour = NA, width = 1.5, height = 1)+
  scale_fill_gradient(low="black",high="white") +
  scale_y_discrete(name="Time (min)",
                   expand =c(0,0),breaks=c(0.5,2.5,5.5,7.5,9.5),
                   labels=c(0,15,30,45,60)) +
  theme(axis.ticks.length = unit(0, "lines"))