簡體   English   中英

如何在不更改 R 中的圖例顏色的情況下在 ggmap 中使用多個符號、顏色和填充?

[英]How to use several symbols, colour, and fill in ggmap without changing the legend colour in R?

我正在使用 ggmap 繪制海洋中的一些填充點,但它們重疊,所以我使用colour=black給它們一個輪廓。 當我使用pch=21 ,圖例不會改變,並且點像我想要的那樣用黑色勾勒出來。 但是,我也試圖在地圖上獲得 3 個不同的符號。 當我指定不同的符號時,我在圖例中的點都變黑了。

這是一些示例數據和我用來獲取具有正確圖例但錯誤符號的地圖的代碼

#library
library(ggmap)
library(mapdata)
library(ggplot2)

#sample

mapoc_temp = data.frame(longitude= c(-64.5, -63.1, -62.4, -62.2, -66.0, -61.9), 
                                    latitude= c(42.7, 44.8, 45.8, 45.6, 43.0, 40.2),
                                    Zone = sample(c(1,4,6,7), 6, replace = T),
                                    location = sample(c(1,2,3), 6, replace = T))

#map
canada = map_data("worldHires")
ggplot(data = canada) +
  borders("world", xlim = c(-130, -60), ylim = c(20, 50), colour = "black", fill = "grey50") +
  geom_polygon(data = canada, aes(x=long, y = lat, group = group), fill = "grey50") +
  #coordinates of my map
  coord_sf(xlim=c(-84, -41), ylim=c(24,51), expand = FALSE) +
  #map the receiver locations
  geom_point(data = mapoc_temp,
             mapping = aes(x = longitude, 
                           y = latitude,
                           fill = Zone), 
             pch = 21,
             size = 15, colour = "black") +
  #fill the zones 
  scale_color_manual(values=c("#01579B", "#4FC3F7", "#ffa600", "#ff6361")) +
  scale_fill_manual(values=c("#01579B", "#4FC3F7", "#ffa600", "#ff6361")) 

這就是地圖的樣子在此處輸入圖片說明

當我嘗試添加符號時,我得到了正確的符號,但我的圖例不再正確。 下面是代碼和圖片。

ggplot(data = canada) +
  borders("world", xlim = c(-130, -60), ylim = c(20, 50), colour = "black", fill = "grey50") +
  geom_polygon(data = canada, aes(x=long, y = lat, group = group), fill = "grey50") +
  #coordinates of my map
  coord_sf(xlim=c(-84, -41), ylim=c(24,51), expand = FALSE) +
  #map the receiver locations
  geom_point(data = mapoc_temp,
             mapping = aes(x = longitude, 
                           y = latitude,
                           fill = as.factor(Zone)), 
             pch = pch = if_else(mapoc_temp$location == 1,25,
                                 if_else(mapoc_temp$location == 3, 23, 21)),
             size = 15, colour = "black") +
  scale_x_continuous(label = abs) +
  scale_y_continuous(label = abs) +
  #fill the zones in with viridis
  scale_color_manual(values=c("#01579B", "#4FC3F7", "#ffa600", "#ff6361")) +
  scale_fill_manual(values=c("#01579B", "#4FC3F7", "#ffa600", "#ff6361")) 

在此處輸入圖片說明

有誰知道如何修復第二張圖像中的圖例,使點的顏色正確? 我不需要符號出現在圖例中

實際上,您的問題是由於此處描述的錯誤造成的: https : //github.com/tidyverse/ggplot2/issues/2322

要過去,您可以執行以下操作:

ggplot(data = canada) +
  borders("world", xlim = c(-130, -60), ylim = c(20, 50), colour = "black", fill = "grey50") +
  geom_polygon(data = canada, aes(x=long, y = lat, group = group), fill = "grey50") +
  #coordinates of my map
  coord_sf(xlim=c(-84, -41), ylim=c(24,51), expand = FALSE) +
  #map the receiver locations
  geom_point(inherit.aes = FALSE, data = mapoc_temp,
             mapping = aes(x = longitude, 
                           y = latitude,
                           fill = factor(Zone),
                           shape = factor(location)),
             size = 5) +
  scale_x_continuous(label = abs) +
  scale_y_continuous(label = abs) +
  #fill the zones in with viridis
  scale_fill_manual(name = "Zone", values=c("#01579B", "#4FC3F7", "#ffa600", "#ff6361"), breaks = c("1","4","6","7")) +
  scale_shape_manual(values = c("1" = 21, "2" = 22, "3" = 24))+
  guides(fill = guide_legend(override.aes=list(shape=21)), shape = FALSE)

在此處輸入圖片說明

正如@Edward 所提到的,您可以通過添加guides(shape = FALSE)來擺脫shape圖例。

它回答你的問題嗎?

我沒有你的數據,所以我將使用好的 ol' mtcars 數據集。 訣竅是手動分配形狀,否則 ggplot 會抱怨“連續變量無法映射到形狀”。

mtcars$shape <- ifelse(mtcars$cyl==4, 21, ifelse(mtcars$cyl==6, 23, 25))

ggplot(mtcars, aes(wt, mpg)) + 
   geom_point(aes(fill = factor(cyl), shape=factor(cyl)), col="black", size=3) +
   scale_shape_manual(values=c(21,23,25))

在此處輸入圖片說明


對於您自己的數據,請嘗試:

mapoc_temp$shape <- factor(mapoc_temp$location + 20)

geom_point(data = mapoc_temp,
           mapping = aes(x = longitude, 
                         y = latitude,
                         fill = factor(Zone),
                         shape= shape),
           size = 15, colour = "black") +
  scale_shape_manual(values=c(21,22,23)) + # Must be same length as unique location.
  guides(shape=FALSE) + # If you don't want an extra shape legend.
  guides(fill = guide_legend(override.aes=list(shape=21))) # A necessary fudge

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM