I want to plot the legend of my fill aesthetic at the bottom and I want a) the legend to be in the middle of the plot (right under the x-axis label) and b) I want the title of the legend to be aligned to the centre of the legend.
# Libraries
library(ggplot2)
library(plyr)
library(latex2exp)
# Function to calculate
calc_wet_bulb_temperature <- function(dry_temp, RH){
wet_temp <- dry_temp*atan(0.151977 * sqrt(RH + 8.313659)) +
0.00391838 * sqrt(RH^3) * atan(0.023101*RH) -
atan(RH - 1.676331) + atan(dry_temp + RH) - 4.686035
return(wet_temp)
}
# Space to visualise
dry_temp <- seq(from = -10, to = 45, by = 1)
RH <- seq(from = 0, to = 100, by = 1)
temp_space <- expand.grid(dry_temp, RH)
names(temp_space) <- c("dry_temp", "RH")
# Calculate wet bulb temperature
temp_space <- ddply(temp_space, c("dry_temp", "RH"), summarise,
wet_temp = calc_wet_bulb_temperature(dry_temp, RH))
# Visualise space
## Calculate value where the colour scale should be red
#diff(range(temp_space$wet_temp))
#diff(c(range(temp_space$wet_temp)[1], 30))
#42.69626/57.88312
## Actual plot
ggplot(temp_space, aes(x = RH, y = dry_temp, fill = wet_temp)) +
geom_tile() +
coord_equal(expand = FALSE) +
scale_fill_viridis_c(option = "H",
values = c(0.0, 0.2, 0.4, 0.6, 0.7376289, 1.0),
limits = range(temp_space$wet_temp)) +
theme(legend.position = "bottom",
legend.title.position = "top") +
labs(title = "Wet - bulb temperature",
x = "Relative humidity",
y = TeX(r"( Dry temperature ($C\degree$) )"),
fill = TeX(r"( Wet temperature ($C\degree$) )"))
Add plot.title=element_text(hjust = 0.5)
and legend.key.size = unit(0.8, "cm")
to your theme
.
theme(legend.position = "bottom",
legend.title.position = "top",
plot.title = element_text(hjust = 0.5),
legend.key.size = unit(0.8, "cm"))