Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

specifying specific breaks in legend using ggplot2

library(raster)
library(dplyr)
library(ggplot2)

get.shapefile.df <- function(shp.in, df.in, 
 region.var){
 require(sf)
 require(sp)
 require(plyr)
 require(ggplot2)
 shp.in@data$id <- rownames(shp.in@data)
 shp.in@data   <- plyr::join(shp.in@data, df.in, 
 by=region.var)
 mapa.df     <- fortify(shp.in)
 mapa.df     <- plyr::join(mapa.df, shp.in@data, 
  by="id")
  return(mapa.df)
 }

mybound <- getData('GADM', country='FRA', level=1)
myShp <- getData('GADM', country='FRA', level=2)
temp <- data.frame(NAME_2 = myShp$NAME_2, 
                   value = sample(1:100, 96))

tempShp <- get.shapefile.df(myShp, temp, 'NAME_2')

ggplot() +
  geom_polygon(data = subset(tempShp, !is.na('value')), aes_string(x = 'long', y = 'lat', group = 'group', fill = 'value')) +
  geom_path(data = mybound, aes(long, lat, group = group)) + coord_equal() +
  scale_fill_viridis_c(limits = c(0, 100),option = 'D') +
  xlab(NULL) + ylab(NULL) +
  theme(plot.title = element_text(size = 8, face = "bold")) +
  theme(legend.title = element_blank(),
        legend.margin=margin(0,0,0,0), legend.box.margin=margin(-10,-10,-10,-10))

enter image description here My issue is that I am more interested in analysing the fine scale patterns of how the values between 1 to 10 are distributed. How can I specify breaks in legend which could be like:

 legendBrks <- c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 30, 50, 100)

EDIT

I ended up doing this

 temp$cuts <- cut(temp$value, 
                 breaks = c(-Inf, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 30, 50, Inf), 
                 labels = c('0-1','1-2','2-3','3-4','4-5','5-6','6-7','7-8','8-9','9-10','10-15','15-20','20-30','30-50','50-100'))

tempShp <- get.shapefile.df(myShp, temp, 'NAME_2')

ggplot() +
 geom_polygon(data = subset(tempShp), 
           aes_string(x = 'long', y = 'lat', group = 'group', fill = 'cuts')) +
 geom_path(data = mybound, aes(long, lat, group = group)) + coord_equal() +
 viridis::scale_fill_viridis(name="", discrete=TRUE) +
 xlab(NULL) + ylab(NULL) +
 theme(plot.title = element_text(size = 8, face = "bold")) +
 theme(legend.title = element_blank(),
    legend.margin = margin(0,0,0,0), legend.box.margin = 
  margin(-10,-10,-10,-10))

enter image description here

like image 400
89_Simple Avatar asked Sep 07 '25 01:09

89_Simple


1 Answers

You can just assign breaks = legendBrks inside scale_fill_viridis_c() but this makes your legend hard to read and it looks not very nice.

What I do when I want to examine patterns of unevenly distributed data in a large range I log-transform them. This way, small differences in the lower end of the range become more visible. But then it's easiest to rely on trans_breaks()for getting good breaks on your legend.

ggplot() +
  geom_polygon(data = subset(tempShp, !is.na('value')), aes_string(x = 'long', y = 'lat', group = 'group', fill = 'value')) +
  geom_path(data = mybound, aes(long, lat, group = group)) + coord_equal() +
  scale_fill_viridis_c(option = 'D', trans = "log2",  breaks = trans_breaks("log2", function(x) 2^x)) +
  xlab(NULL) + ylab(NULL) +
  theme(plot.title = element_text(size = 8, face = "bold")) +
  theme(legend.title = element_blank(),
        legend.margin=margin(0,0,0,0), legend.box.margin=margin(-10, 10,-10,- 10))

enter image description here

Maybe it serves your puproses as well.

like image 67
Humpelstielzchen Avatar answered Sep 10 '25 05:09

Humpelstielzchen