Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中标记地图中的数据点_R_Ggplot2 - Fatal编程技术网

在R中标记地图中的数据点

在R中标记地图中的数据点,r,ggplot2,R,Ggplot2,我已经创建了一个绘制数据点的地图,我想: 城市名称更为明显,或将其上移或下移,以便 标记数据点1-7,使数字显示在顶部。 以下是我的数据集和我所做的工作: library("ggplot2") theme_set(theme_bw()) library("sf") library("rnaturalearth") library("rnaturalearthdata") stations <- data.frame(longitude = c(-58,-54,-50,-44.95,-40

我已经创建了一个绘制数据点的地图,我想:

城市名称更为明显,或将其上移或下移,以便 标记数据点1-7,使数字显示在顶部。 以下是我的数据集和我所做的工作:

library("ggplot2")
theme_set(theme_bw())
library("sf")
library("rnaturalearth")
library("rnaturalearthdata")

stations <- data.frame(longitude = c(-58,-54,-50,-44.95,-40,-35.87,-31), latitude = c(22,22,22,23.367,23,22.33,22))

world <- ne_countries(scale = "medium", returnclass = "sf")
class(world)

world_points<- st_centroid(world)
world_points <- cbind(world, st_coordinates(st_centroid(world$geometry)))

a <- world_points[50,]
b <- world_points[34,]
cities <- rbind(a,b)

ggplot(data = world) +
    geom_sf() +
  geom_text(data= cities,aes(x=X, y=Y, label=name),
    color = "black", fontface = "bold", check_overlap = FALSE) +
annotate(geom = "text", x = -90, y = 26, label = "Gulf of Mexico", 
    fontface = "italic", color = "grey22", size = 6) +
    geom_point(data = stations, aes(x = longitude, y = latitude), size = 4, 
        shape = 21, fill = "red") +
    coord_sf(xlim = c(-80, -10), ylim = c(10, 30), expand = FALSE)
使用ggrepel,可以使城市名称稍微偏离其位置。要标记桩号,可以使用参数标签添加几何图形文字

总之,它可以是这样的:

图书馆排斥 ggplotdata=世界+ geom_sf+ geom_text_repeldata=城市,aesx=X,y=y,label=name, 颜色=黑色,字体=粗体+ 注释GEOM=文本,x=-90,y=26,标签=墨西哥湾, 字体面=斜体,颜色=灰色22,大小=6+ geom_pointdata=站点,aesx=经度,y=纬度,label=1:7,size=4, 形状=21,填充=红色+ coord_sfxlim=c-80,-10,ylim=c10,30,expand=FALSE+ geom_textdata=站点,aesx=经度,y=纬度,标签=1:7,尺寸=4, 形状=21,填充=红色,vjust=-1 下面是一种使用ggrepel包中的geom_text_repel的方法

library("rgeos")
library("ggrepel")
ggplot(data = world) + geom_sf() + 
  geom_text_repel(data= cities,aes(x=X, y=Y, label=name), fontface = "bold",nudge_x = c(-5,5), nudge_y = c(-3,5)) +
  geom_point(data = stations, aes(x = longitude, y = latitude), size = 4, shape = 21, fill = "red") +  
  geom_text_repel(data= stations,aes(x=longitude, y=latitude, label=1:7), fontface = "bold",nudge_x = 1, nudge_y = 1) +
  coord_sf(xlim = c(-80, -10), ylim = c(10, 30), expand = FALSE)

哇,我花了这么长时间,迟到了11秒。打得好,哈哈哈;我在另一篇文章中也遇到了同样的情况。我想用轻推部分改进geom_text_Refel,但你先得到它,所以我们称之为平局:谢谢各位,非常有帮助!