ggplot指定经度/纬度轴刻度值

huangapple go评论110阅读模式
英文:

ggplot specify longitude/latitude axis breaks

问题

我正在使用以下代码来创建一个地图,该地图基于一个shapefile中的一个'trial_area',并在一个第二个shapefile中叠加了一个海岸线和'prod_areas'。然后,我使用coord_sf来缩放地图以适应trial_area多边形的st_bbox。然而,对于某些区域(参见下面的示例),轴刻度文本标签最终会重叠在一起,是否有一种方法可以指定轴刻度标记的间隔以避免这种情况(例如,纬度为0.1,经度为0.5)?

  1. poly <- trial_areas %>%
  2. filter(Id==5)
  3. ext <- st_bbox(poly)
  4. plot_SoundOfSleat <- ggplot() +
  5. theme(panel.background = element_rect(fill = 'light blue'), element_line()) +
  6. geom_sf(data=poly) +
  7. geom_sf(data=prod_areas, fill=mycol) +
  8. geom_sf(data = Scot, aes(),
  9. fill = "lightgreen", col="darkgreen") +
  10. coord_sf(xlim = c(ext[1], ext[3]), ylim = c(ext[2], ext[4])) +
  11. ggtitle("Sound of Sleat Trial Area 5") +
  12. geom_sf_text(aes(label = Producti_1), data=prod_areas, size=3, hjust=0, vjust=0) +
  13. labs(x = "Longitude", y= "Latitude")
  14. plot_SoundOfSleat

ggplot指定经度/纬度轴刻度值

  1. <details>
  2. <summary>英文:</summary>
  3. I am using the following code to create a map of one &#39;trial_area&#39; from a shapefile and overlaying a coastline and &#39;prod_areas&#39; from a second shapefile. I&#39;m then using the coord_sf to zoom the map to the st_bbox of the trial_area polygon. However for some areas (see the example below) the axis tick text labels end up overlapping, is there a way that i can specify the axis tick mark intervals to avoid this (e.g. .1 for latitude and .5 for longitude)?
  4. poly &lt;- trial_areas %&gt;%
  5. filter(Id==5)
  6. ext &lt;- st_bbox(poly)
  7. plot_SoundOfSleat &lt;- ggplot() +
  8. theme(panel.background = element_rect(fill = &#39;light blue&#39;),element_line()) +
  9. geom_sf(data=poly)+
  10. geom_sf(data=prod_areas,fill=mycol) +
  11. geom_sf(data = Scot, aes(),
  12. fill = &quot;lightgreen&quot;,col=&quot;darkgreen&quot;) +
  13. coord_sf(xlim = c(ext[1], ext[3]), ylim = c(ext[2], ext[4])) +
  14. ggtitle(&quot;Sound of Sleat Trial Area 5&quot;) +
  15. geom_sf_text(aes(label = Producti_1), data=prod_areas,size=3,hjust=0, vjust=0) +
  16. labs(x = &quot;Longitude&quot;, y= &quot;Latitude&quot;)
  17. plot_SoundOfSleat
  18. [![enter image description here][1]][1]
  19. [1]: https://i.stack.imgur.com/iZeme.png
  20. </details>
  21. # 答案1
  22. **得分**: 6
  23. `geom_sf()` 应该能够与 `ggplot2::scale_*_continuous()` 无缝配合使用,您可以使用 `breaks = ` 参数。请注意西经度,因为它们在数据中是负数,但在标签中是正数。
  24. 以下是一些示例:
  25. ```R
  26. library(sf)
  27. # 样本数据
  28. nc <- st_read(system.file("shape/nc.shp", package="sf"))
  29. # 不编辑格网线
  30. nc_1 <- ggplot(nc) +
  31. geom_sf() +
  32. ggtitle('原始')
  33. nc_2 <- ggplot(nc) +
  34. geom_sf() +
  35. scale_y_continuous(breaks = c(34, 35, 36)) +
  36. scale_x_continuous(breaks = seq(-84, -76, by = 1)) +
  37. ggtitle('更少的纬度,更多的经度')
  38. nc_3 <- ggplot(nc) +
  39. geom_sf(data = st_graticule(nc,
  40. lat = seq(34, 36, by = 1),
  41. lon = seq(-84, -76, by = 4)),
  42. color = 'orange') +
  43. geom_sf() +
  44. coord_sf(datum = NA) +
  45. ggtitle('使用 st_graticule')
  46. # 使用 cowplot 输出单个图像,而不是3个
  47. cowplot::plot_grid(nc_1, nc_2, nc_3, ncol = 1)

您应该能够使用 st_bbox 的输出来自动化合理数量的格网线(网格线),如果有必要。

英文:

geom_sf() should work seamlessly with ggplot2::scale_*_continuous(), where you can use the breaks = argument. Be careful with west longitudes, as they're negative numbers in the data but positive in the labels.

I've included some examples below:

  1. library(sf)
  2. # sample data
  3. nc &lt;- st_read(system.file(&quot;shape/nc.shp&quot;, package=&quot;sf&quot;))
  4. # No edits to graticules
  5. nc_1 &lt;- ggplot(nc) +
  6. geom_sf() +
  7. ggtitle(&#39;original&#39;)
  8. nc_2 &lt;- ggplot(nc) +
  9. geom_sf() +
  10. scale_y_continuous(breaks = c(34, 35, 36)) +
  11. scale_x_continuous(breaks = seq(-84, -76, by = 1)) +
  12. ggtitle(&#39;fewer lat, more lon&#39;)
  13. nc_3 &lt;- ggplot(nc) +
  14. geom_sf(data = st_graticule(nc,
  15. lat = seq(34, 36, by = 1),
  16. lon = seq(-84, -76, by = 4)),
  17. color = &#39;orange&#39;) +
  18. geom_sf() +
  19. coord_sf(datum = NA) +
  20. ggtitle(&#39;using st_graticule&#39;)
  21. # using cowplot to output single image, rather than 3
  22. cowplot::plot_grid(nc_1, nc_2, nc_3, ncol = 1)

ggplot指定经度/纬度轴刻度值

You should be able to use the output from st_bbox to automate a sensible number of graticules (grid lines) if necessary.

huangapple
  • 本文由 发表于 2020年1月6日 18:26:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610405.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定