什么是annotation_scale()引用coords_sf(),使比例不准确?

wrrgggsh  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(44)

我试图用纬度和经度数据制作一张Map。我能够使用coords_sf()缩放Map上绘制的数据,但当我试图使用annotation_scale()将比例尺添加到geom_plot()时,它并没有像this例子中那样重新组织几何限制。我试图使用geom_sf()coords_sf()来绘制数据,有相同的结果(2米长的比例尺)。我已经能够添加它与ggsn::scalebar()功能,但我很好奇如何使它使用annotation_scale()工作。提前感谢!请在下面找到我的代码。

library(ggplot2)
library(maps)
library(mapdata)
library(ggpubr)
library(ggspatial)
library(sf)

region <- map_data("world2Hires")  # the basemap polygon, saved in a object called "region"
region <- subset(region, region %in% c('Canada', 'USA')) # break region into Canada and the USA
region$long = (360 - region$long)*-1    # convert lat/lon

# Save Canada and the USA as different objects
Canada <- subset(region, region == 'Canada') # Canada 
USA <- subset(region, region == 'USA')       # USA 

# Set the coordinates of map

lons = c(-68, -59.5)     # (max longitude, min longitude)
lats = c(43, 48)       # (min latitude, max latitude)

# Create the map using ggplot
# the Longitude/Latitude of each circle comes from the CSV files imported in step 2 

study.area <- ggplot() +
  coord_sf(xlim = lons, ylim = lats) +  
  geom_polygon(data = Canada, aes(x = long, y = lat, group = group), colour = "grey20", fill = "grey85") + 
  geom_polygon(data = USA, aes(x = long, y = lat, group = group), colour = "grey20", fill = "grey93") +
  xlab("Longitude") + 
  ylab("Latitude") + 
  theme_bw() +
  theme(panel.grid.minor = element_blank(),
        panel.grid.major = element_blank(),
        panel.background = element_blank(),
        axis.text = element_text(size = 11),
        axis.title = element_text(size = 12)) +
  theme(plot.margin = unit(c(0.5,0.5,0.2,0.3), "cm")) +
  #geom_point(data = mydata, aes(long, lat, fill=type), colour="black", pch=21) (this is my data plotted as points which I commented out for the sake of the example set)
  labs(shape = "year") +
  geom_jitter() +
  annotation_scale()

study.area

字符串

093gszye

093gszye1#

找到了!我需要把coord_sf()函数放在annotate_scale()函数之后。

study.area <- ggplot() + 
  geom_polygon(data = Canada, aes(x = long, y = lat, group = group), colour = "grey20", fill = "grey85") + 
  geom_polygon(data = USA, aes(x = long, y = lat, group = group), colour = "grey20", fill = "grey93") +
  xlab("Longitude") + 
  ylab("Latitude") + 
  theme_bw() +
  theme(panel.grid.minor = element_blank(),
        panel.grid.major = element_blank(),
        panel.background = element_blank(),
        axis.text = element_text(size = 11),
        axis.title = element_text(size = 12)) +
  theme(plot.margin = unit(c(0.5,0.5,0.2,0.3), "cm")) +
  #geom_point(data = mydata, aes(long, lat, fill=type), colour="black", pch=21) (this is my data plotted as points which I commented out for the sake of the example set)
  labs(shape = "year") +
  geom_jitter() + 
  annotation_scale(location = "bl", width_hint = 0.5) + 
  coord_sf(xlim = lons, ylim = lats, crs = 4326)
study.area

字符串

相关问题