如何使用ggplot标记R中的节点

如何使用ggplot标记R中的节点,r,ggplot2,maps,visualization,names,R,Ggplot2,Maps,Visualization,Names,我已经绘制了2014年西班牙劳动力流动图。我已按照此链接修改代码: 现在,我想把城市的名字添加到地图上。有什么想法吗 以下是我使用的代码: # Load the data about the labor flow input <- read.csv("~/Desktop/flujo.csv", sep=";") # Now we need to associate the Spanish regions with geographical coordinates. centroids &

我已经绘制了2014年西班牙劳动力流动图。我已按照此链接修改代码:

现在,我想把城市的名字添加到地图上。有什么想法吗

以下是我使用的代码:

# Load the data about the labor flow
input <- read.csv("~/Desktop/flujo.csv", sep=";")

# Now we need to associate the Spanish regions with geographical coordinates.
centroids <- read.csv("~/Desktop/coordinates.csv", sep=";")

# Join the coordinates with the cities
or.xy<- merge(input, centroids, by.x="origin", by.y="origin")

or.xy$o_name<-or.xy$origin
names(or.xy)<- c("origin", "destination", "trips", "oX", "oY","o_name")

dest.xy<- merge(or.xy, centroids, by.x="destination", by.y="origin")

dest.xy$d_name<-dest.xy$destination

names(dest.xy)<- c("origin", "destination", "trips", "oX", "oY","o_name", "dX", "dY","d_name")


# Now for plotting with ggplot2.This first step removes the axes in the resulting plot.
xquiet<- scale_x_continuous("", breaks=NULL)
yquiet<-scale_y_continuous("", breaks=NULL)
quiet<-list(xquiet, yquiet)

# Let’s build the plot. First we specify the dataframe we need, with a filter excluding flows of <10

mapa<-ggplot(dest.xy[which(dest.xy$trips>1),], aes(oY,oX))+

  # The next line tells ggplot that we wish to plot line segments. The “alpha=” is line transparency and used below

  geom_segment(aes(x=oX, y=oY,xend=dX, yend=dY, alpha=trips), col="white")+

  # Here is the magic bit that sets line transparency – essential to make the plot readable

scale_alpha_continuous(range = c(0.07,0.07))+

  # Set black background, remove axes and fix aspect ratio

  theme(panel.background = element_rect(fill="black",colour='black'))+quiet+coord_equal()
#加载有关劳动力流的数据

我们需要一个输入来帮助您,但基本上,您必须添加一个
geom_文本(aes(label=cityname,x=city\u x\u coord,y=city\u y\u coord))
层非常感谢您的回答!如果我将您转发到原始示例(我的是一份包含其他数据的副本),您可能会更容易:在那里,您可以使用原始数据(或其中的一部分),然后标记示例中使用的一个城市!此页上链接的文件太重。请提供一个最小的数据集:只需发布dput的输出(head(dest.xy))
好的,非常感谢您的回答!这里是调整后的代码:
library(plyr)library(ggplot2)library(map tools)
只要您不提供简单的方法让我们获得相同的数据,并且在运行代码时出现相同的错误,您的代码是可复制的。请仔细阅读我在第一条评论中给出的链接的第一个答案,并相应地调整您的问题。我们需要一个帮助您的链接,但基本上,您必须添加一个
geom_文本(aes(label=cityname,x=city_x_coord,y=city_y_coord))
层非常感谢您的回答!如果我将您转发到原始示例(我的是一份包含其他数据的副本),您可能会更容易:在那里,您可以使用原始数据(或其中的一部分),然后标记示例中使用的一个城市!此页上链接的文件太重。请提供一个最小的数据集:只需发布dput的输出(head(dest.xy))好的,非常感谢您的回答!这里是调整后的代码:
library(plyr)library(ggplot2)library(map tools)
只要您不提供简单的方法让我们获得相同的数据,并且在运行代码时出现相同的错误,您的代码是可复制的。请仔细阅读我在第一次评论中给出的链接的第一个答案,并相应地修改您的问题