簡體   English   中英

R ggplot2世界地圖。 打破傳奇

[英]R ggplot2 worldmap. breaks in legend

我正在嘗試在R中使用ggplot2生成世界地圖。它應該是基於國家/地區的熱圖。 我正在處理的數據來自twitter,我想顯示這些tweet的來源。 有兩個問題:

  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

這是因為scale_colour_hue()用於離散比例。 您必須使用scale_fill_gradient()因為您要更改填充而不是輪廓。

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

妳去 您還可以將級別放入中斷中,這是沒有意義的,tweet的數量是數字。

在此處輸入圖片說明

您可以在此處獲取新地圖並按照該帖子中的說明進行操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM