Kami
Kami

Reputation: 1

Plotting map in R but it is showing nothing

My code:

library("ggplot2")
library("sf")
library("rnaturalearth")
library("rnaturalearthdata")
library("ggspatial")


chesapeake_bay <- ne_countries(scale = "medium", returnclass = "sf")
plot<-ggplot(data = chesapeake_bay) +geom_sf(fill="darkgreen") + coord_sf(xlim = c(-77.5, -75), ylim = c(37, 40),expand = TRUE)+
  xlab("Longitude") + ylab("Latitude") + ggtitle("Chesapeake Bay")+
  theme(panel.background = element_rect(fill = "lightblue")) +
  annotate(geom = "text",x = -76.1,y = 37.8,label = "Chesapeake Bay",color = "brown",size = 3, angle=90) +
  annotation_north_arrow(location = "tl",pad_x = unit(0.5, "cm"),pad_y = unit(1, "cm"),height=unit(1,"cm"),width=unit(0.5,"cm")) + 
  theme(panel.grid.major = element_line(linetype = "dashed", size = 0.2))

this code is supposed to produce a plot of a map of the Chesapeake Bay however when I run it, it doesn't produce anything.

Upvotes: 0

Views: 714

Answers (1)

Damien Georges
Damien Georges

Reputation: 126

I think this is an issue with the namespace since plot is a base R function. renaming your object plot.01 or something like that may solve the issue.

chesapeake_bay <- ne_countries(scale = "medium", returnclass = "sf")
plot.01<-ggplot(data = chesapeake_bay) +geom_sf(fill="darkgreen") + coord_sf(xlim = c(-77.5, -75), ylim = c(37, 40),expand = TRUE)+
  xlab("Longitude") + ylab("Latitude") + ggtitle("Chesapeake Bay")+
  theme(panel.background = element_rect(fill = "lightblue")) +
  annotate(geom = "text",x = -76.1,y = 37.8,label = "Chesapeake Bay",color = "brown",size = 3, angle=90) +
  annotation_north_arrow(location = "tl",pad_x = unit(0.5, "cm"),pad_y = unit(1, "cm"),height=unit(1,"cm"),width=unit(0.5,"cm")) + 
  theme(panel.grid.major = element_line(linetype = "dashed", size = 0.2))

plot.01

Upvotes: 1

Related Questions