2

I would like to be able to plot my own values for a hand full of countries. For Example: China, United States, United Kingdom, Canada and Russia.

I have my own txt file that has 3 columns - ISO3V10, Country and No of Documents.

ISO3V10 Country              No of Documents
CAN     Canada               30
CHN     China                20
RUS     Russia               10
GBR     United Kingdom       38
USA     United States        50

The idea would be to have a world map coloured in for the Country and the data being plotted is No of Documents.

So far I have done this:

myData2 <- read.delim("noofdocuments.txt",header=T, sep='\t')
names(myData2)
myData2[]

jessdata <- data.frame(myData2=c("China", "United States", "United Kingdom", 
                                 "Russia", "Canada"))
sPDF <- joinCountryData2Map(jessdata, 
                            joinCode = "NAME", 
                            nameJoinColumn = "myData2")
par(mai=c(0,0,0.2,0),xaxs="i",yaxs="i")
mapCountryData(sPDF, nameColumnToPlot="REGION")

Ideally I would like sPDF to be:

sPDF <- joinCountryData2Map(countryExData, 
                            joinCode = "ISO3", nameJoinColumn = "ISO3V10")

Also for REGION to be:

mapCountryData(sPDF, nameColumnToPlot="No.of.Documents")

I have tried all the ways possible to do this, which is why I have REGION as nameColumnToPlot as this is the only way I can get it too work.

Would someone be able to tell me where I have gone wrong in the code?

joran
  • 169,992
  • 32
  • 429
  • 468
Jess Sheasby
  • 51
  • 1
  • 3
  • 2
    Your example is not self contained, you should add "library(rworldmap)" at the beginning. Also, you could use the function dput() on myData2 to make it easier for readers to try you exemple. – Arnaud A May 03 '12 at 02:21

1 Answers1

4

If the following code works for you, then there may be a problem with the format of your text file or the way that it is read into R.

library(rworldmap)

countryExData<-read.table(text="
ISO3V10\tCountry\tNo of Documents
CAN\tCanada\t30
CHN\tChina\t20
RUS\tRussia\t10
GBR\tUnited Kingdom\t38
USA\tUnited States\t50"
,sep="\t",header=TRUE)

# > countryExData
#   ISO3V10        Country No.of.Documents
# 1     CAN         Canada              30
# 2     CHN          China              20
# 3     RUS         Russia              10
# 4     GBR United Kingdom              38
# 5     USA  United States              50

sPDF <- joinCountryData2Map(countryExData, 
                            joinCode = "ISO3", nameJoinColumn = "ISO3V10")
# 5 codes from your data successfully matched countries in the map
# 0 codes from your data failed to match with a country code in the map
# 241 codes from the map weren't represented in your data

par(mai=c(0,0,0.2,0),xaxs="i",yaxs="i")
mapCountryData(sPDF, nameColumnToPlot="No.of.Documents")

World map with number of docs

If that worked, you should examine your countryExData object (or myData2? it's unclear from your post) for differences between it and the above object. If you do not discover anything amiss, please post the result of dput(head(countryExData)) in your original post.

BenBarnes
  • 19,114
  • 6
  • 56
  • 74