英文:
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
<details>
<summary>英文:</summary>
I am using the following code to create a map of one 'trial_area' from a shapefile and overlaying a coastline and 'prod_areas' from a second shapefile. I'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 <- 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
[![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 <- st_read(system.file("shape/nc.shp", package="sf"))
# No edits to graticules
nc_1 <- ggplot(nc) +
geom_sf() +
ggtitle('original')
nc_2 <- ggplot(nc) +
geom_sf() +
scale_y_continuous(breaks = c(34, 35, 36)) +
scale_x_continuous(breaks = seq(-84, -76, by = 1)) +
ggtitle('fewer lat, more lon')
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('using st_graticule')
# using cowplot to output single image, rather than 3
cowplot::plot_grid(nc_1, nc_2, nc_3, ncol = 1)
You should be able to use the output from st_bbox to automate a sensible number of graticules (grid lines) if necessary.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论