【问题标题】:rename x-axis label so that coordinates are show as positive重命名 x 轴标签,使坐标显示为正
【发布时间】:2021-04-28 17:00:18
【问题描述】:

我正在尝试将“coord_sf”(由于需要投影)与 ggplot2 一起使用。但是,我无法更改 x 轴上的标签并删除坐标前的 - 符号。我想标记 x 轴中的值,如该图 (在没有coord_sf 的情况下获得)。

示例数据和代码如下。

dts<-structure(list(x = c(-119, -118, -117, -116, -115, -114, -113, 
-112, -111, -110, -109, -108, -107, -106, -105, -104, -103, -102, 
-101, -119, -118, -117, -116, -115, -114, -113, -112, -111, -110, 
-109, -108, -107, -106, -105, -104, -103, -102, -101, -119, -118, 
-117, -116, -115, -114, -113, -112, -111, -110, -109, -108, -107, 
-106, -105, -104, -103, -102, -101, -119, -118, -117, -116, -115, 
-114, -113, -112, -111, -110, -109, -108, -107, -106, -105, -104, 
-103, -102, -101, -119, -118, -117, -116, -115, -114, -113, -112, 
-111, -110, -109, -108, -107, -106, -105, -104, -103, -102, -101, 
-119, -118, -117, -116, -115, -114, -113, -112, -111, -110, -109, 
-108, -107, -106, -105, -104, -103, -102, -101, -119, -118, -117, 
-116, -115, -114, -113, -112, -111, -110, -109, -108, -107, -106, 
-105, -104, -103, -102, -101, -119, -118, -117, -116, -115, -114, 
-113, -112, -111, -110, -109, -108, -107, -106, -105, -104, -103, 
-102, -101), y = c(54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 
54, 54, 54, 54, 54, 54, 54, 54, 53, 53, 53, 53, 53, 53, 53, 53, 
53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 52, 52, 52, 52, 52, 
52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 51, 51, 
51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 
51, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 
54, 54, 54, 54, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 
53, 53, 53, 53, 53, 53, 53, 52, 52, 52, 52, 52, 52, 52, 52, 52, 
52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 51, 51, 51, 51, 51, 51, 
51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51), val = c(3.4, 
3.6, 3.5, 3.4, 3.3, 3.3, 3.4, 3.4, 3.4, 3.4, 3.5, 3.5, 3.1, 3, 
3.4, 3.6, 3.5, 3.1, 3.1, 3.5, 3.6, 3.7, 3.7, 3.5, 3.6, 3.5, 3.5, 
3.5, 3.4, 3.3, 3.4, 3.4, 3.5, 3.5, 3.5, 3.7, 3.5, 2.9, 3.6, 3.6, 
3.8, 3.8, 3.5, 3.4, 3.4, 3.5, 3.5, 3.5, 3.4, 3.4, 3.4, 3.4, 3.4, 
3.3, 3.4, 3.5, 3.7, 3.3, 3.5, 3.7, 3.5, 3.5, 3.5, 3.6, 3.4, 3.4, 
3.4, 3.4, 3.4, 3.2, 3.3, 3.3, 3.3, 3.4, 3.3, 3.4, -0.7, -0.7, 
-0.7, -0.7, -0.7, -0.7, -0.7, -0.7, -0.7, -0.7, -0.7, -0.7, -0.7, 
-0.6, -0.7, -0.7, -0.6, -0.6, -0.5, -0.7, -0.7, -0.8, -0.7, -0.7, 
-0.7, -0.7, -0.7, -0.7, -0.7, -0.7, -0.7, -0.7, -0.7, -0.7, -0.7, 
-0.7, -0.6, -0.5, -0.8, -0.8, -0.8, -0.8, -0.8, -0.8, -0.8, -0.8, 
-0.8, -0.8, -0.8, -0.7, -0.7, -0.7, -0.7, -0.7, -0.7, -0.6, -0.6, 
-0.8, -0.8, -0.8, -0.8, -0.8, -0.8, -0.8, -0.8, -0.8, -0.8, -0.8, 
-0.8, -0.7, -0.7, -0.7, -0.7, -0.7, -0.6, -0.6)), row.names = c(NA, 
-152L), class = c("tbl_df", "tbl", "data.frame"))

代码。此代码创建绘图,但 x 轴上的标签为负数,我无法将其更改为正数。任何帮助将不胜感激。

plt<-ggplot(dts,aes(x=x,y=y))+
  geom_tile(aes(fill=val),alpha=1)+
  coord_sf(datum = st_crs(3978))+ #must keep this projection ( or how can i use EPSG::3978 with coord_map ?)
  xlab("Longitude (°W)")+
  ylab("Latitude (°N)")
  # scale_x_continuous(breaks = seq(-120,-100,2),
  #                    labels = seq(120,100,-2),
  #                    expand = c(0, 0))
plt

【问题讨论】:

  • scale_x_continuous(labels = function(x) abs(x)) + 工作吗?
  • 我只是在修正我的评论,@JonSpring。更短:+ scale_x_continuous(labels=abs).
  • @r2evans 的解决方案效果很好。

标签: r ggplot2 sf


【解决方案1】:
plt<-ggplot(dts,aes(x=x,y=y))+
  geom_tile(aes(fill=val),alpha=1)+
  coord_sf(datum = st_crs(3978))+ 
  xlab("Longitude (°W)")+
  ylab("Latitude (°N)") +
  scale_x_continuous(breaks = seq(-120,-100,2),
                    labels = abs(seq(120,100,-2)))
plt  

【讨论】:

    猜你喜欢
    • 2020-12-30
    • 1970-01-01
    • 2015-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-04
    • 2023-04-05
    • 1970-01-01
    相关资源
    最近更新 更多