我正在建立一个前面提到的问题,geom_tile单色为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))
Run Code Online (Sandbox Code Playgroud)
我想将图例移到底部,图例标题位于顶部并居中.代码和结果如下:
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"))
Run Code Online (Sandbox Code Playgroud)
重新定位图例工作,标题位于"顶部"但我丢失了连续刻度并且无法使标题居中.有关改变传奇标题位置的SO的一些信息性问答,但似乎没有一个问题是将连续的传奇改为谨慎.
这是你想要的吗?
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"))
Run Code Online (Sandbox Code Playgroud)