带有geom_sf和ggmap的轴标签

问题描述 投票:1回答:1

[我正在尝试使用通过ggmap(get_stamenmap)拉入的底图创建地图,并在顶部放置一些shapefile(geom_sf)。我想更改轴标签!为什么这么难!

  1. 当我只绘制底图时,我得到的轴标有经/纬度和无字符-即-19

  2. 当我使用geom_sf添加shapefile时,得到标有经/纬度AND字符的轴-即19°S

    ] >>
  3. 当我尝试使用scale_x_discretesf_coord(expand = F)更改轴标签时,我可以得到想要的东西

  4. 当我想给它们添加带有标签文本的scale_y_discrete时,我不断收到此错误:

  5. Error: Breaks and labels along y direction are different lengths

这里发生了什么?是否缺少我在y轴上看不见的断点?

此作品:

ggmap(SA) +
  geom_sf(data = traj_outSF, alpha = 0.4, inherit.aes = F) +
  coord_sf(expand = FALSE) +
  xlab(expression(paste("Longitude (", degree,"E)"))) +
  ylab(expression(paste("Latitude (", degree,"S)"))) +
  scale_x_discrete(breaks = c(33.5, 34, 34.5, 35, 35.5), labels = c("33.5", "34", "34.5", "35", "35.5"))

并且让我得到这个:correct x axis

一旦添加scale_y_discrete,我就会收到错误消息

ggmap(SA) +
  geom_sf(data = traj_outSF, alpha = 0.4, inherit.aes = F) +
  coord_sf(expand = FALSE) +
  xlab(expression(paste("Longitude (", degree,"E)"))) +
  ylab(expression(paste("Latitude (", degree,"S)"))) +
  scale_x_discrete(breaks = c(33.5, 34, 34.5, 35, 35.5),
                   labels = c("33.5", "34", "34.5", "35", "35.5"))
  scale_y_discrete(breaks = c(20, 19.5, 19, 18.5, 18,17.5),
                   labels = c("20","19.5","19","18.5","18","17.5"))

这里有一个代表:

g = st_sfc(st_point(c(34,-19)))
st_crs(g) <- 4326

SA <- get_stamenmap(bbox = c(33.18, -20.3, 35.8, -17.3), maptype = "toner-lite", zoom = 11)

ggmap(SA) +
  geom_sf(data = g) +
  coord_sf(expand = F) +
  scale_x_discrete(breaks = c(33.5, 34, 34.5, 35, 35.5),
                   labels = c("33.5", "34", "34.5", "35", "35.5")) +
  scale_y_discrete(breaks = c(20, 19.5, 19, 18.5, 18,17.5),
                   labels = c("20","19.5","19","18.5","18","17.5"))

谢谢!

[我正在尝试使用通过ggmap(get_stamenmap)拉入的底图创建地图,并在顶部放置一些shapefile(geom_sf)。我想更改轴标签!为什么这么难!当我只绘制...

r ggplot2 ggmap sf
1个回答
0
投票

为了使您的示例生效,我必须将inherit.aes = FALSE添加到geom_sf,然后(如注释所建议的那样)将scale更改为continuous

© www.soinside.com 2019 - 2024. All rights reserved.