Reputation: 681
I am building off a question asked earlier, geom_tile single color as 0... It starts with the following code and the provided answer code creates the plot:
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))
I would like to move the legend to the bottom with the legend title on top and centered. The code and result are 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"))
Repositioning the legend works and the title is on "top" but I lost the continuous scale and can't center the title. There are a number of informative q&a on SO about changing legend title position but none seem to have the problem of having a continuous legend changed to discreet.
Upvotes: 3
Views: 1899
Reputation: 1321
This is how you want it?
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"))
Upvotes: 6