I´m trying to produce a worldmap using ggplot2 in R. It schould be a countrybased heatmap. The data I’m working on comes from twitter and I want to show where the tweets come from. There are 2 problems:
-
the integrated data
map_data("world")gives me a map older the 20 years (USSR).
map_data("world2")seems damaged. or there is some ordering issue but I don’t know how to solve it.
http://schloegl.net/supersambo/world2.pdf
- I would like to change the colour breaks and don’t know how to access it. Since Brazil is the only country easy to read it would be important to edit the breaks and show the difference between countries with less then 1000 tweets.
http://schloegl.net/supersambo/world.pdf
here is my code
WD <- getwd()
if (!is.null(WD)) setwd(WD)
library(maps)
library(plyr)
library(ggplot2)
twitter=read.csv("/Users/stephanschloegl/Studium/Diplomarbeit/rawData/c_userInfo/c_userInfo.csv",header=TRUE,check.names=FALSE,sep=";")
#read geodata
cities=read.csv("GeoWorldMap/cities.txt",header=TRUE,check.names=FALSE,sep=",")
countries=read.csv("GeoWorldMap/countries.txt",header=TRUE,check.names=FALSE,sep=",")
#find countries for twitter$timezone
lista <- twitter$time_zone
country_ids <- cities$CountryID[match(lista,cities$City)]
country <- countries$Country[match(country_ids,countries$CountryId)]
#FREQENCIES
frequencies <- as.data.frame(table(country))
names(frequencies) <- c("region","freq")
#change 0's to NA
frequencies$freq[frequencies$freq==0] <- NA
#load world data
world <- map_data("world2")
#Delete Antarctica
world <- subset(world,region!="Antarctica")
#merge twitterdata and geodata
world$tweets <- frequencies$freq[match(world$region,frequencies$region,nomatch=NA)]
map <- qplot(long, lat, data = world, group = group,fill=tweets,geom ="polygon",ylab="",xlab="")
#this does'nt work
map + scale_colour_hue(name="Number of\nTweets",breaks=levels(c(10,20,100,200,1000)))
map
That’s because
scale_colour_hue()is for discrete scales. You have to usescale_fill_gradient()because you want to change the fill not the outline.There you go. You have also put levels in the breaks which makes no sense, the number of tweets is numeric.
You can get the new map here and follow the instructions in that post to make it work.