将两个图例(大小和颜色)合并为一个

时间:2015-05-27 20:10:05

标签: r ggplot2

将两个图例合而为一的代码是什么:带有颜色的圆圈图例?

我认为,根据"尺寸"和#34;#犯罪总数"是展示传奇的最佳方式。

期望的输出:

1)应该有一个传说:圈子而不是黑色应该是彩色的:0圈="黄色"到800圈="红色"。

enter image description here

我的代码:

library(maps)
library(ggmap)

从Google地图获取地图

 lima <- get_map(location = "lima", zoom = 11, maptype = c("terrain"))

剧情

ggmap(lima) + geom_point(data = limanov2, aes(x = LONGITUD , y = LATITUD, color = TOTALES,
                                                  size = TOTALES)) +
        scale_size_continuous(name = "Cantidad\ndelitos",range = c(2,12)) + 
        scale_color_gradient(name = "Cantidad\ndelitos", low = "yellow", high = "red") +
        theme(legend.text= element_text(size=14)) +
        ggtitle("TOTAL DELITOS - LIMA NOV 2012") +
        theme(plot.title = element_text(size = 12, vjust=2, family="Verdana", face="italic"),
               legend.position = 'left')

我的数据:

    structure(list(DISTRITO = c("SAN JUAN DE LURIGANCHO", "CALLAO", 
"LOS OLIVOS", "ATE", "LIMA", "SAN MARTIN DE PORRES", "SANTIAGO DE SURCO", 
"CHORILLOS", "COMAS", "INDEPENDENCIA", "EL AGUSTINO", "LA VICTORIA", 
"SAN JUAN DE MIRAFLORES", "VILLA EL SALVADOR", "SAN MIGUEL", 
"CARABAYLLO", "MIRAFLORES", "SAN BORJA", "VENTANILLA", "SURQUILLO", 
"BREÑA", "ANCON", "PTE. PIEDRA", "RIMAC", "BARRANCO", "LA MOLINA", 
"SAN LUIS", "SANTA ANITA", "LURIGANCHO", "P. LIBRE", "MAGDALENA DEL MAR", 
"LA PERLA", "CHACLACAYO", "PUENTE PIEDRA", "SAN ISIDRO", "JESUS MARIA", 
"BELLAVISTA", "LINCE", "CARMEN DE LA LEGUA REYNOSO", "CIENEGUILLA", 
"SANTA ROSA", "LURIN", "PUNTA NEGRA", "PUCUSANA", "LA PUNTA", 
"PUNTA HERMOSA", "PACHACAMAC", "SAN BARTOLO", "SANTA MARIA"), 
    TOTALES = c(861L, 696L, 696L, 642L, 516L, 479L, 442L, 378L, 
    371L, 368L, 361L, 333L, 325L, 291L, 282L, 251L, 239L, 196L, 
    193L, 188L, 185L, 174L, 165L, 161L, 138L, 134L, 128L, 119L, 
    115L, 105L, 67L, 65L, 63L, 58L, 58L, 56L, 45L, 38L, 23L, 
    23L, 11L, 8L, 6L, 5L, 3L, 3L, 2L, 0L, 0L), HOMICIDIOS = c(1L, 
    7L, 0L, 1L, 2L, 0L, 0L, 1L, 7L, 4L, 4L, 4L, 0L, 0L, 0L, 2L, 
    0L, 0L, 7L, 0L, 0L, 0L, 0L, 4L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 
    0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L), LESIONES = c(100L, 72L, 61L, 43L, 44L, 8L, 10L, 
    15L, 44L, 40L, 50L, 15L, 52L, 28L, 7L, 33L, 15L, 3L, 21L, 
    7L, 36L, 33L, 15L, 19L, 14L, 1L, 8L, 6L, 16L, 4L, 4L, 9L, 
    1L, 12L, 2L, 9L, 5L, 2L, 5L, 7L, 1L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L), VIO..DE.LA.LIBERTAD.PERSONAL = c(0L, 7L, 6L, 
    5L, 6L, 1L, 1L, 0L, 3L, 1L, 2L, 0L, 2L, 0L, 1L, 0L, 1L, 0L, 
    1L, 1L, 0L, 3L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 
    0L, 1L, 0L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L), VIO..DE.LA.LIBERTAD.SEXUAL = c(56L, 14L, 12L, 15L, 7L, 
    10L, 2L, 9L, 11L, 13L, 8L, 9L, 7L, 14L, 4L, 15L, 4L, 2L, 
    17L, 7L, 3L, 4L, 6L, 12L, 2L, 1L, 5L, 3L, 11L, 4L, 1L, 2L, 
    0L, 6L, 2L, 0L, 3L, 0L, 2L, 2L, 0L, 4L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L), HURTO.SIMPLE.Y.AGRAVADO = c(217L, 203L, 296L, 230L, 
    260L, 167L, 226L, 217L, 130L, 117L, 154L, 133L, 121L, 46L, 
    163L, 72L, 161L, 119L, 69L, 120L, 64L, 19L, 64L, 21L, 57L, 
    44L, 39L, 2L, 48L, 60L, 30L, 19L, 48L, 20L, 41L, 25L, 19L, 
    27L, 7L, 11L, 9L, 0L, 6L, 0L, 2L, 3L, 1L, 0L, 0L), ROBO.SIMPLE.Y.AGRAVADO = c(460L, 
    289L, 308L, 344L, 186L, 277L, 198L, 130L, 165L, 184L, 137L, 
    149L, 134L, 188L, 104L, 126L, 58L, 72L, 64L, 51L, 77L, 115L, 
    79L, 76L, 64L, 88L, 73L, 108L, 40L, 36L, 30L, 32L, 14L, 17L, 
    12L, 22L, 12L, 8L, 6L, 3L, 1L, 3L, 0L, 2L, 1L, 0L, 1L, 0L, 
    0L), MICRO.COM.DE.DROGAS = c(26L, 100L, 13L, 3L, 10L, 15L, 
    5L, 5L, 11L, 8L, 3L, 23L, 9L, 15L, 3L, 3L, 0L, 0L, 8L, 2L, 
    5L, 0L, 0L, 28L, 0L, 0L, 1L, 0L, 0L, 0L, 2L, 2L, 0L, 2L, 
    0L, 0L, 6L, 0L, 0L, 0L, 0L, 0L, 0L, 3L, 0L, 0L, 0L, 0L, 0L
    ), TENENCIA.ILEGAL.DE.ARMAS = c(1L, 4L, 0L, 1L, 1L, 1L, 0L, 
    1L, 0L, 1L, 3L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 6L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), LONGITUD = c(-77, 
    -77.12, -77.08, -76.89, -77.04, -77.09, -76.99, -77.01, -77.05, 
    -77.05, -77, -77.02, -76.97, -76.94, -77.09, -76.99, -77.03, 
    -77, -77.13, -77.01, -77.05, -77.11, -77.08, -76.7, -77.02, 
    -76.92, -77, -76.96, -76.86, -77.06, -77.07, -77.12, -76.76, 
    -77.08, -77.03, -77.05, -77.11, -77.04, -77.09, -76.78, -77.16, 
    -76.81, -76.73, -76.77, -77.16, -76.76, -76.83, -76.73, -76.77
    ), LATITUD = c(-11.99, -12.04, -11.95, -12.04, -12.06, -12, 
    -12.16, -12.2, -11.93, -11.99, -12.04, -12.08, -12.16, -12.23, 
    -12.08, -11.79, -12.12, -12.1, -11.89, -12.11, -12.06, -11.69, 
    -11.88, -11.94, -12.15, -12.09, -12.08, -12.04, -11.98, -12.08, 
    -12.09, -12.07, -11.99, -11.88, -12.1, -12.08, -12.06, -12.09, 
    -12.04, -12.07, -11.81, -12.24, -12.32, -12.47, -12.07, -12.28, 
    -12.18, -12.38, -12.42)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -49L), .Names = c("DISTRITO", "TOTALES", 
"HOMICIDIOS", "LESIONES", "VIO..DE.LA.LIBERTAD.PERSONAL", "VIO..DE.LA.LIBERTAD.SEXUAL", 
"HURTO.SIMPLE.Y.AGRAVADO", "ROBO.SIMPLE.Y.AGRAVADO", "MICRO.COM.DE.DROGAS", 
"TENENCIA.ILEGAL.DE.ARMAS", "LONGITUD", "LATITUD"))

1 个答案:

答案 0 :(得分:1)

我找到了解决方案。阅读GGPLOT2 V. 0.9

的文档

新功能:guide_legend()应在guides()内使用。

此功能可让您更好地控制图例标签。

这是带有结果输出的结束代码(参见最后一行):

ggmap(lima) + geom_point(data = limanov2, aes(x = LONGITUD , y = LATITUD, color = TOTALES,
                                              size = TOTALES)) +
    scale_size_continuous(name = "Cantidad\ndelitos",range = c(2,12)) + 
    scale_color_gradient(name = "Cantidad\ndelitos", low = "yellow", high = "red") +
    theme(legend.text= element_text(size=14)) +
    ggtitle("TOTAL DELITOS - LIMA NOV 2012") +
    theme(plot.title = element_text(size = 12, vjust=2, family="Verdana", face="italic"),
           legend.position = 'left') +
    guides(colour = guide_legend())

enter image description here