【问题标题】:ggplot2 scale_shape_manual legend does not displayggplot2 scale_shape_manual 图例不显示
【发布时间】:2020-06-25 04:51:35
【问题描述】:

这是一个数据最少的玩具示例。 ggrepel 是必要的,因为我的实际数据集包含近似点对。 ggthemes 不是必需的,但包括在内以获得更清晰的地图。示例中的Id 数字和字母是任意的,是对我的数据集进行采样的副产品。

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()

我想在地图右侧(外侧)显示一个图例,显示与地图上的字母相匹配的大学名称:

h Hankuk University of Foreign Studies
m Kyung Hee University

但是图例不显示:

【问题讨论】:

    标签: r ggplot2 sf ggrepel


    【解决方案1】:

    没有显示形状图例,因为您没有将任何东西映射到形状美学。

    试试这个:

    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)))
    

    【讨论】:

      猜你喜欢
      • 2023-02-17
      • 1970-01-01
      • 2021-01-09
      • 1970-01-01
      • 2020-02-04
      • 1970-01-01
      • 2013-12-21
      • 2013-03-03
      • 1970-01-01
      相关资源
      最近更新 更多