使用两个数据集相同的地图R ggmap

时间:2015-03-05 21:48:50

标签: r ggplot2 ggmap

我想在ggmap的同一个地图上设置两个不同的数据集。问题是,当我尝试叠加两个数据集时,它会一直擦除第一个数据集。知道如何解决这个问题吗?

数据集1例如是一个国家/地区的每个城市的专利数量,而数据集2将是同一国家/地区的大学数量。

我的代码是:

    map.ma <- get_map(location = 'Massachusetts', zoom = 8, color="bw")
    mapPoints <- ggmap(map.ma) + geom_point(aes(x = lon, y = lat, size = (sbir.cities1$SBIR)), data = sbir.cities1, alpha = .5, color="red")
    mapPoints2 <- mapPoints + scale_size_area(breaks = (c(1, 5, 10, 50, 100, 500)), labels = c(1, 5, 10, 50, 100, 500), name = "SBIR")
    mapPoints3 <- mapPoints2 + geom_point(aes(x = lon, y = lat, size = sqrt(ma.universities1$Enrollment)), data = ma.universities1, alpha = .5, color="blue")
    mapPoints4 <- mapPoints3 + scale_size_area(breaks = sqrt(c(5000, 10000, 15000, 20000, 25000, 30000)), labels = c(5000, 10000, 15000, 20000, 25000, 30000), name = "Enrollment")
    mapPoints4

我的数据集是:

    structure(list(X = c(1L, 3L, 4L, 5L, 6L), name = c("Acton", "Amesbury", 
"Amherst", "Andover", "Arlington"), SBIR = c(7L, 2L, 13L, 265L, 
6L), country.etc = structure(c(1L, 1L, 1L, 2L, 2L), .Label = c("#N/A", 
"MA"), class = "factor"), pop = structure(c(1L, 1L, 1L, 24L, 
10L), .Label = c("#N/A", "101342", "103085", "133313", "152095", 
"177973", "40063", "40441", "40565", "41246", "41520", "44473", 
"44690", "45127", "50698", "53579", "55066", "55862", "567759", 
"57377", "58407", "60784", "65103", "67715", "74802", "84969", 
"93128", "94039", "94945"), class = "factor"), lati = c(42.29, 
42.51, 42.23, 42.65, 42.42), long = c(-71.26, -70.55, -72.31, 
-71.14, -71.16), capital = structure(c(1L, 1L, 1L, 2L, 2L), .Label = c("#N/A", 
"0", "2"), class = "factor"), geocode = c("Acton Massachusetts", 
"Amesbury Massachusetts", "Amherst Massachusetts", "Andover Massachusetts", 
"Arlington Massachusetts"), lon = c(-71.43284, -70.9300376, -72.517, 
-71.1367953, -71.1564729), lat = c(42.4850931, 42.8583925, 42.367, 
42.6583356, 42.4153925)), .Names = c("X", "name", "SBIR", "country.etc", 
"pop", "lati", "long", "capital", "geocode", "lon", "lat"), row.names = c(89L, 
128L, 72L, 135L, 123L), class = "data.frame")

    structure(list(School = c("Boston University", "University of Massachusetts Amherst", 
"Harvard University", "Northeastern University", "University of Massachusetts Lowell"
), Location = structure(c(5L, 1L, 11L, 5L, 28L), .Label = c("Amherst", 
"Andover", "Bedford", "Beverly", "Boston", "Bourne", "Bridgewater", 
"Brighton", "Brockton", "Brookline", "Cambridge", "Chestnut Hill", 
"Chicopee", "Conway", "Dartmouth", "Dudley", "Easton", "Fall River", 
"Fitchburg", "Framingham", "Franklin", "Gardner", "Great Barrington", 
"Greenfield", "Haverhill", "Holyoke", "Longmeadow", "Lowell", 
"Lynn", "Medford", "Milton", "Needham", "Newton", "North Adams", 
"North Andover", "Northampton", "Norton", "Paxton", "Pittsfield", 
"Quincy", "Roxbury", "Salem", "South Hadley", "South Hamilton", 
"Springfield", "Swampscott", "Turners Falls", "Waltham", "Wellesley", 
"Wenham", "West Barnstable", "Westfield", "Weston", "Williamstown", 
"Worcester"), class = "factor"), Control = structure(c(2L, 3L, 
2L, 2L, 3L), .Label = c("For-profit", "Private", "Public"), class = "factor"), 
    Type = structure(c(7L, 7L, 7L, 7L, 7L), .Label = c("Associate\325s college", 
    "Baccalaureate college", "Baccalaureate/associate's college", 
    "Master's", "Master's university", "Master\325s university", 
    "Research university", "Special-focus institution"), class = "factor"), 
    Enrollment = c(32603L, 28518L, 28147L, 20053L, 16932L), geocode = c("Boston University Massachusetts", 
    "University of Massachusetts Amherst Massachusetts", "Harvard University Massachusetts", 
    "Northeastern University Massachusetts", "University of Massachusetts Lowell Massachusetts"
    ), lon = c(-71.1053991, -72.5267121, -71.1166601, -71.0891717, 
    -71.3340665), lat = c(42.3504997, 42.3911569, 42.3770029, 
    42.3398067, 42.6426529)), .Names = c("School", "Location", 
"Control", "Type", "Enrollment", "geocode", "lon", "lat"), row.names = c(NA, 
5L), class = "data.frame")

0 个答案:

没有答案