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

wrrgggsh  于 2024-01-03  发布在  其他
关注(0)|答案(1)|浏览(184)

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

  1. library(ggplot2)
  2. library(maps)
  3. library(mapdata)
  4. library(ggpubr)
  5. library(ggspatial)
  6. library(sf)
  7. region <- map_data("world2Hires") # the basemap polygon, saved in a object called "region"
  8. region <- subset(region, region %in% c('Canada', 'USA')) # break region into Canada and the USA
  9. region$long = (360 - region$long)*-1 # convert lat/lon
  10. # Save Canada and the USA as different objects
  11. Canada <- subset(region, region == 'Canada') # Canada
  12. USA <- subset(region, region == 'USA') # USA
  13. # Set the coordinates of map
  14. lons = c(-68, -59.5) # (max longitude, min longitude)
  15. lats = c(43, 48) # (min latitude, max latitude)
  16. # Create the map using ggplot
  17. # the Longitude/Latitude of each circle comes from the CSV files imported in step 2
  18. study.area <- ggplot() +
  19. coord_sf(xlim = lons, ylim = lats) +
  20. geom_polygon(data = Canada, aes(x = long, y = lat, group = group), colour = "grey20", fill = "grey85") +
  21. geom_polygon(data = USA, aes(x = long, y = lat, group = group), colour = "grey20", fill = "grey93") +
  22. xlab("Longitude") +
  23. ylab("Latitude") +
  24. theme_bw() +
  25. theme(panel.grid.minor = element_blank(),
  26. panel.grid.major = element_blank(),
  27. panel.background = element_blank(),
  28. axis.text = element_text(size = 11),
  29. axis.title = element_text(size = 12)) +
  30. theme(plot.margin = unit(c(0.5,0.5,0.2,0.3), "cm")) +
  31. #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)
  32. labs(shape = "year") +
  33. geom_jitter() +
  34. annotation_scale()
  35. study.area

字符串

093gszye

093gszye1#

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

  1. study.area <- ggplot() +
  2. geom_polygon(data = Canada, aes(x = long, y = lat, group = group), colour = "grey20", fill = "grey85") +
  3. geom_polygon(data = USA, aes(x = long, y = lat, group = group), colour = "grey20", fill = "grey93") +
  4. xlab("Longitude") +
  5. ylab("Latitude") +
  6. theme_bw() +
  7. theme(panel.grid.minor = element_blank(),
  8. panel.grid.major = element_blank(),
  9. panel.background = element_blank(),
  10. axis.text = element_text(size = 11),
  11. axis.title = element_text(size = 12)) +
  12. theme(plot.margin = unit(c(0.5,0.5,0.2,0.3), "cm")) +
  13. #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)
  14. labs(shape = "year") +
  15. geom_jitter() +
  16. annotation_scale(location = "bl", width_hint = 0.5) +
  17. coord_sf(xlim = lons, ylim = lats, crs = 4326)
  18. study.area

字符串

展开查看全部

相关问题