R语言 如何在ggplot图例中更改背景以获得线条美感,而不更改其他背景?

jc3wubiy  于 2023-05-26  发布在  其他
关注(0)|答案(2)|浏览(133)

我正在用两组线绘制sfggplot2的Map。一组行有一列值,我正在匹配“颜色”美学。另一组线我想都是白色的(Map的背景是黑色的)。
我希望图例在黑色背景上显示白色,以及在白色背景上显示彩色线。似乎element_rect只能放在主题部分,而不是guides/override. aes。

我怎样才能让一条白色穿过一个黑色的盒子,而“lines 1”几何体的背景是白色的?

可复制代码:

library(sf)
library(geosphere)
library(ggplot2)

nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)

a<-st_coordinates(st_centroid(nc[1:10,]))
b<-st_coordinates(st_centroid(nc[91:100,]))

c<-st_coordinates(st_centroid(nc[21:30,]))
d<-st_coordinates(st_centroid(nc[81:90,]))

lines1<-gcIntermediate(a,b,sp = TRUE)
lines1<-st_as_sf(lines1)
lines1$col <- rep(c("A","B"),5)
lines2<-gcIntermediate(c,d,sp = TRUE)
lines2<-st_as_sf(lines2)

ggplot()+
  geom_sf(data = nc,fill = 'black',color = 'white')+
  geom_sf(data = lines1,aes(color = col))+
  geom_sf(data = lines2, color = 'white',aes(linetype = "help"))+
  theme(legend.key=element_rect(fill="black"))+
  scale_color_manual(values = c("darkred","yellow"),
                     guide = guide_legend(override.aes = list(linewidth = c(4))))

这是我的资料

我想要的是

watbbzwu

watbbzwu1#

我认为最好的方法是覆盖填充图例(就像在here中一样),但它似乎不适用于geom_sf。因此,我将更改key_glyph函数。

# Here, I determine how the legend is going to be displayed
my_key <- function(data, params, size) {
  grid::grobTree(
    draw_key_rect(data, list()),
    grid::segmentsGrob(0.1, 0.5, 0.9, 0.5),
    gp = grid::gpar(
      col = data$colour %||% "grey20",
      fill = data$fill %||% "white",
      lwd = (data$size %||% 0.5) * .pt,
      lty = data$linetype %||% 1
    )
  )
}

ggplot() +
  geom_sf(data = nc,
          fill = 'black',
          color = 'white') +
  geom_sf(data = lines1,
          aes(color = col))+
  geom_sf(data = lines2, 
          aes(lty = "help"),
          key_glyph = my_key, # Here, I used the new key function
          fill = "black",
          color = 'white') +
  scale_color_manual(values = c("darkred","yellow"),
                     guide = guide_legend(override.aes = list(linewidth = c(4))))

结果,我得到了这个。

lf5gs5x2

lf5gs5x22#

编辑:

这与@JavierMtzRdz发布的解决方案基本相同(+1)

原始答案:

我无法使用guides()theme()获得工作解决方案,因此您可能需要针对此用例求助于自定义key_glyph(),例如。

library(sf)
#> Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(geosphere)
library(ggplot2)

nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)

a<-st_coordinates(st_centroid(nc[1:10,]))
#> Warning: st_centroid assumes attributes are constant over geometries
b<-st_coordinates(st_centroid(nc[91:100,]))
#> Warning: st_centroid assumes attributes are constant over geometries

c<-st_coordinates(st_centroid(nc[21:30,]))
#> Warning: st_centroid assumes attributes are constant over geometries
d<-st_coordinates(st_centroid(nc[81:90,]))
#> Warning: st_centroid assumes attributes are constant over geometries

lines1<-gcIntermediate(a,b,sp = TRUE)
lines1<-st_as_sf(lines1)
lines1$col <- rep(c("A","B"),5)
lines2<-gcIntermediate(c,d,sp = TRUE)
lines2<-st_as_sf(lines2)

# Create a custom key_glyph function:
draw_key_custom <- function(data, params, size) {
  gp = grid::gpar(
    col = "white",
    fill = "black",
    lwd = 3,
    lty = 1
  )
  grid::grobTree(
    grid::rectGrob(width = 1, height = 1),
    grid::linesGrob(x = c(0.2,0.8), y = 0.5),
    gp = gp
  )
}

ggplot()+
  geom_sf(data = nc,fill = 'black',color = 'white')+
  geom_sf(data = lines1, aes(color = col))+
  geom_sf(data = lines2, color = 'white', aes(linetype = "help"),
          key_glyph = draw_key_custom)+
  scale_color_manual(values = c("darkred","yellow")) +
  guides(color = guide_legend(override.aes = list(linewidth = 3)))

创建于2023-05-24使用reprex v2.0.2

相关问题