2012-09-03 64 views
1

我试图在R中使用ggplot2生成一个世界地图。它应该是一个基于国家的热图。我正在处理的数据来自Twitter,我想显示推文的来源。有2个问题:R ggplot2世界地图。在传说中的突破

  1. 集成数据

    map_data("world") 
    

    给了我一张地图年长的20年(苏联)。

    map_data("world2") 
    

    似乎损坏。或者有一些订购问题,但我不知道如何解决它。

http://schloegl.net/supersambo/world2.pdf

  1. 我想改变颜色休息和不知道如何访问它。由于巴西是唯一容易阅读的国家,因此编辑休息时间并显示少于1000条推文的国家之间的差异非常重要。

http://schloegl.net/supersambo/world.pdf

这里是我的代码

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 

回答

1

这是因为scale_colour_hue()是离散的尺度。您必须使用scale_fill_gradient(),因为您要更改填充而不是大纲。

map + scale_fill_gradient(name="Number of\nTweets", trans = "log", 
          breaks = c(10, 20, 100, 200, 1000)) 

你走了。你也在关卡中加入关卡,这是毫无意义的,推文的数量是数字。

enter image description here

你可以得到新的地图here并按照该职位的说明,使其工作。

+0

嗨,我已经认为可能有规模的命令有问题。但不幸的是,这也没有任何区别。这里是我的数据http://158.255.212.46/supersambo/Map.zip(8MB) – supersambo

+0

尝试在通话内部使用trans =“log”。这样你就可以看到10和20之间的差异。我还没有试过你的数据。 –

+0

这是我的结果。 trans =“log”没有区别。 http://158.255.212.46/supersambo/Rplot.pdf – supersambo