【问题标题】:Axis labels with geom_sf & ggmap带有 geom_sf 和 ggmap 的轴标签
【发布时间】:2020-06-17 14:13:31
【问题描述】:

我正在尝试使用使用 ggmap (get_stamenmap) 拉入的基本地图创建地图,其中一些 shapefile 放在顶部 (geom_sf)。我想更改轴标签!为什么这么难!

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

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

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

  4. 当我根据需要添加带有标签文本的 scale_y_discrete 时,我不断收到此错误:

错误:沿 y 方向的断点和标签长度不同

这里发生了什么?我缺少的 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"))

然后给我这个:

我添加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"))

谢谢!

【问题讨论】:

标签: r ggplot2 ggmap sf


【解决方案1】:

为了让您的示例起作用,我必须将 inherit.aes = FALSE 添加到 geom_sf 并(如评论建议的那样)将 scale 更改为 continuous

y 轴的问题是标签是“20 S”和“19.5 S”,当表示为数字时应该是 -20 和 -19.5。查看bbox 调用中的值。

ggmap(SA) +
  geom_sf(data = g, inherit.aes = FALSE) +
  coord_sf(expand = F) +
  scale_x_continuous(breaks = c(33.5, 34, 34.5, 35, 35.5),
                     labels = c("33.5", "34", "34.5", "35", "35.5")) +
  scale_y_continuous(breaks = (c(-20, -19.5, -19, -18.5, -18,-17.5)),
                     labels = c("20", "19.5", "19", "18.5", "18", "17.5"))

【讨论】:

    猜你喜欢
    • 2022-08-19
    • 2012-04-26
    • 2020-02-28
    • 1970-01-01
    • 2014-06-29
    • 1970-01-01
    • 2012-09-20
    • 2014-12-20
    • 2014-02-12
    相关资源
    最近更新 更多