0

I'd like to create a map in ggplot2 with my target coordinates, the north arrow and scale bar for example, but despite the ggsave() function saving the last plot, it doesn't work in mymap.png image. In my example:

#Packages
library(ggplot2)
library(ggsn)

# Get data set - x any are the points
all.stands.predict<-read.csv("https://raw.githubusercontent.com/Leprechault/trash/main/prediction__bug_2021-03-18.csv")
all.stands.predict<-all.stands.predict[all.stands.predict[,3]=="VILA PALMA",] # Area selection

#Create a map
gg <- ggplot() +
  geom_point(data=all.stands.predict, 
  aes(x=x, y=y), color="red") +
  xlab("Latitude") + ylab("Longitude") +
  theme_bw() 

#Add a scale bar.
gg <- gg + scalebar(location="bottomright",y.min=max(all.stands.predict$y)-0.001, y.max=max(all.stands.predict$y), 
             x.min=max(all.stands.predict$x)-0.001, x.max=max(all.stands.predict$y), model='WGS84',
             transform=TRUE)
#

#Add a north arrow
north2(gg, 0.85, 0.85, symbol = 10)

#Save image in png
ggsave("mymap.png", dpi=300, width = 20, height = 20)   
#

When I inspected my "mymap.png" image created, just the north arrow is represented and looks like this:

mymap.png

Please, any ideas for saved all the map elements?

Thanks in advance!

2
  • If you are just adding the symbol then use north with data=NULL like this gg + north(...). May this help, I would try out some example map when time allow. Commented Mar 19, 2021 at 7:51
  • Thanks but not work, the output is: #Add a north arrow gg+north(data=NULL, 0.85, 0.85, symbol = 10) Error in north(data = NULL, 0.85, 0.85, symbol = 10) : argumento "x.min" ausente, sem padrão Commented Mar 19, 2021 at 11:59

1 Answer 1

1

A better solution is possible with the ggspatial package:

#Packages
library(ggplot2)
library(ggspatial)
library(sf)

# Get data set - x any are the points
all.stands.predict<-read.csv("https://raw.githubusercontent.com/Leprechault/trash/main/prediction__bug_2021-03-18.csv")
all.stands.predict<-all.stands.predict[all.stands.predict[,3]=="VILA PALMA",] # Area selection

#Create a map
(sites <- st_as_sf(all.stands.predict, coords = c("x", "y"), 
                   crs = 4326, agr = "constant"))
gg <- ggplot() +
  geom_sf(data=sites, color="red") +
  annotation_north_arrow(location = "bl", which_north = "true", 
                         pad_x = unit(0.3, "in"), pad_y = unit(0.5, "in"),
                         style = north_arrow_fancy_orienteering) + #Add a north arrow
  annotation_scale(location = "bl", width_hint = 0.55) + #Add a scale bar
  xlab("Latitude") + ylab("Longitude") +
  theme_bw() 
plot(gg)
ggsave("mymap.png", dpi=300, width = 20, height = 20)  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.