1

I have created a map of the labor mobility in Spain in 2014. I have followed this link to adapt the code: http://www.r-bloggers.com/mapping-flows-in-r/

Now, I would like to add the names of the cities to the map. Any ideas?

Here is the code I used:

# 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()
  • we need a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to help you, but basically, you will have to add a `geom_text(aes(label=cityname,x=city_x_coord,y=city_y_coord))` layer – scoa Jul 09 '15 at 15:10
  • Thank you very much for your answer! It will probably easier for you if I forward you to the original example (mine is a copy with other data): http://www.r-bloggers.com/mapping-flows-in-r/ There you can use the original data (or part of it) and then label one of the cities used in the example! – alphabetagamma Jul 11 '15 at 18:24
  • The file linked on this page is too heavy. Please provide a minimal dataset : just post the output of `dput(head(dest.xy))` – scoa Jul 11 '15 at 20:48
  • OK, thank you very much for the answer! Here the adapted code: `library(plyr) library(ggplot2) library(map tools)` – alphabetagamma Jul 25 '15 at 08:32
  • You code is note reproducible as long as you don't provide a simple way for us to get the same data and have the same error when we run your code. Please read carefully the first answer to the link I gave in my first comment and adapt your question accordingly http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – scoa Jul 25 '15 at 10:51

0 Answers0