更改图例标题位置而不更改标签

时间:2017-02-06 14:46:09

标签: r ggplot2

我正在建立一个前面提到的问题,geom_tile single color as 0...它从以下代码开始,提供的答案代码创建了情节:

df <- data.frame(expand.grid(1:10,1:10))
df$z <- sample(0:10, nrow(df), replace=T)
# provided answer from SO
ggplot(df,aes(x = Var1,y = Var2, fill = z)) + 
  geom_tile() + 
  scale_fill_gradientn(colours = c("white", "green", "red"), values = c(0,0.1,1))

enter image description here

我想将图例移到底部,图例标题位于顶部并居中。代码和结果如下:

ggplot(df,aes(x = Var1,y = Var2, fill = z)) + 
  geom_tile() + 
  scale_fill_gradientn(colours = c("white", "green", "red"), values = c(0,0.1,1)) +
  theme(legend.position="bottom") +
  guides(fill = guide_legend(title.position="top",label.position = "bottom"))

enter image description here

重新定位图例并且标题位于“顶部”但我丢失了连续比例并且无法使标题居中。关于改变传奇标题位置的SO有很多信息性问题,但似乎没有一个问题让连续的传奇变得谨慎。

1 个答案:

答案 0 :(得分:5)

这就是你想要的吗?

ggplot(df,aes(x = Var1,y = Var2, fill = z)) + 
  geom_tile() + 
  theme(legend.position="bottom") +
  scale_fill_gradientn(colours = c("white", "green", "red"),
                       values = c(0, 0.1, 1)) +
  guides(fill = guide_colourbar(title.position = "top",
                                title.hjust = .5,
                                label.position = "bottom"))

enter image description here

相关问题