我正在制作一张 map ,我需要一些城市是某种 colored颜色 的点(A城市),而一些城市是不同 colored颜色 的点(B城市).所有B城市也都在A列表上,所以我将B geom_sf放在第二位,这样这些点将位于A城市的顶部.

在这张有效的 map 上,橙色的圆点可以在其他圆点上看到.但在传说中,A和B都是橙色的.我需要A作为深色!

(请忽略 map 的其他美学问题哈哈,我会做到的)

显示问题的 map 示例:

sample map showing issue

usa_states <- map_data("state") %>%
  rename(lon=long, State=region, County=subregion)

Acities <- read_csv("Acities.csv")
Acities_sf <- st_as_sf(Acities, coords = c("lon", "lat"), crs = 4326)

Bcities <- read_csv("Bcities.csv")
Bcities_sf <- st_as_sf(Bcities, coords = c("lon", "lat"), crs=4326)

AandBmap <- ggplot() + geom_polygon(data=usa_states, aes(x=lon, y=lat, group=group), fill="#DAD9D5", color="white") + #this is a gray US background
  coord_quickmap() + 
  geom_sf(data=Acities_sf, color = "#174337", size = 1.5, aes(fill="A Location")) + #A cities
  geom_sf(data=Bcities_sf, color = "#EAAA1B", size = 1.5, aes(fill="A and B Location")) + #B cities
  guides(fill=guide_legend(title=NULL)) +
  labs(x=NULL, y=NULL) + 
  theme_void()
AandBmap

对不起,我不知道如何上传我的样本数据/上传到哪里,如果有人可以告诉我,我可以这样做.与此同时,这应该会起作用,而不是加载CSV.

Acities <- matrix(c(34.0549, -118.243, 38.9072, -77.0369, 29.9511, -90.0715, 40.8137, -96.7026, 40.7128, -74.006), nrow=5, ncol=2, byrow=TRUE)

Acities <- matrix(c(34.0549, -118.243, 40.7128, -74.006), nrow=2, ncol=2, byrow=TRUE)

推荐答案

问题是,您已经将 colored颜色 设置为aes()以外的参数,然后将您想要的标签映射到fill AES Inside aes()上.相反,您必须在color AES上进行映射,并使用scale_color_manual设置您所需的 colored颜色 和标签.此外,您可以通过将城市数据绑定到一个数据帧中并为城市添加标识符列来进行简化:

library(ggplot2)
library(sf)
#> Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(maps)
library(dplyr, warn = FALSE)

Acities <- as.data.frame(Acities)
Bcities <- as.data.frame(Bcities)
names(Acities) <- names(Bcities) <- c("lon", "lat")

usa_states <- map_data("state") %>%
  rename(lon = long, State = region, County = subregion)

AB_cities <- list(A = Acities, B = Bcities) |>
  dplyr::bind_rows(.id = "city")

AB_cities_sf <- AB_cities |>
  st_as_sf(coords = c("lon", "lat"), crs = 4326)

ggplot() +
  geom_polygon(
    data = usa_states, aes(x = lon, y = lat, group = group),
    fill = "#DAD9D5", color = "white"
  ) +
  geom_sf(data = AB_cities_sf, size = 1.5, aes(color = city)) +
  scale_color_manual(
    values = c(A = "#174337", B = "#EAAA1B"),
    labels = c(A = "A Location", B = "A and B Location")
  ) +
  guides(fill = guide_legend(title = NULL)) +
  labs(x = NULL, y = NULL) +
  theme_void()

R相关问答推荐

使用Shiny组合和显示复制和粘贴的数据

使用scale_x_continuous复制ggplot 2中的离散x轴

R形式的一维数字线/箱形图样式图表

我不能在docker中加载sf

R-更新面内部的栅格值

计算满足R中条件的连续列

以相同的方式对每个表进行排序

解析R函数中的变量时出现的问题

计算数据帧中指定值之前的行数,仅基于每行之后的future 行,单位为r

如何在PackageStatus()中列出&q;不可用的包&q;?

有没有一种方法可以同时对rhandsontable进行排序和从rhandsontable中删除?

我如何使用tidyselect来传递一个符号数组,比如Pivot_Long?

我是否可以使用多个变异项来构建顺序列(标记多个问题)

用多边形替换地块点

如何调整一个facet_work()面板内的框图和移动标签之间的水平宽度?

R-找出存在其他变量的各种大小的所有组合

是什么打破了此Quarto仪表板中的工具提示?

R,将组ID分配给另一个观测ID变量中的值的组合

如何使用ggsurvfit包更改风险表中的标签名称?

当y为负值时,无法使stat_cor正确定位到底部?