0

我的代码:

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))

这段代码应该生成切萨皮克湾的地图,但是当我运行它时,它不会产生任何东西。

4

1 回答 1

1

我认为这是命名空间的一个问题,因为plot它是一个基本的 R 函数。重命名您的对象plot.01或类似的东西可能会解决问题。

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
于 2021-10-19T07:42:28.527 回答