有许多问题涉及从标准ggplot2图中删除空格,例如barplots。但是,目前还没有关于基于 choroplethr 的ggplot2图的问题。我也没有发现任何关于其他非标准情节的问题,所以我问。
我正在为Argentinian first-level administrative divisions (provinces)映射数据。首先加载库和示例数据:
library(ggplot2); library(grid); library(choroplethr); library(choroplethrAdmin1)
d_argentina_admin1 =
structure(list(country = c("argentina", "argentina", "argentina",
"argentina", "argentina", "argentina", "argentina", "argentina",
"argentina", "argentina", "argentina", "argentina", "argentina",
"argentina", "argentina", "argentina", "argentina", "argentina",
"argentina", "argentina", "argentina", "argentina", "argentina",
"argentina"), region = c("ciudad autonoma de buenos aires", "provincia de buenos aires",
"provincia de catamarca", "provincia de cordoba", "provincia de corrientes",
"provincia de entre rios", "provincia de formosa", "provincia de jujuy",
"provincia de la pampa", "provincia de la rioja", "provincia de mendoza",
"provincia de misiones", "provincia de rio negro", "provincia de salta",
"provincia de san juan", "provincia de san luis", "provincia de santa cruz",
"provincia de santa fe", "provincia de santiago del estero",
"provincia de tierra del fuego, antartida e islas del atlantico sur",
"provincia de tucuman", "provincia del chaco", "provincia del chubut",
"provincia del neuquen"), region_short = c("ciudad autonoma de buenos aires",
"buenos aires", "catamarca", "cordoba", "corrientes", "entre rios",
"formosa", "jujuy", "la pampa", "la rioja", "mendoza", "misiones",
"rio negro", "salta", "san juan", "san luis", "santa cruz", "santa fe",
"santiago del estero", "tierra del fuego, antartida e islas del atlantico sur",
"tucuman", "chaco", "chubut", "neuquen"), CA = c(1.68523081351625,
0.12549520300237, -1.27397814948309, 0.455533000767044, -0.852755464371651,
0.311078868096833, -1.15435688398305, -0.731142526555172, 1.10745473359446,
-1.17376627430079, 0.410907812201062, -0.559784594595219, 0.752534373004307,
-0.10532704793929, -0.534917391396886, 0.565419267663713, 0.550492884041435,
0.775972211209032, -1.35483468421443, 0.243461376532096, -0.456931964639015,
-1.21548156415082, -0.184277139940435, 2.61397314194124), S = c(2.28055681849635,
0.886904398431874, -0.0336697376305896, 0.795396060124547, -1.10055353978519,
0.215269561474404, -1.91800141017787, -0.85287131899651, 0.602260636644222,
0.107582135770333, 0.509706470343261, -1.34372858962124, 0.266348607390528,
-0.813557146573346, 0.178982808194874, 0.387580775513218, 0.836845928452549,
0.633109400616697, -1.19041212503163, 1.10688354366868, -0.180773498720898,
-1.91244609716818, 0.0289904264255843, 0.50959589215834), European_pct = c(NA,
79L, 55L, 70L, 56L, 70L, 58L, 45L, 72L, 55L, 68L, 65L, 63L, 45L,
60L, 67L, 63L, 76L, 55L, 63L, 63L, 58L, 63L, 63L), HDI2011 = c(0.889,
0.838, 0.836, 0.862, 0.828, 0.839, 0.806, 0.829, 0.864, 0.834,
0.852, 0.817, 0.851, 0.832, 0.825, 0.828, 0.873, 0.846, 0.807,
0.88, 0.843, 0.807, 0.848, 0.855), HDI1996 = c(0.854, 0.769,
0.766, 0.776, 0.766, 0.763, 0.739, 0.743, 0.782, 0.76, 0.782,
0.747, 0.77699999999999, 0.756, 0.767, 0.762, 0.791, 0.777, 0.745,
0.804, 0.77, 0.765, 0.777, 0.784), Latitude = c(34.6, 34.92,
28.47, 31.42, 27.47, 31.74, 26.19, 24.19, 36.62, 29.41, 32.89,
27.36, 40.81, 24.78, 31.54, 33.3, 51.62, 31.61, 27.78, 54.8,
26.81, 27.45, 43.3, 38.95), Mean.C = c(18.1, 15.8, 20.5, 17.63,
21.1, 18, 22.1, 18.7, 15.5, 20.1, 16.7, 21.3, 14.2, 16.5, 17.9,
17.2, 7.6, 18.5, 20.4, 5.7, 19.3, 21, 13.6, 14.5), Knoema_name = c("Ciudad Autónoma de Buenos Aires",
"Buenos Aires", "Catamarca", "Córdoba", "Corrientes", "Entre Ríos",
"Formosa", "Jujuy", "La Pampa", "La Rioja", "Mendoza", "Misiones",
"Río Negro", "Salta", "San Juan", "San Luis", "Santa Cruz", "Santa Fe",
"Santiago del Estero", "Tierra del Fuego", "Tucumán", "Chaco",
"Chubut", "Neuquén"), long = c(-58.4087168822226, -60.4520279200146,
-67.0033502110092, -63.6371590458406, -57.5922837730968, -59.1929261299057,
-59.9002069633236, -65.6846836561529, -66.0781718361702, -67.8240418774711,
-68.9698150834006, -54.6502325454809, -67.7111354201418, -65.5153259609209,
-68.9777591351386, -66.3059831227416, -69.6838127357142, -60.6945670435963,
-64.3580773677863, -65.8132823586689, -65.4186581044413, -59.9636744025983,
-67.6203412779368, -70.5435477557748), lat = c(-34.6301055975446,
-37.2961720341364, -27.4440629216902, -31.7921585910377, -28.7753987205406,
-32.1078433856807, -25.095300376526, -23.4789231673989, -38.0265724152956,
-29.3771688411262, -34.257024768382, -26.8538125248083, -40.047744111783,
-24.0730809995991, -30.6947781801545, -33.0413870848178, -49.2068318102752,
-31.675607181458, -28.0818062780313, -54.5269245027309, -27.015093754783,
-25.8734309245111, -43.9885104650552, -38.5793873300344), value = c(18.1,
15.8, 20.5, 17.63, 21.1, 18, 22.1, 18.7, 15.5, 20.1, 16.7, 21.3,
14.2, 16.5, 17.9, 17.2, 7.6, 18.5, 20.4, 5.7, 19.3, 21, 13.6,
14.5)), .Names = c("country", "region", "region_short", "CA",
"S", "European_pct", "HDI2011", "HDI1996", "Latitude", "Mean.C",
"Knoema_name", "long", "lat", "value"), row.names = c(NA, -24L
), class = "data.frame")
我们可以使用以下代码绘制温度数据:
d_argentina_admin1$value = d_argentina_admin1$Mean.C
admin1_choropleth(country.name = "argentina",
df = d_argentina_admin1,
#title = "2010 Japan Population Estimates",
legend = "Mean temperature (C)",
num_colors = 1) +
geom_text(data = d_argentina_admin1, aes(long, lat, label = Knoema_name, group = NULL), size = 2.5)
ggplot("map_temp.png")
第一行是因为此函数总是希望使用value
列,因此必须将数据移动到该列而不是指定列。我们使用ggsave
保存它,它看起来像这样:
北方的平均气温高于预期。
可以看到(使用鼠标悬停)地图周围有很多想要移除的空白。可以通过修改ggsave
参数来手动删除它:
ggsave("figures/map_temp2.png", width = 5, height = 7)
但是应该有一种自动方法来做到这一点。我尝试按照其他问题的建议添加0
绘图边距,但它没有效果。
theme(plot.margin = unit(c(0,0,0,0), "cm"))
如何从这种类型的ggplot2图中自动删除空格/边距?