world map?

68 views
Skip to first unread message

Ignacio Martinez

unread,
Nov 19, 2013, 10:38:53 AM11/19/13
to ggp...@googlegroups.com
I have a data frame that tells me the country of origin for each user:

    country <- structure(list(anon_user_id = 1:887, country = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 8L, 8L, 8L, 9L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
    11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 16L, 18L, 18L, 19L, 
    19L, 19L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 
    26L, 26L, 26L, 26L, 26L, 28L, 28L, 34L, 34L, 34L, 34L, 34L, 34L, 
    34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 
    34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 
    34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 38L, 39L, 
    39L, 40L, 40L, 46L, 47L, 49L, 50L, 50L, 50L, 54L, 54L, 56L, 61L, 
    61L, 61L, 61L, 65L, 65L, 65L, 65L, 69L, 69L, 69L, 69L, 69L, 69L, 
    69L, 69L, 69L, 69L, 69L, 69L, 69L, 69L, 69L, 69L, 71L, 71L, 71L, 
    71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 73L, 73L, 80L, 
    80L, 83L, 83L, 83L, 83L, 83L, 83L, 83L, 83L, 83L, 84L, 85L, 87L, 
    87L, 87L, 87L, 87L, 88L, 88L, 88L, 89L, 89L, 89L, 89L, 89L, 93L, 
    94L, 102L, 102L, 102L, 114L, 121L, 121L, 121L, 121L, 121L, 121L, 
    121L, 121L, 121L, 121L, 132L, 132L, 132L, 132L, 132L, 132L, 132L, 
    132L, 132L, 132L, 132L, 134L, 134L, 134L, 134L, 139L, 139L, 147L, 
    148L, 148L, 148L, 148L, 148L, 148L, 148L, 149L, 150L, 150L, 152L, 
    152L, 152L, 152L, 152L, 152L, 153L, 153L, 153L, 153L, 153L, 153L, 
    153L, 161L, 163L, 163L, 163L, 163L, 163L, 163L, 166L, 168L, 168L, 
    172L, 172L, 172L, 172L, 172L, 172L, 172L, 173L, 175L, 175L, 175L, 
    175L, 175L, 175L, 175L, 175L, 175L, 175L, 175L, 175L, 175L, 175L, 
    175L, 175L, 175L, 175L, 175L, 175L, 176L, 180L, 180L, 181L, 186L, 
    186L, 186L, 192L, 192L, 196L, 196L, 196L, 196L, 196L, 198L, 198L, 
    198L, 198L, 198L, 198L, 198L, 198L, 198L, 198L, 198L, 198L, 198L, 
    198L, 198L, 198L, 198L, 198L, 198L, 198L, 198L, 198L, 198L, 198L, 
    198L, 198L, 198L, 198L, 198L, 198L, 198L, 198L, 198L, 198L, 198L, 
    198L, 198L, 198L, 198L, 198L, 198L, 198L, 198L, 198L, 198L, 198L, 
    198L, 198L, 198L, 198L, 198L, 198L, 198L, 198L, 198L, 198L, 198L, 
    198L, 198L, 198L, 198L, 198L, 198L, 198L, 198L, 198L, 198L, 198L, 
    198L, 198L, 198L, 198L, 198L, 198L, 198L, 198L, 199L, 199L, 200L, 
    202L), .Label = c("United States", "Afghanistan", "Albania", 
    "Algeria", "Andorra", "Angola", "Antigua and Barbuda", "Argentina", 
    "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahamas, The", 
    "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", 
    "Benin", "Bhutan", "Bolivia", "Bosnia and Herzegovina", "Botswana", 
    "Brazil", "Brunei", "Bulgaria", "Burkina Faso", "Burma", "Burundi", 
    "Cambodia", "Cameroon", "Canada", "Cape Verde", "Central African Republic", 
    "Chad", "Chile", "China", "Colombia", "Comoros", "Congo, Democratic Republic of the", 
    "Congo, Republic of the", "Costa Rica", "Cote d'Ivoire", "Croatia", 
    "Cuba", "Curacao", "Cyprus", "Czech Republic", "Denmark", "Djibouti", 
    "Dominica", "Dominican Republic", "East Timor (see Timor-Leste)", 
    "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", 
    "Estonia", "Ethiopia", "Fiji", "Finland", "France", "Gabon", 
    "Gambia, The", "Georgia", "Germany", "Ghana", "Greece", "Grenada", 
    "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Holy See", 
    "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", 
    "Iran", "Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", 
    "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Korea, North", 
    "Korea, South", "Kosovo", "Kuwait", "Kyrgyzstan", "Laos", "Latvia", 
    "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", 
    "Luxembourg", "Macau", "Macedonia", "Madagascar", "Malawi", "Malaysia", 
    "Maldives", "Mali", "Malta", "Marshall Islands", "Mauritania", 
    "Mauritius", "Mexico", "Micronesia", "Moldova", "Monaco", "Mongolia", 
    "Montenegro", "Morocco", "Mozambique", "Namibia", "Nauru", "Nepal", 
    "Netherlands", "Netherlands Antilles", "New Zealand", "Nicaragua", 
    "Niger", "Nigeria", "North Korea", "Norway", "Oman", "Pakistan", 
    "Palau", "Palestinian Territories", "Panama", "Papua New Guinea", 
    "Paraguay", "Peru", "Philippines", "Poland", "Portugal", "Qatar", 
    "Romania", "Russia", "Rwanda", "Saint Kitts and Nevis", "Saint Lucia", 
    "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Sao Tome and Principe", 
    "Saudi Arabia", "Senegal", "Serbia", "Seychelles", "Sierra Leone", 
    "Singapore", "Sint Maarten", "Slovakia", "Slovenia", "Solomon Islands", 
    "Somalia", "South Africa", "South Korea", "South Sudan", "Spain", 
    "Sri Lanka", "Sudan", "Suriname", "Swaziland", "Sweden", "Switzerland", 
    "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "Timor-Leste", 
    "Togo", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", 
    "Turkmenistan", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", 
    "United Kingdom", "Uruguay", "Uzbekistan", "Vanuatu", "Venezuela", 
    "Vietnam", "Yemen", "Zambia", "Zimbabwe"), class = "factor")), .Names = c("anon_user_id", 
    "country"), row.names = c(NA, -887L), class = "data.frame")

I would like to plot a map of the world that shows how many users I have in each country? How can I do this with ggplot2?

Thanks!

Ignacio Martinez

unread,
Nov 19, 2013, 5:03:45 PM11/19/13
to ggp...@googlegroups.com
I made some progress, but not with ggplot2

countries<-table(country$country)
countries<-countries[countries>0]
library(maps)
map(database="world")
map(database="world", col=countries, fil=countries)
legend("topleft", fill = countries, legend = countries, col = countries)
box()

The output looks really bad. I would love to be able to get this done with ggplot2. 

Thanks for the help

Dennis Murphy

unread,
Nov 19, 2013, 7:28:25 PM11/19/13
to Ignacio Martinez, ggplot2
(1) Start here: http://docs.ggplot2.org/current/ . Scroll down to the
section headed 'Fortify' and look at the three related functions for
converting spatial data frames or map files into a data frame object
that ggplot2 can handle. Also look at the help for geom_map().
(2) Look into the ggmap package by Dr. David Kahle, documented here:
journal.r-project.org/archive/2013-1/kahle-wickham.pdf
(3) Do a Google search on 'world map ggplot2'; there are more than a few hits.
(4) Do a search of the archives of this group; there are also more
than a few hits.

That should be enough to get you started on the way to graphing
map-related data in ggplot2.

Dennis
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Ignacio Martinez

unread,
Nov 20, 2013, 10:31:46 AM11/20/13
to Dennis Murphy, ggplot2
Thanks!

I can make a dynamic map using but I'm having troubles with making an static map using ggplot2. For example, with ggplot2 it looks like i have no one in the US.
This is my code

#Load My data
countries <- structure(list(country = c("United States", "Afghanistan", "Albania", 
                                        "Argentina", "Armenia", "Australia", "Austria", "Bahrain", "Bangladesh", 
                                        "Belarus", "Belgium", "Bosnia and Herzegovina", "Brazil", "Bulgaria", 
                                        "Canada", "Chile", "China", "Colombia", "Croatia", "Cuba", "Cyprus", 
                                        "Czech Republic", "Denmark", "Dominican Republic", "Ecuador", 
                                        "Egypt", "El Salvador", "Estonia", "Finland", "France", "Germany", 
                                        "Greece", "Guatemala", "Haiti", "Hong Kong", "Hungary", "Iceland", 
                                        "India", "Indonesia", "Iran", "Ireland", "Israel", "Italy", "Japan", 
                                        "Jordan", "Kazakhstan", "Kenya", "Korea, South", "Latvia", "Libya", 
                                        "Lithuania", "Macedonia", "Malaysia", "Malta", "Mexico", "Moldova", 
                                        "Morocco", "Netherlands", "New Zealand", "Nicaragua", "Niger", 
                                        "Nigeria", "Norway", "Pakistan", "Panama", "Peru", "Philippines", 
                                        "Poland", "Portugal", "Romania", "Russia", "Saudi Arabia", "Serbia", 
                                        "Singapore", "Slovakia", "Slovenia", "Somalia", "South Africa", 
                                        "South Korea", "Spain", "Sri Lanka", "Sweden", "Switzerland", 
                                        "Taiwan", "Thailand", "Turkey", "Ukraine", "United Arab Emirates", 
                                        "United Kingdom", "Uruguay", "Uzbekistan", "Venezuela", "Zimbabwe"
), count = c(1224L, 1L, 1L, 4L, 2L, 40L, 2L, 1L, 2L, 5L, 8L, 
             2L, 40L, 3L, 106L, 4L, 16L, 10L, 8L, 4L, 2L, 5L, 4L, 5L, 3L, 
             1L, 2L, 5L, 1L, 10L, 26L, 41L, 3L, 1L, 3L, 2L, 1L, 34L, 2L, 3L, 
             10L, 4L, 19L, 1L, 1L, 1L, 1L, 1L, 4L, 1L, 3L, 1L, 2L, 2L, 36L, 
             1L, 1L, 31L, 10L, 1L, 1L, 1L, 2L, 6L, 2L, 3L, 29L, 7L, 11L, 13L, 
             21L, 5L, 9L, 6L, 3L, 2L, 1L, 22L, 2L, 42L, 1L, 3L, 5L, 2L, 6L, 
             5L, 13L, 2L, 157L, 4L, 1L, 5L, 1L)), .Names = c("country", "count"
             ), row.names = c(NA, -93L), class = "data.frame")

#Make dynamic map
library(googleVis)
# Make the map!
geoMap <- gvisGeoMap(countries, locationvar="country", numvar="count",
                     options=list(dataMode="regions"))
plot(geoMap)

#Make ggplot2 map
library(maps)
library(ggplot2)
#load world data
world <- map_data("world")

#Delete Antarctica
world <- subset(world,region!="Antarctica")
#Add count
world$count<-countries$count[match(world$region,countries$country,nomatch=NA)]
qplot(long, lat, data = world, group = group, fill=count, geom ="polygon",ylab="",xlab="")

Why is the ggplot2 map wrong? How can I fix it?

Thanks!

Ista Zahn

unread,
Nov 20, 2013, 11:00:12 AM11/20/13
to Ignacio Martinez, Dennis Murphy, ggplot2
The country names don't match the region names, e.g., "USA" vs "United
States". Here is the list of countries that don't match region:

unique(countries$country)[!unique(countries$country) %in% unique(world$region)]

If I were you I would probably just fix them by hand.

Best,
Ista
Reply all
Reply to author
Forward
0 new messages