我希望用一个整体标签和不同字体大小的标签制作一个谷歌地图的多面图。例如,考虑以下代码,这些代码基于 Max Marchi 在博客文章(链接)中提供的代码:
# 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()
我明白了:
为了减少白色空格并使整个标签可见,我在theme
'splot.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
,但我仍然没有得到我需要的情节,这将是这样的:
我在图像编辑器软件的帮助下制作了最后一张/想要的图,但是为了我的目标,这不是一个好主意,因为我需要制作几个这样的多面谷歌地图图;每个都有一个整体标签,并且标签具有不同的字体大小。
有没有人对如何在 R 中制作像最后一个这样的情节有任何建议?提前致谢。
PS1:上图周围的黑色边框是使用图像编辑器软件手动绘制的,以突出我遇到的白色空白问题。
PS2:在提供的代码中,我将绘图导出为 PDF,因为图形用于发布;因此,太多的空白空间不是一件好事,因为科学期刊通常对图形尺寸有限制。