Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot extend legend colorbar [duplicate]

I'm trying to make a heatmap on ggplot. I have a data frame similar to the one below

wday    hour    quant
Mon 0   1.2346944
Tue 0   0.8418860
Wed 0   0.8904375
Thu 0   0.8906767
Fri 0   1.0761553
Sat 0   2.1095617
Sun 0   2.1421696
Mon 1   2.9178615
Tue 1   0.7481875
Wed 1   0.6200556
Thu 1   0.5530000
Fri 1   0.3852611
Sat 1   0.4791192
Sun 1   1.0958043
Mon 2   2.8627222
Tue 2   0.7989769
Wed 2   0.4209105
Thu 2   0.6512810
Fri 2   0.5047176
Sat 2   0.6544059
Sun 2   0.8167846

And my code is :

ggplot(df , aes(x = hour, y = wday)) +
  geom_raster(aes(fill = quant), interpolate=TRUE) +
  scale_fill_distiller(palette = "Spectral") + 
  theme(legend.position = "bottom")

it renders a graph like that :

enter image description here

You can see that the colorbar of the legend is really contracted, how can I extend the legend horizontally so it becomes wider?

like image 214
K.Hua Avatar asked Dec 03 '22 19:12

K.Hua


1 Answers

legend.key.width is what you want. For instance,

ggplot(df , aes(x = hour, y = wday)) +
  geom_raster(aes(fill = quant), interpolate = TRUE) +
  scale_fill_distiller(palette = "Spectral") + 
  theme(legend.position = "bottom", legend.key.width = unit(2.5, "cm"))

enter image description here

like image 128
Julius Vainora Avatar answered Dec 11 '22 17:12

Julius Vainora