4

I would like to be able to create a world map with the Circles on the graph relevant to the No of Documents.

This I have tried to do using the mapBubbles, but when plotting the graph something goes wrong.

The data file I have is as below:

ISO3V10   Country   No of Documents    Lat  Lon
ARG     Argentina   41          -64 -34
AUS     Australia   224         133 -27
CAN     Canada      426         -95 60
IRL     Ireland 68           -8 53
ITA     Italy             583           12.8333 42.8333
NLD     Netherlands 327          5.75   52.5
NZL     New Zealand 26           174    -41
ESP     Spain             325            -4  40
GBR     United Kingdom  2849             -2 54
USA     United States   3162            -97 38

The code I have written is below:

# Thanks to Andrie for the reproducible code and Paolo for the suggested edit

zz <-"ISO3V10   Country No.of.Documents Lat  Lon
ARG Argentina   41  -64 -34
AUS Australia   224 133 -27
CAN Canada  426 -95 60
IRL Ireland 68  -8  53
ITA Italy   583 12.8333 42.8333
NLD Netherlands 327 5.75    52.5
NZL 'New Zealand' 26  174 -41
ESP Spain   325 -4  40
GBR 'United Kingdom'  2849    -2  54
USA 'United States'   3162    -97 38
"

dF2 <- read.table(textConnection(zz), header = TRUE)

# dF2 <- read.delim(file="C:\\Users\\js207963\\Documents\\noofpublications_AllUpdated.txt", header = TRUE, sep = "\t")
 dF2[]
 par(mai=c(0,0,0.2,0),xaxs="i",yaxs="i")
 mapBubbles(dF2=getMap(), nameZSize="No.of.Documents", nameZColour="Country",oceanCol="lightblue", landCol="wheat", addLegend=FALSE

So the question is can you help me fix the code to plot the data correctly?

Cheers, Jess

Jess Sheasby
  • 41
  • 1
  • 6

2 Answers2

7

You can plot this in ggplot using:

enter image description here

Recreate your data:

dat <- read.table(text="
ISO3V10   Country   'No of Documents'    Lat  Lon
ARG     Argentina   41          -64 -34
AUS     Australia   224         133 -27
CAN     Canada      426         -95 60
IRL     Ireland 68           -8 53
ITA     Italy             583           12.8333 42.8333
NLD     Netherlands 327          5.75   52.5
NZL     'New Zealand' 26           174    -41
ESP     Spain             325            -4  40
GBR     'United Kingdom'  2849             -2 54
USA     'United States'   3162            -97 38
", header=TRUE)

Load packages and plot:

library(ggplot2)
library(maps)

mdat <- map_data('world')

str(mdat)
ggplot() + 
  geom_polygon(dat=mdat, aes(long, lat, group=group), fill="grey50") +
  geom_point(data=dat, 
             aes(x=Lat, y=Lon, map_id=Country, size=`No.of.Documents`), col="red")
Andrie
  • 176,377
  • 47
  • 447
  • 496
  • when I use the code above I get this error message: 'Error in eval(expr, envir, enclos) : object 'long' not found' any idea whats wrong? – Jess Sheasby Jun 19 '12 at 17:15
  • 1
    @JessSheasby I've just run this code in a clean R session and it works for me. I'm sorry, but I don't know what's happening in your case. – Andrie Jun 20 '12 at 06:40
  • I realise this question is old, but in the data I think the lat and lon columns are labeled the wrong way round - you have `lat` values of > 90 & < -90. – SymbolixAU May 07 '17 at 00:29
1

You need to pass a bit more info to the mapBubbles function, since your data frame is not a SpatialPolygonsDataFrame. The following should work (you're Lat and Lon might be mislabeled):

 mapBubbles(dF=dF2, nameZSize="No.of.Documents",
  nameZColour="Country",oceanCol="lightblue", landCol="wheat",
  addLegend=FALSE, nameX = "Lat", nameY = "Lon")

Above, nameX and nameY are passed to the function to indicate where to plot the bubbles. Furthermore, pass the data frame dF2 to the argument dF, instead of a call to getMap().

EDIT:

I also like Andrie's answer with ggplot2 (among other things, the space is better used than in the graphic below), but since you've posted other questions specifically using rworldmap, I thought it appropriate to stick to that package.

World Map with bubbles

BenBarnes
  • 19,114
  • 6
  • 56
  • 74
  • This works great, is there away to remove the legend or make it smaller? As when I zoom in on Europe the legend remains and the values also remanin the same which could cause confussion! – Jess Sheasby Jun 19 '12 at 17:17
  • To avoid plotting the legend for the color coding by country, add `addColourLegend = FALSE` to the arguments for `mapBubbles`. To get the legend smaller, it looks like you might have to tweak the `mapBubbles` function or add the legend after plotting by a call to `addMapLegendBoxes`. – BenBarnes Jun 20 '12 at 05:42
  • @JessSheasby, sorry forgot to include your username so that you get notified. Please see the comment above :) – BenBarnes Jun 20 '12 at 19:12