#load packages #instal the packages before using install.packages("name of packages") library(reshape2) library(data.table)#to use the melt library(ggplot2)#to plot, map_data, scale_fill_gradientn, etc library(utils)#to read the data #data directory setwd("E:/My Drive/Mirror/data/") elev_grid=read.delim("Elevation_grid_1deg.txt", header = TRUE, sep = "\t", dec = ".") # prec=read.delim("Precipitaton_data.txt", header = TRUE, sep = "\t", dec = ".") r_pal=terrain.colors(10) xlim1=c(-115,-101.3) ylim1=c(29.5,42.5) df <- melt(elev_grid$Elev, na.rm = TRUE) df$Lon <- elev_grid$Lon df$Lat <- elev_grid$Lat names(df) <- c("elev","Lon", "Lat") zlime=range(elev_grid$Elev) # zlime[1]=0 zlab=seq(0,3500, by=500) siz1=14 siz2=10 MainStates <- map_data("state") states_full=c("arizona","colorado","new mexico","utah") name="Elevation" g1=ggplot() + geom_tile(data = df, aes(x = Lon, y = Lat, fill=elev))+ geom_polygon( data=MainStates, aes(x=long, y=lat, group=group), color="gray20", fill="transparent" )+ ggtitle("") + xlab("Longitude") + ylab("Latitude")+ scale_fill_gradientn(name="Elevation (m)", colours = r_pal,limits=zlime,breaks=zlab,labels=zlab,guide="colourbar")+ theme_bw()+ theme(legend.position="right")+ theme(panel.grid.major = element_line(colour = "grey50", linetype = "dashed"), legend.text = element_text(size = 10),plot.margin=unit(c(0.0,0.01,0.01,0.01),"in"))+ coord_sf(xlim = xlim1, ylim = ylim1,label_axes = list(bottom = "E", left = "N"), expand =FALSE) dev.new();g1 ggsave("Elevation.jpeg", width = 6, height = 5, units = 'in', bg = 'transparent') # save figure dev.off