将 legend/text table 添加到具有多层的 ggmap
Adding a legend/text table to a ggmap with multiple layers
我正在 ggmap 上绘制路线和点,我需要添加一个图例来列出这些点代表的名称
即
1 比尔·哈里斯,
2 安妮·琼斯
等..
我准备了示例数据。
structure(list(business.names = structure(c(8L, 9L, 5L, 6L, 7L,
1L, 4L, 3L, 2L, 10L), .Label = c("Bill Harris", "Anne Jones",
"Sue Perkins", "Morris Jones", "Bert Harris", "Bob Jones",
"Mike Taylor", "Paul Jones", "John Roberts",
"Fred Morris"), class = "factor"), X1 = structure(c(7L,
8L, 10L, 9L, 3L, 2L, 1L, 4L, 5L, 6L), .Label = c("53.186744",
"53.1884117", "53.1894004", "53.189836", "53.1902965", "53.1905138631287",
"53.1912196", "53.1916771", "53.193418", "53.1934748"), class = "factor"),
X2 = structure(c(7L, 3L, 2L, 1L, 5L, 4L, 6L, 10L, 9L, 8L), .Label = c("-2.881248",
"-2.8814698", "-2.8846099", "-2.88802", "-2.8886692", "-2.890165",
"-2.8902453", "-2.89043889005541", "-2.8919373", "-2.893896"
), class = "factor")), .Names = c("business.names", "X1",
"X2"), row.names = c(10L, 9L, 8L, 1L, 7L, 6L, 3L, 4L, 5L, 2L), class = "data.frame")
这是我迄今为止的代码。请注意,对于 geom_path 和 geom.route,大小和颜色参数在 AES 函数之外。我不需要根据数据调整尺寸或颜色。如果我将大小和颜色放入 AES 函数中,我会得到一个图例,但这不是我想要的。它使用的是点数据而不是商家名称。
我什至不确定传奇是否是最好的选择。我只需要以简单的方式 table.
显示在图表上的数据
create.map<-function(lst){
require("ggmap")
require("qmap")
way.points<-data.frame(lapply(lst[,1:3], as.character), stringsAsFactors=FALSE)
rte.from <- apply(way.points[-nrow(way.points),2:3],1,paste,collapse=",")
rte.to <- apply(way.points[-1,2:3],1,paste,collapse=",")
rte <- do.call(rbind,
mapply(route, rte.from, rte.to, SIMPLIFY=FALSE,
MoreArgs=list(mode="walking",structure="legs")))
map.centre<-c(mean(as.numeric(way.points$X2)),mean(as.numeric(way.points$X1)))
coords <- rbind(as.matrix(rte[,7:8]),as.matrix(rte[nrow(rte),9:10]))
coords <- as.data.frame(coords)
ggm <- qmap(location=map.centre,zoom = 15, maptype = "road", legend="topright")
ggm +
geom_path(data=coords,aes(x=startLon,y=startLat),color="blue",size=2)+
geom_point(data=way.points,aes(x=as.numeric(X2),y=as.numeric(X1)),
size=10,color="yellow")+
geom_text(data=way.points,
aes(x=as.numeric(X2),y=as.numeric(X1), label=seq_along(X1)))
}
这是一种可能。您可能想要排列公司名称并再次使用 geom_point
来创建图例。在这个 geom_point
中,您使用 alpha = 0
。因此,您实际上看不到任何要点;您只会看到第一个 geom_point
中指定的黄色点。在这里,您的数据称为 mydf
.
foo <- mutate(mydf, business.names = paste(seq_along(X1), business.names, sep = " "))
# business.names X1 X2
#1 1 Paul Jones 53.1912196 -2.8902453
#2 2 John Roberts 53.1916771 -2.8846099
#3 3 Bert Harris 53.1934748 -2.8814698
#4 4 Bob Jones 53.193418 -2.881248
#5 5 Mike Taylor 53.1894004 -2.8886692
#6 6 Bill Harris 53.1884117 -2.88802
#7 7 Morris Jones 53.186744 -2.890165
#8 8 Sue Perkins 53.189836 -2.893896
#9 9 Anne Jones 53.1902965 -2.8919373
#10 10 Fred Morris 53.1905138631287 -2.89043889005541
create.map<-function(lst){
require("ggmap")
require("qmap")
way.points <-data.frame(lapply(lst[,1:3], as.character), stringsAsFactors=FALSE)
rte.from <- apply(way.points[-nrow(way.points),2:3],1,paste,collapse=",")
rte.to <- apply(way.points[-1,2:3],1,paste,collapse=",")
rte <- do.call(rbind,
mapply(route, rte.from, rte.to, SIMPLIFY=FALSE,
MoreArgs=list(mode="walking",structure="legs")))
map.centre<-c(mean(as.numeric(way.points$X2)),mean(as.numeric(way.points$X1)))
coords <- rbind(as.matrix(rte[,7:8]),as.matrix(rte[nrow(rte),9:10]))
coords <- as.data.frame(coords)
ggm <- qmap(location=map.centre,zoom = 15, maptype = "road", legend="topright")
ggm +
geom_path(data=coords,aes(x=startLon,y=startLat),color="blue",size=2)+
geom_point(data=way.points,aes(x=as.numeric(X2),y=as.numeric(X1)),
size=10,color="yellow")+
geom_point(data=way.points,aes(x=as.numeric(X2),y=as.numeric(X1),
color = factor(business.names, levels=business.names)), alpha = 0) +
geom_text(data=way.points,
aes(x=as.numeric(X2),y=as.numeric(X1), label=seq_along(X1))) +
scale_color_discrete(name = "Business names") +
theme(legend.key = element_rect(fill = NA))
}
create.map(foo)
我正在 ggmap 上绘制路线和点,我需要添加一个图例来列出这些点代表的名称 即
1 比尔·哈里斯, 2 安妮·琼斯 等..
我准备了示例数据。
structure(list(business.names = structure(c(8L, 9L, 5L, 6L, 7L,
1L, 4L, 3L, 2L, 10L), .Label = c("Bill Harris", "Anne Jones",
"Sue Perkins", "Morris Jones", "Bert Harris", "Bob Jones",
"Mike Taylor", "Paul Jones", "John Roberts",
"Fred Morris"), class = "factor"), X1 = structure(c(7L,
8L, 10L, 9L, 3L, 2L, 1L, 4L, 5L, 6L), .Label = c("53.186744",
"53.1884117", "53.1894004", "53.189836", "53.1902965", "53.1905138631287",
"53.1912196", "53.1916771", "53.193418", "53.1934748"), class = "factor"),
X2 = structure(c(7L, 3L, 2L, 1L, 5L, 4L, 6L, 10L, 9L, 8L), .Label = c("-2.881248",
"-2.8814698", "-2.8846099", "-2.88802", "-2.8886692", "-2.890165",
"-2.8902453", "-2.89043889005541", "-2.8919373", "-2.893896"
), class = "factor")), .Names = c("business.names", "X1",
"X2"), row.names = c(10L, 9L, 8L, 1L, 7L, 6L, 3L, 4L, 5L, 2L), class = "data.frame")
这是我迄今为止的代码。请注意,对于 geom_path 和 geom.route,大小和颜色参数在 AES 函数之外。我不需要根据数据调整尺寸或颜色。如果我将大小和颜色放入 AES 函数中,我会得到一个图例,但这不是我想要的。它使用的是点数据而不是商家名称。
我什至不确定传奇是否是最好的选择。我只需要以简单的方式 table.
显示在图表上的数据create.map<-function(lst){
require("ggmap")
require("qmap")
way.points<-data.frame(lapply(lst[,1:3], as.character), stringsAsFactors=FALSE)
rte.from <- apply(way.points[-nrow(way.points),2:3],1,paste,collapse=",")
rte.to <- apply(way.points[-1,2:3],1,paste,collapse=",")
rte <- do.call(rbind,
mapply(route, rte.from, rte.to, SIMPLIFY=FALSE,
MoreArgs=list(mode="walking",structure="legs")))
map.centre<-c(mean(as.numeric(way.points$X2)),mean(as.numeric(way.points$X1)))
coords <- rbind(as.matrix(rte[,7:8]),as.matrix(rte[nrow(rte),9:10]))
coords <- as.data.frame(coords)
ggm <- qmap(location=map.centre,zoom = 15, maptype = "road", legend="topright")
ggm +
geom_path(data=coords,aes(x=startLon,y=startLat),color="blue",size=2)+
geom_point(data=way.points,aes(x=as.numeric(X2),y=as.numeric(X1)),
size=10,color="yellow")+
geom_text(data=way.points,
aes(x=as.numeric(X2),y=as.numeric(X1), label=seq_along(X1)))
}
这是一种可能。您可能想要排列公司名称并再次使用 geom_point
来创建图例。在这个 geom_point
中,您使用 alpha = 0
。因此,您实际上看不到任何要点;您只会看到第一个 geom_point
中指定的黄色点。在这里,您的数据称为 mydf
.
foo <- mutate(mydf, business.names = paste(seq_along(X1), business.names, sep = " "))
# business.names X1 X2
#1 1 Paul Jones 53.1912196 -2.8902453
#2 2 John Roberts 53.1916771 -2.8846099
#3 3 Bert Harris 53.1934748 -2.8814698
#4 4 Bob Jones 53.193418 -2.881248
#5 5 Mike Taylor 53.1894004 -2.8886692
#6 6 Bill Harris 53.1884117 -2.88802
#7 7 Morris Jones 53.186744 -2.890165
#8 8 Sue Perkins 53.189836 -2.893896
#9 9 Anne Jones 53.1902965 -2.8919373
#10 10 Fred Morris 53.1905138631287 -2.89043889005541
create.map<-function(lst){
require("ggmap")
require("qmap")
way.points <-data.frame(lapply(lst[,1:3], as.character), stringsAsFactors=FALSE)
rte.from <- apply(way.points[-nrow(way.points),2:3],1,paste,collapse=",")
rte.to <- apply(way.points[-1,2:3],1,paste,collapse=",")
rte <- do.call(rbind,
mapply(route, rte.from, rte.to, SIMPLIFY=FALSE,
MoreArgs=list(mode="walking",structure="legs")))
map.centre<-c(mean(as.numeric(way.points$X2)),mean(as.numeric(way.points$X1)))
coords <- rbind(as.matrix(rte[,7:8]),as.matrix(rte[nrow(rte),9:10]))
coords <- as.data.frame(coords)
ggm <- qmap(location=map.centre,zoom = 15, maptype = "road", legend="topright")
ggm +
geom_path(data=coords,aes(x=startLon,y=startLat),color="blue",size=2)+
geom_point(data=way.points,aes(x=as.numeric(X2),y=as.numeric(X1)),
size=10,color="yellow")+
geom_point(data=way.points,aes(x=as.numeric(X2),y=as.numeric(X1),
color = factor(business.names, levels=business.names)), alpha = 0) +
geom_text(data=way.points,
aes(x=as.numeric(X2),y=as.numeric(X1), label=seq_along(X1))) +
scale_color_discrete(name = "Business names") +
theme(legend.key = element_rect(fill = NA))
}
create.map(foo)