简体   繁体   中英

ggplot2 scale_shape_manual legend does not display

Here's a toy example with minimal data. ggrepel is necessary because my actual dataset contains proximate point pairs. ggthemes is not necessary but included to get a cleaner map. The Id numbers and letters in the example are arbitrary, a byproduct of sampling my dataset.

library(sf)
library(ggplot2)
library(ggrepel)
library(ggthemes)

# generate sf object of university coordinates
univ <- structure(list(Name = c("Hankuk University of Foreign Studies", 
"Kyung Hee University"), Id = c(8L, 13L), X = c(961061.06117124, 
960434.729117049), Y = c(1955411.02747011, 1955429.52582411), 
    geometry = structure(list(structure(c(961061.06117124, 1955411.02747011
    ), class = c("XY", "POINT", "sfg")), structure(c(960434.729117049, 
    1955429.52582411), class = c("XY", "POINT", "sfg"))), class = c("sfc_POINT", 
    "sfc"), precision = 0, bbox = structure(c(xmin = 960434.729117049, 
    ymin = 1955411.02747011, xmax = 961061.06117124, ymax = 1955429.52582411
    ), class = "bbox"), crs = structure(list(input = "EPSG:5179", 
        wkt = "PROJCRS[\"Korea 2000 / Unified CS\",\n    BASEGEOGCRS[\"Korea 2000\",\n        DATUM[\"Geocentric datum of Korea\",\n            ELLIPSOID[\"GRS 1980\",6378137,298.257222101,\n                LENGTHUNIT[\"metre\",1]]],\n        PRIMEM[\"Greenwich\",0,\n            ANGLEUNIT[\"degree\",0.0174532925199433]],\n        ID[\"EPSG\",4737]],\n    CONVERSION[\"Korea Unified Belt\",\n        METHOD[\"Transverse Mercator\",\n            ID[\"EPSG\",9807]],\n        PARAMETER[\"Latitude of natural origin\",38,\n            ANGLEUNIT[\"degree\",0.0174532925199433],\n            ID[\"EPSG\",8801]],\n        PARAMETER[\"Longitude of natural origin\",127.5,\n            ANGLEUNIT[\"degree\",0.0174532925199433],\n            ID[\"EPSG\",8802]],\n        PARAMETER[\"Scale factor at natural origin\",0.9996,\n            SCALEUNIT[\"unity\",1],\n            ID[\"EPSG\",8805]],\n        PARAMETER[\"False easting\",1000000,\n            LENGTHUNIT[\"metre\",1],\n            ID[\"EPSG\",8806]],\n        PARAMETER[\"False northing\",2000000,\n            LENGTHUNIT[\"metre\",1],\n            ID[\"EPSG\",8807]]],\n    CS[Cartesian,2],\n        AXIS[\"northing (X)\",north,\n            ORDER[1],\n            LENGTHUNIT[\"metre\",1]],\n        AXIS[\"easting (Y)\",east,\n            ORDER[2],\n            LENGTHUNIT[\"metre\",1]],\n    USAGE[\n        SCOPE[\"unknown\"],\n        AREA[\"Korea, Republic of (South Korea)\"],\n        BBOX[28.6,122.71,40.27,134.28]],\n    ID[\"EPSG\",5179]]"), class = "crs"), n_empty = 0L)), sf_column = "geometry", agr = structure(c(Name = NA_integer_, 
Id = NA_integer_, X = NA_integer_, Y = NA_integer_), .Label = c("constant", 
"aggregate", "identity"), class = "factor"), row.names = c(9L, 
13L), class = c("sf", "data.frame"))

# map universities as points labelled with letters 
ggplot(univ) + geom_sf(color="black") + geom_text_repel(aes(x=X, y=Y, label=letters[Id])) + 
  scale_shape_manual(values=letters[univ$Id], name=univ$Name) + theme_map()

I'd like to display a legend on the right (outside) of the map showing the university names matching the letters on the map:

h Hankuk University of Foreign Studies
m Kyung Hee University

But the legend doesn't display: 在此处输入图像描述

The shape legend didn't display because you didn't map anything to the shape aesthetic.

Try this:

ggplot(univ) +
  geom_sf(color="black") + 
  geom_text_repel(aes(x=X, y=Y, label=letters[Id])) +
  geom_point(aes(x = X, y = Y, shape = Name), alpha = 0) + # invisible point layer
                                                           # for shape mapping
  scale_shape_manual(values = letters[univ$Id],
                     guide = guide_legend(override.aes = list(alpha = 1, 
                                                              size = 3)))

阴谋

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM