最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - When plotting sf data, axis lables in decimal degrees even though data have projected CRS - Stack Overflow

programmeradmin3浏览0评论

I am attmepting to graph 100 nztm points on a graph, after converting from WGS1984 I have attempted to graph the points using ggplot2 within the tidyverse package, The graph was produced but the axis display decimal degrees instead of Northing and Easting, which is what the data is in. Used the Sf package to transform and represent

require(tidyverse)
require(xml2)
require(sf)
#Reading GPX File
gpx_file <- "C:/Users/Hp001/OneDrive/Documents/FORE_342_Assignment_1/Waypoints_07-MAR-25.gpx"
gpx_raw<-read_xml(gpx_file)
#Extracting Waypoint Details from GPX files
waypoints <- xml_find_all(gpx_raw,"//*[local-name()='wpt']") 
#Converting Waypoints into Tibble
gpx_df_latlong<-tibble(
  lat=as.numeric(xml_attr(waypoints, "lat")),
  lon=as.numeric(xml_attr(waypoints, "lon")),
  ele=as.numeric(xml_text(xml_find_first(waypoints, "ele"))),
  name=xml_text(xml_find_first(waypoints, "name")),
  time=xml_text(xml_find_first(waypoints, "time"))
)
#Converting the Points from WGS1984 to NZTM2000
waypoints_sf<-st_as_sf(gpx_df_latlong,coords=c("lon","lat"),crs=4326)
waypoints_nztm<-st_transform(waypoints_sf,crs=2193)
#Splitting the Data
gpx_valley<-waypoints_nztm[1:100,]
#Checking the Transformation
st_crs(waypoints_nztm)
#Plotting the Data
ggplot() +
  geom_sf(data = gpx_valley, aes(geometry = geometry), color = "blue", size = 3) +
  coord_sf(crs=st_crs(2193))+
  labs(title= "Rapaki Valley 100 Points", x="Easting(NZTM)", y="Northing(NZTM)")+
  theme_minimal()

the dataset. The transformed data is named "gpx_valley"

I tried first to verify the data being in NZTM using: st_coordinates(gpx_valley) got results indicating the data is in NZTM I later added a specification for the coordinate system in the ggplot code block that being

coord_sf(crs=st_crs(2193)) 

with crs 2193 being the desired coordinate system.

I expected the graph produced to be in NZTM northing and easting units, like the data. Instead I received this:

I am attmepting to graph 100 nztm points on a graph, after converting from WGS1984 I have attempted to graph the points using ggplot2 within the tidyverse package, The graph was produced but the axis display decimal degrees instead of Northing and Easting, which is what the data is in. Used the Sf package to transform and represent

require(tidyverse)
require(xml2)
require(sf)
#Reading GPX File
gpx_file <- "C:/Users/Hp001/OneDrive/Documents/FORE_342_Assignment_1/Waypoints_07-MAR-25.gpx"
gpx_raw<-read_xml(gpx_file)
#Extracting Waypoint Details from GPX files
waypoints <- xml_find_all(gpx_raw,"//*[local-name()='wpt']") 
#Converting Waypoints into Tibble
gpx_df_latlong<-tibble(
  lat=as.numeric(xml_attr(waypoints, "lat")),
  lon=as.numeric(xml_attr(waypoints, "lon")),
  ele=as.numeric(xml_text(xml_find_first(waypoints, "ele"))),
  name=xml_text(xml_find_first(waypoints, "name")),
  time=xml_text(xml_find_first(waypoints, "time"))
)
#Converting the Points from WGS1984 to NZTM2000
waypoints_sf<-st_as_sf(gpx_df_latlong,coords=c("lon","lat"),crs=4326)
waypoints_nztm<-st_transform(waypoints_sf,crs=2193)
#Splitting the Data
gpx_valley<-waypoints_nztm[1:100,]
#Checking the Transformation
st_crs(waypoints_nztm)
#Plotting the Data
ggplot() +
  geom_sf(data = gpx_valley, aes(geometry = geometry), color = "blue", size = 3) +
  coord_sf(crs=st_crs(2193))+
  labs(title= "Rapaki Valley 100 Points", x="Easting(NZTM)", y="Northing(NZTM)")+
  theme_minimal()

the dataset. The transformed data is named "gpx_valley"

I tried first to verify the data being in NZTM using: st_coordinates(gpx_valley) got results indicating the data is in NZTM I later added a specification for the coordinate system in the ggplot code block that being

coord_sf(crs=st_crs(2193)) 

with crs 2193 being the desired coordinate system.

I expected the graph produced to be in NZTM northing and easting units, like the data. Instead I received this:

Share Improve this question edited Mar 19 at 18:38 L Tyrone 7,57323 gold badges28 silver badges41 bronze badges asked Mar 18 at 2:58 Dane VerharenDane Verharen 111 silver badge1 bronze badge 1
  • You might find this answer on this topic interesting. It shows that the graph is still plotting in the datum you have specified but that the lines of lat and long (graticules) are overlaid – Sarah Commented Mar 20 at 3:00
Add a comment  | 

1 Answer 1

Reset to default 1

You were close. To get the axis labels to match the corresponding CRS, you need coord_sf(datum = ...). As the coordinate values are relatively long, the default ggplot2 breaks on the x-axis can cause the labels to overlap. The code below shows one way to avoid this by adding custom breaks.

library(sf)
library(ggplot2)

# Create sample points for Rapaki Valley
points_sf <- st_as_sf(data.frame(lon = 172.645314, lat = -43.571187), 
                     coords = c("lon", "lat"), 
                     crs = 4326) |>
  st_buffer(500) |>
  st_sample(100) |>
  st_transform(2193)

# Plot with axis labels as NZTM coordinates
ggplot() +
  geom_sf(data = points_sf, fill = "red") +
  scale_x_continuous(
    breaks = seq(st_bbox(points_sf)["xmin"],
                 st_bbox(points_sf)["xmax"],
                 length.out = 3)) +
  coord_sf(datum = 2193) +
  labs(title = "Rapaki Valley 100 Points",
       x = "Easting(NZTM)",
       y = "Northing(NZTM)") +
  theme_minimal()

发布评论

评论列表(0)

  1. 暂无评论