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

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

ggplot specify longitude/latitude axis breaks

问题

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

poly <- trial_areas %>%
  filter(Id==5) 
ext <- st_bbox(poly)

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

plot_SoundOfSleat

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


<details>
<summary>英文:</summary>

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)? 

    poly &lt;- trial_areas %&gt;% 
    filter(Id==5) 
    ext &lt;- st_bbox(poly)
  
    plot_SoundOfSleat &lt;- ggplot() +
    theme(panel.background = element_rect(fill = &#39;light blue&#39;),element_line()) +
    geom_sf(data=poly)+
    geom_sf(data=prod_areas,fill=mycol) +
    geom_sf(data = Scot, aes(),
            fill = &quot;lightgreen&quot;,col=&quot;darkgreen&quot;) +
    coord_sf(xlim = c(ext[1], ext[3]), ylim = c(ext[2], ext[4]))  +
    ggtitle(&quot;Sound of Sleat Trial Area 5&quot;) +
    geom_sf_text(aes(label = Producti_1), data=prod_areas,size=3,hjust=0, vjust=0) +
    labs(x = &quot;Longitude&quot;, y= &quot;Latitude&quot;)

    plot_SoundOfSleat

[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/iZeme.png

</details>


# 答案1
**得分**: 6

`geom_sf()` 应该能够与 `ggplot2::scale_*_continuous()` 无缝配合使用,您可以使用 `breaks = ` 参数。请注意西经度,因为它们在数据中是负数,但在标签中是正数。

以下是一些示例:

```R
library(sf)

# 样本数据
nc <- st_read(system.file("shape/nc.shp", package="sf"))

# 不编辑格网线
nc_1 <- ggplot(nc) + 
          geom_sf() +
          ggtitle('原始')

nc_2 <- ggplot(nc) +
          geom_sf() + 
          scale_y_continuous(breaks = c(34, 35, 36)) + 
          scale_x_continuous(breaks = seq(-84, -76, by = 1)) +
          ggtitle('更少的纬度,更多的经度')

nc_3 <- ggplot(nc) +
          geom_sf(data = st_graticule(nc, 
                                      lat = seq(34, 36, by = 1),
                                      lon = seq(-84, -76, by = 4)), 
                  color = 'orange') +
          geom_sf() +
          coord_sf(datum = NA) +
          ggtitle('使用 st_graticule')

# 使用 cowplot 输出单个图像,而不是3个
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:

library(sf)

# sample data
nc &lt;-  st_read(system.file(&quot;shape/nc.shp&quot;, package=&quot;sf&quot;))

# No edits to graticules
nc_1 &lt;- ggplot(nc) + 
          geom_sf() +
          ggtitle(&#39;original&#39;)

nc_2 &lt;- ggplot(nc) +
          geom_sf() + 
          scale_y_continuous(breaks = c(34, 35, 36)) + 
          scale_x_continuous(breaks = seq(-84, -76, by = 1)) +
          ggtitle(&#39;fewer lat, more lon&#39;)

nc_3 &lt;- ggplot(nc) +
          geom_sf(data = st_graticule(nc, 
                                      lat = seq(34, 36, by = 1),
                                      lon = seq(-84, -76, by = 4)), 
                  color = &#39;orange&#39;) +
          geom_sf() +
          coord_sf(datum = NA) +
          ggtitle(&#39;using st_graticule&#39;)

# using cowplot to output single image, rather than 3
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:

确定