【问题标题】:Plotting map in R but it is showing nothing在R中绘制地图,但它什么也没显示
【发布时间】:2021-10-19 07:33:47
【问题描述】:

我的代码:

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

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

【问题讨论】:

  • 如果你不打电话给plot,这肯定不会产生情节。你的代码对我来说很好用
  • 我调用 plot 得到这个:function (x, y, ...) UseMethod("plot")
  • 这是plot函数,通常我们不给存储对象函数名称,我建议你更改存储对象图的名称

标签: r ggplot2 sf ggspatial


【解决方案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

【讨论】:

    猜你喜欢
    • 2013-07-16
    • 2017-05-03
    • 2018-02-14
    • 2014-02-13
    • 1970-01-01
    • 2020-03-09
    • 1970-01-01
    • 2022-07-22
    • 2012-03-29
    相关资源
    最近更新 更多