Google 地图的多面图,带有一个整体标签和具有不同字体大小的标签

Facetted plot of Google maps with an overall label and the label having different font sizes

我想制作一个 Google 地图的多面图,其中包含一个整体标签和具有不同字体大小的标签。 例如,考虑以下代码,这些代码基于 Max Marchi 在博客 post (link) 中提供的代码:

# Load the data
airports <- read.csv("https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports.dat", header = FALSE)
colnames(airports) <- c("ID", "name", "city",
  "country", "IATA_FAA", "ICAO", "lat", "lon",
   "altitude", "timezone", "DST")

routes <- read.csv("https://github.com/jpatokal/openflights/raw/master/data/routes.dat", header = FALSE)
colnames(routes) <- c("airline", "airlineID",
  "sourceAirport", "sourceAirportID",
  "destinationAirport", "destinationAirportID",
  "codeshare", "stops", "equipment")

# Getting the data ready for plotting
# * For a detailed explanation on setting up the data 
# I suggest consulting Max Marchi's post: 
# http://www.milanor.net/blog/maps-in-r-plotting-data-points-on-a-map/
library(plyr)
departures <- ddply(routes, .(sourceAirportID), "nrow")
names(departures)[2] <- "flights"
arrivals <- ddply(routes, .(destinationAirportID), "nrow")
names(arrivals)[2] <- "flights"
airportD <- merge(airports, departures, by.x = "ID", 
                  by.y = "sourceAirportID")
airportA <- merge(airports, arrivals, by.x = "ID", 
                  by.y = "destinationAirportID")
airportD$type <- "departures"
airportA$type <- "arrivals"

# The final data frame used for plotting
airportDA <- rbind(airportD, airportA)

# Get the map of Europe from Google Maps
library(ggmap)
map <- get_map(location = 'Europe', zoom = 4)

# Make a facetted Google map plot 
library(ggplot2)
facet.gmap <- ggmap(map) +
  geom_point(aes(x = lon, y = lat, 
  size = sqrt(flights)), 
  data = airportDA, alpha = .5) +
  facet_wrap( ~ type, ncol=2) +
  theme(legend.position="none") 

# Add an overall label with different font sizes 
library(gtable)
library(grid)
facet.gmap.label <- ggplotGrob(facet.gmap) 
facet.gmap.label <- gtable_add_grob(facet.gmap.label, 
            grobTree(textGrob("M", x=0.05, 
                        y=0.85,just="left",  
                        gp = gpar(fontsize = 14, 
                        fontface = "bold")), 
                     textGrob("Some label", x=0.18, 
                        y=0.68, just="left",
                        gp = gpar(fontsize = 9,
                        fontface = "bold")) ), 
                     t=1, b=4, l=1, r=4)

# Save as PDF
pdf("facet.gmap.label.pdf", 
      width=4.5,  
      height=3.6)
grid.draw(facet.gmap.label)
dev.off()

我明白了:

为了减少空白并使整个标签可见,我在 themeplot.margin 参数中测试了不同的值,“最佳”(最差)图是通过以下方式获得的设置 plot.margin = unit(c(0.8, 0.4, -3.8, 0.3), "lines"):

# Edit the 'theme' 'plot.margin' parameter
facet.gmap2 <- facet.gmap + 
theme(plot.margin = unit(c(0.8, 0.4, -3.8, 0.3), "lines"))

# Add again the overall label with different font sizes 
facet.gmap.label2 <- ggplotGrob(facet.gmap2) 
facet.gmap.label2 <- gtable_add_grob(facet.gmap.label2, 
            grobTree(textGrob("M", x=0.05, 
                        y=0.85,just="left",  
                        gp = gpar(fontsize = 14, 
                        fontface = "bold")), 
                     textGrob("Some label", x=0.18, 
                        y=0.68, just="left",
                        gp = gpar(fontsize = 9,
                        fontface = "bold")) ), 
                     t=1, b=4, l=1, r=4)

# Save as PDF
pdf("facet.gmap.label2.pdf", 
      width=4.5,  
      height=3.6)
grid.draw(facet.gmap.label2)
dev.off()

结果:

底线:尽管我在 plot.margin 中测试了不同的值,但我仍然没有得到我需要的情节,就像这样:

我在图像编辑器软件的帮助下制作了这个 last/desired 图,但是为了我的目标,这不是一个好主意,因为我需要制作几个这样的 Google 地图图;每个都有一个整体标签和不同字体大小的标签。

有没有人对如何在 R 中制作像最后一个这样的情节有任何建议?提前致谢。

为什么不能只使用 ggtitle,它会自动处理保证金增加?

这应该会为您提供所需的文本输出,而不必费力寻找所需的确切 x-y 坐标:

facet.gmap +
  ggtitle(expression(bold(M)~scriptstyle("Some Label") ))

给出了你要找的情节

您似乎遇到的另一个问题是纵横比。 ggmap(有帮助)坚持使用 coord_fixed 或类似的方法来确保正确保留 x 与 y 距离的比率。如果输出的纵横比(调用 pdf 时的 widthheight 不同),绘图将按比例缩小以适应最严格的尺寸并留白space 别处。我在 RStudio 的 plot window 中摆弄它,并在 440x287(我以前导出的)时得到了一个很好的比率。您可以尝试从保留该近似比例的宽度到高度开始,然后从那里进行修补。