paleolimbot / ggspatial

Enhancing spatial visualization in ggplot2
https://paleolimbot.github.io/ggspatial
368 stars 34 forks source link

ggspatial annotation_scale return 0 cm #123

Open hutaohutc opened 8 months ago

hutaohutc commented 8 months ago

I want to use annotation_scale to show scale in ggplot2 map . there is my code ,but i don not know why ggspatial::annotation_scale return 0 cm.

rm(list = ls())
library(ggplot2)
library(sf)
library(RColorBrewer)
library(rnaturalearth)
library(ggspatial)

Russian_data <- data.frame(province = c("Tuva" ,"Krasnoyarsk" ,"Sakhalin"   , "Kamchatka"  
                           ,"Chita"   ,    "Primor'ye"  , "Khabarovsk" , "Buryat"     
                           ,"Irkutsk" ,    "Tyumen'"  ,   "Omsk"    ,    "Novosibirsk"
                           ,"Altay"   ,    "Kemerovo"  ,  "Tomsk"  ,     "Khakass"    
                           ,"Amur"  ,      "Zabaykalsky")

                           ,number = c(1 ,1 ,1 ,1, 1, 3, 3 ,1, 1 ,1 ,1 ,1, 1, 1 ,1 ,1 ,1 ,1)

                           )

# Russian_data <- read.xlsx("俄罗斯.xlsx")
Russian_map <- ne_states(country = "Russia", returnclass = "sp")
Russian <- sp::merge(Russian_map,Russian_data,all.x = T,by.x = "name",by.y = "province")
Russian2 <- sf::st_as_sf(Russian, "sf")
st_crs(Russian2 ) <- "+proj=longlat +ellps=GRS80 +no_defs"

Russian2$number2<-cut(Russian2$number
                      ,breaks=c(0,2,4,6,8,10,15,20,25,Inf)
                      ,labels = c(
                        "[0,2 )"
                        ,"[2,4)"
                        ,"[4,6)"
                        ,"[6,8)"
                        ,"[8,10)"
                        ,"[10,15)"
                        ,"[15,20)"
                        ,"[20,25)"
                        ,"[25,Inf)"

                      ))

# +proj=longlat +ellps=GRS80 +no_defs
ggplot() + 
  geom_sf(data = Russian2
          ,aes(fill = number2)
          ,colour = "gray60"
          ,size = 0.1
  )+
  scale_fill_brewer(palette = "YlOrRd",na.value="white")+
  labs(fill="Species number")+
  theme_bw() +
  xlab("")+
  ylab("")+
  coord_sf(expand = F,xlim = c(-180, 180),crs = 4326)+
  scale_x_continuous(breaks = seq(from = -180, to = 180, by = 20))+
  scale_y_continuous(breaks = seq(from = -80, to = 80, by = 20))+
  annotation_north_arrow( ,pad_x = unit(1.5, "cm")
                          ,pad_y = unit(0.5, "cm")
                          , which_north = "false"
                          ,style = north_arrow_fancy_orienteering)+

  annotation_scale(#location ="bl"
    ,pad_x = unit(1.5, "cm")
    ,pad_y = unit(0, "cm")
  )

image

hutaohutc commented 8 months ago

it is a conflict between coord_sf and annotation_scale ,but i don't konw how to do..