How to change the scale color of the legend of raster ggplot2 R -
i want expose problem, in order give me ways forward. goal display card full raster this program:
library(raster) ; library(rgdal) ; library(sp);library(rgeos);library(ggplot2) alti=raster("raster.tif") hdf <- rastertopoints(alti) hdf <- data.frame(hdf) colnames(hdf) <- c("long","lat","altitude") ggplot()+ layer(geom="raster",data=hdf,mapping=aes(long,lat,fill=altitude))+ # draw boundaries geom_path(color="black", linestyle=0.2)+ scale_fill_gradientn(name="a",colours=c("red","blue","green","grey","yellow","orange","black"), breaks=c(0,100,200,500,750,1000,2000)) at end, can not adjust colors of legend ranges of value have proposed.
i want have legend :
red : 0-100. blue : 100-200. green : 200-500. grey : 500-750. yellow : 750-1000. orange : 1000-2000. black : upper 2000. example: range [1000-2000] it's orange, in result on legend there 3 colors grey, yellow , orange. want limits of intervals should correspond value limits of colors in legend.
if create factor variable altitude column , use scale_fill_manual(), can work properly. create nice labels legend automatically:
library(raster) ; library(rgdal) ; library(sp);library(rgeos);library(ggplot2) alti=raster("raster.tif") hdf <- rastertopoints(alti) hdf <- data.frame(hdf) colnames(hdf) <- c("long","lat","altitude") tmp <- hdf$altitude hdf$altitude <- ifelse(tmp <= 100, "0-100", hdf$altitude) hdf$altitude <- ifelse(tmp > 100 & tmp <= 200, "100-200", hdf$altitude) hdf$altitude <- ifelse(tmp > 200 & tmp <= 500, "200-500", hdf$altitude) hdf$altitude <- ifelse(tmp > 500 & tmp <= 750, "500-750", hdf$altitude) hdf$altitude <- ifelse(tmp > 750 & tmp <= 1000, "750-1000", hdf$altitude) hdf$altitude <- ifelse(tmp > 1000 & tmp <= 2000, "1000-2000", hdf$altitude) hdf$altitude <- ifelse(tmp > 2000, "upper 2000", hdf$altitude) hdf$altitude <- factor(hdf$altitude) g <- ggplot()+ layer(geom="raster",data=hdf,mapping=aes(long,lat,fill=altitude))+ # draw boundaries geom_path(color="black", linestyle=0.2)+ scale_fill_manual(values = c("red","blue","green","grey","yellow","orange","black"))
Comments
Post a Comment