ggplot map - adding numeric label on colorbar

53 Views Asked by At

How can I add starting and ending numeric label on color bar ggplot map figure. For now my code looks like this:

my_breaks <- c(1,60000,1200000,24000000,320000000)
ggplot(data = opcine_spojeno) +
geom_sf(aes(fill= HAMAG_Bespovratna_IRI_Pod))+
scale_fill_viridis(trans = "log",
                   name="€",
                   breaks=c(1,60000,1200000,24000000,320000000),
                   labels = scales::label_comma(), 
                   alpha = 3/4) +
 labs(
subtitle = "HAMAG BICRO:Isplaćana bespovratna sredstva u razdoblju 2016-2023",
caption = "Data: HAMAG-BICRO"
 ) +
 theme_minimal()

My map looks like this and I would like to add on color bar to the right 1 and 320 000 000, which I tried with piece of code my_breaks <- c(1,60000,1200000,24000000,320000000) but it only adds numbers in the middle of the color bar enter image description here

1

There are 1 best solutions below

3
L Tyrone On

Using the nc dataset as a basis. This will also work with your labels list as stated in your comment. I have included other potential options, commented out, that also work:

library(sf)
library(dplyr)
library(ggplot2)
library(viridis)

# Sample data
set.seed(1)
nc <- st_read(system.file("shape/nc.shp", package = "sf")) %>%
  mutate(HAMAG_Bespovratna_IRI_Pod = sample(c(1:3.2e8), 100, replace = TRUE))

my_breaks <- c(1,60000,1200000,24000000,320000000)
code_labels <- c("1€","60.000 €","1.200.000 €","24.000.000 €","320.000.000 €")

ggplot(data = nc) +
  geom_sf(aes(fill= HAMAG_Bespovratna_IRI_Pod))+
  scale_fill_viridis(trans = "log",
                     name="€",
                     breaks = my_breaks,
                     limits = c(1, 3.2e8),
                     # limits = range(my_breaks),
                     labels = scales::label_comma(),
                     # labels = code_labels,
                     alpha = 3/4) +
  labs(
    subtitle = "HAMAG BICRO:Isplaćana bespovratna sredstva u razdoblju 2016-2023",
    caption = "Data: HAMAG-BICRO"
  ) +
  theme_minimal()

result