【问题标题】:R ggplot2 worldmap. breaks in legendR ggplot2 世界地图。传说中的突破
【发布时间】:2012-08-28 09:47:52
【问题描述】:

我正在尝试使用 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

【问题讨论】:

    标签: r map ggplot2 legend


    【解决方案1】:

    那是因为scale_colour_hue() 用于离散刻度。您必须使用scale_fill_gradient(),因为您想更改填充而不是轮廓。

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

    给你。您还把级别放在了没有意义的中断中,推文的数量是数字。

    您可以获取新地图here 并按照该帖子中的说明进行操作。

    【讨论】:

    • 嗨,我已经认为 scale 命令可能存在问题。但不幸的是,这也没有任何区别。这是我的数据158.255.212.46/supersambo/Map.zip (8MB)
    • 尝试在通话中使用 trans = "log"。这样你就可以真正看到 10 和 20 之间的差异。我没有尝试过你的数据。
    • 这是我的结果。 trans="log" 没有区别。 158.255.212.46/supersambo/Rplot.pdf
    • 非常感谢。我犯的另一个错误是将 trans="log" 选项置于中断之后。
    • 对world2问题有什么建议吗?或者从哪里获得更新的世界地图?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-29
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多