R ggmap/ggplot2中多个图例的对齐

R ggmap/ggplot2中多个图例的对齐,r,ggplot2,ggmap,r-grid,R,Ggplot2,Ggmap,R Grid,我试图用两个表示形状和颜色的图例(以下示例中的“Type”和“Org”)制作一张地图,并插入图例。我可以放置图例,但我希望它们左对齐,以便其左边缘对齐。我只能让他们互相尊重: require(ggplot2) require(ggmap) require(grid) require(mapproj) data <- data.frame(Org=rep(c("ABCDEFG","HIJKLMNOP","QRSTUVWX"),4) , Type=rep

我试图用两个表示形状和颜色的图例(以下示例中的“Type”和“Org”)制作一张地图,并插入图例。我可以放置图例,但我希望它们左对齐,以便其左边缘对齐。我只能让他们互相尊重:

require(ggplot2)
require(ggmap)
require(grid)
require(mapproj)

data <- data.frame(Org=rep(c("ABCDEFG","HIJKLMNOP","QRSTUVWX"),4)
                   , Type=rep(c("Y","Z"),6), Lat=runif(12,48,54.5)
                   , Long=runif(12,-133.5,-122.5))

osmMap <- get_map(location=c(-134,47.5,-122,55), source = 'osm')

points <- geom_jitter(data=data, aes(Long, Lat, shape=Type
                                     , colour=Org))

legend <- theme(legend.justification=c(0,0), legend.position=c(0,0)
                , legend.margin=unit(0,"lines"), legend.box="vertical"
                , legend.key.size=unit(1,"lines"), legend.text.align=0
                , legend.title.align=0)

ggmap(osmMap) + points + legend
require(ggplot2)
需要(ggmap)
需要(网格)
需要(mapproj)

数据此选项现在在ggplot2 0.9.3.1中可用,请使用

ggmap(osmMap) + points + legend + theme(legend.box.just = "left")
旧的手动解决方案:

以下是一个解决方案:

require(gtable)
require(ggplot2)
require(ggmap)
require(grid)
require(mapproj)

# Original data
data <- data.frame(Org=rep(c("ABCDEFG","HIJKLMNOP","QRSTUVWX"),4),
                   Type=rep(c("Y","Z"),6), Lat=runif(12,48,54.5),
                   Long=runif(12,-133.5,-122.5))
osmMap <- get_map(location=c(-134,47.5,-122,55), source = 'google')
points <- geom_jitter(data=data, aes(Long, Lat, shape=Type, colour=Org))
legend <- theme(legend.justification=c(0,0), legend.position=c(0,0),
                legend.margin=unit(0,"lines"), legend.box="vertical",
                legend.key.size=unit(1,"lines"), legend.text.align=0,
                legend.title.align=0)

# Data transformation
p <- ggmap(osmMap) + points + legend
data <- ggplot_build(p)
gtable <- ggplot_gtable(data)

# Determining index of legends table
lbox <- which(sapply(gtable$grobs, paste) == "gtable[guide-box]")
# Each legend has several parts, wdth contains total widths for each legend
wdth <- with(gtable$grobs[[lbox]], c(sum(as.vector(grobs[[1]]$widths)), 
                                     sum(as.vector(grobs[[2]]$widths))))
# Determining narrower legend
id <- which.min(wdth)
# Adding a new empty column of abs(diff(wdth)) mm width on the right of 
# the smaller legend box
gtable$grobs[[lbox]]$grobs[[id]] <- gtable_add_cols(
                                      gtable$grobs[[lbox]]$grobs[[id]], 
                                      unit(abs(diff(wdth)), "mm"))
# Plotting
grid.draw(gtable)
require(gtable)
需要(ggplot2)
需要(ggmap)
需要(网格)
需要(mapproj)
#原始数据

数据这不是一个直接的答案,但你可以通过使用格式使它们的大小相等来欺骗一点。试试
data$Type这是一个巧妙的技巧,谢谢。我将在周一试一试谢谢你@Julius-这很好,让我对使用gtable有了一些见解,这是我以前没有真正探讨过的。我无法重现这个例子。我从哪里获得
osmMap
?我假设这是一些数据集。@FaheemMitha,是的,这是问题中的数据。我已经把它添加到了我的答案中,并且将
source='osm'
更改为
source='google'
,因为前者至少目前不起作用。谢谢你,朱利叶斯。我感谢你的快速回复。