我想用geom_point制作谷歌地图。我有两个男女收入colomns,我想在同一张地图上一起展示。
state capital_name lat long women earnings men earning
1 Alabama Montgomery 32.36154 -86.27912 621 832
2 Alaska Juneau 58.30194 -134.41974 797 1008
3 Arizona Phoenix 33.44846 -112.07384 669 827
4 Arkansas Little Rock 34.73601 -92.33112 610 703
正如你所看到的,在相同的数字中,我现在有女性收入和男性收入。我希望它们一起显示在地图中。
我尝试将一个加号(+)加到代码上,然后在resolt上加上它。这在我的代码中:
mapPoints <-
#the woman:
ggmap(map) + geom_point(aes(x = long, y = lat, size =
sqrt(women_median_weekly_earnings)), data = join2, alpha = .5
,color="darkred")+scale_size(range=c(3,20))
#the men:
+ ggmap(map) + geom_point(aes(x = long, y = lat, size =
sqrt(men_median_weekly_earnings)), data = join2, alpha = .5
,color="blue")+scale_size(range=c(3,20))
我有一个错误问题如下:
Error in add_ggplot(e1, e2, e2name) :
argument "e2" is missing, with no default
他们每个人分开工作但不在一起工作。
女性:enter image description here
男人:enter image description here
另外,也许geom_point不太好?我看不到美国的状态。在地图上拍摄这些数据可能还有其他一些优势吗?列\吧可能吗?
感谢。
答案 0 :(得分:2)
这样的事情:
df <- read.table(text = "state capital_name lat long women_earnings men_earning
Alabama Montgomery 32.36154 -86.27912 621 832
Alaska Juneau 58.30194 -134.41974 797 1008
Arizona Phoenix 33.44846 -112.07384 669 827
Arkansas Little_Rock 34.73601 -92.33112 610 703", header = T)
## I'm going to reshape the data into long-form so it's easier to work with
library(reshape2)
df <- melt(df, id.vars = c("state", "capital_name", "lat", "long"), value.name = "earnings")
ggmap(map) +
geom_point(aes(x = long, y = lat, size = sqrt(earnings), colour = variable),
data = df, alpha = .5) +
scale_colour_manual(values = c("red", "darkblue")) +
scale_size(range=c(3,20), guide = 'none')
如果您想要交互式地图,可以选择一些选项,尤其是leaflet
和googleway
。他们如何接受&#39;数据,但它没有太多的操纵。
library(leaflet)
colourPalette <- colorFactor(c("red", "darkblue"), unique(df$variable))
df %>%
leaflet() %>%
addTiles() %>%
addCircles(radius = ~(earnings * 500), stroke = FALSE,
fillColor = ~colourPalette(variable)) %>%
addLegend(pal = colourPalette, values = unique(df$variable))
library(googleway)
mapKey <- 'your_api_key'
## the colours and radius are required on the data itself
df_colours <- data.frame(variable = unique(df$variable),
colour = c("red", "darkblue"))
df <- merge(df, df_colours, by = "variable")
df$radius <- df$earnings * 500
google_map(key = mapKey) %>%
add_circles(data = df, fill_colour = "colour", radius = "radius")
答案 1 :(得分:1)
我采用了SymbolixAU的方法,但是这里的一些代码只对您问题中列出的方法进行了微小的更改。
library(ggmap)
library(RCurl)
library(dplyr)
# Create dataframe
join_2 <-
tibble::tribble(
~state, ~capital_name, ~lat, ~long, ~women_earning, ~men_earning,
"Alabama", "Montgomery", 32.36154, -86.27912, 621, 832,
"Alaska", "Juneau", 58.30194, -134.41974, 797, 1008,
"Arizona", "Phoenix", 33.44846, -112.07384, 669, 827,
"Arkansas", "Little Rock",34.73601, -92.33112, 610, 703)
# Get map
map <- get_map(location = c(mean(join_2$long),mean(join_2$lat)), zoom = 3)
#> Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=39.711987,-106.275955&zoom=3&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
# Produce map
ggmap(map) +
geom_point(aes(x = long, y = lat, size = sqrt(women_earning)),
data = join_2, alpha = .5, color="darkred") +
geom_point(aes(x = long, y = lat, size = sqrt(men_earning)),
data = join_2, alpha = .5,color="blue") +
scale_size_continuous(range = c(3,20))