How to change the legend title using geom_tile

My question is how to change the title of the ggplot legend. It seems an easy question, but I have tried various ways and still couldn't get it correct.

wd.files <- c("wind_39.9375_-105.3125"," wind_39.9375_-105.4375",
              "wind_40.0625_-105.3125", "wind_40.0625_-105.4375")

In each txt file, such as "wind_39.9375_-105.3125", there are annual average wind speed from 1950 to 2000, formatted like the following:

I have combined all the txt files together (each file represents wind speed at that grid cell), and got the dataset with three columns: longitude, latitude, and wind speed (at the specific grid cell). I use the code below to plot the surface, but don't know how to change the title of the legend to "Wind speed (m/s)". Could anyone help me with this? Thanks for your help.

wd.yr =,col.names=c('lon','lat','wd'))

colnames(wd.yr)<- c('lon', 'lat', 'wd')
my_year <- theme_bw() + theme(panel.ontop=TRUE, panel.background=element_blank())
my_fill <- scale_fill_distiller(palette='Spectral')

yr.sel = ggplot(wd.yr, aes(y=lat, x=lon, fill=wd) + geom_tile() + my_year + my_fill+ coord_quickmap(xlim=range(wd.yr$lon), ylim=range(wd.yr$lat))+ borders('world', xlim=range(wd.yr$lon), ylim=range(wd.yr$lat), colour='black')+ xlab('Longitude(°)')+ ylab('Latitude(°)')
print(yr.sel) # with region boundary

add + labs(fill="Wind speed (m/s)"). In general, use the labs function to change labels for any aesthetic (e.g., x, y, color, fill, etc.). Another option, since you're already using scale_fill_distiller() is to do scale_fill_distiller(name="Wind speed (m/s)", palette='Spectral'). In general, you can set the label using the name argument to the scale_***_***() functions.

1 Like

Thanks, it works. I tried both of the two ways previously, but maybe I missed something.

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.