Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ggspatial annotation_scale return 0 cm #123

Open
hutaohutc opened this issue Nov 23, 2023 · 2 comments
Open

ggspatial annotation_scale return 0 cm #123

hutaohutc opened this issue Nov 23, 2023 · 2 comments

Comments

@hutaohutc
Copy link

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
Copy link
Author

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

@modche
Copy link

modche commented Jul 31, 2024

Try to call annotation_scale() before coord_sf(). Michael

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants