R语言 在ggplot中叠加两个数据源相同但变量类型不同的geom_sf

pexxcrt2  于 2023-05-11  发布在  其他
关注(0)|答案(3)|浏览(148)

我想把图1和图2重叠在同一个图上。他们来自同一个来源,我想在后台的情节1和添加情节2与一些透明度的顶部。

plot1= ggplot() +
  geom_sf(data = toy,
          aes(geometry = geometry, fill = b),
          color = NA) +
  scale_fill_gradient(low="white", high="#0099FF")

plot2= ggplot() +
  geom_sf(data = toy ,
          aes(geometry = geometry, fill = a),
          color = NA) +
  scale_fill_manual(values=c(alpha("#CCCCCC",0), alpha("#990033",1)))

这些工作独立做得很好,但我无法把它们结合起来.我尝试了下面的东西,但得到了一些错误

ggplot() +
  geom_sf(data = toy,
          aes(geometry = geometry, fill = b),
          color = NA) +
  scale_fill_gradient(low="white", high="#0099FF") +
  geom_sf(data = toy ,
          aes(geometry = geometry, fill = a),
          color = NA) +
  scale_fill_manual(values=c(alpha("#CCCCCC",0), alpha("#990033",1)))
#> Scale for 'fill' is already present. Adding another scale for 'fill', which
#> will replace the existing scale.
#> Error: Continuous value supplied to discrete scale

要执行代码,请用途:

library(sf)
library(ggplot2)

我的玩具数据结构如下:

structure(list(a = structure(c(1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 
1L, 2L), .Label = c("0", "1"), class = "factor"), b = c(0.22798252850771, 
0.743008424993604, 0.388169342186302, 0.907995192101225, 0.953832301311195, 
0.40455531864427, 0.363415864063427, 0.805322113214061, 0.519045175751671, 
0.484226288506761), geometry = structure(list(structure(list(
    structure(c(1223540.00733145, 1223540.00733145, 1224040.00733145, 
    1224040.00733145, 1223540.00733145, 6046527.88033381, 6047027.88033381, 
    6047027.88033381, 6046527.88033381, 6046527.88033381), .Dim = c(5L, 
    2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
    structure(c(1224040.00733145, 1224040.00733145, 1224540.00733145, 
    1224540.00733145, 1224040.00733145, 6046527.88033381, 6047027.88033381, 
    6047027.88033381, 6046527.88033381, 6046527.88033381), .Dim = c(5L, 
    2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
    structure(c(1224540.00733145, 1224540.00733145, 1225040.00733145, 
    1225040.00733145, 1224540.00733145, 6046527.88033381, 6047027.88033381, 
    6047027.88033381, 6046527.88033381, 6046527.88033381), .Dim = c(5L, 
    2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
    structure(c(1225040.00733145, 1225040.00733145, 1225540.00733145, 
    1225540.00733145, 1225040.00733145, 6046527.88033381, 6047027.88033381, 
    6047027.88033381, 6046527.88033381, 6046527.88033381), .Dim = c(5L, 
    2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
    structure(c(1223540.00733145, 1223540.00733145, 1224040.00733145, 
    1224040.00733145, 1223540.00733145, 6047027.88033381, 6047527.88033381, 
    6047527.88033381, 6047027.88033381, 6047027.88033381), .Dim = c(5L, 
    2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
    structure(c(1224040.00733145, 1224040.00733145, 1224540.00733145, 
    1224540.00733145, 1224040.00733145, 6047027.88033381, 6047527.88033381, 
    6047527.88033381, 6047027.88033381, 6047027.88033381), .Dim = c(5L, 
    2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
    structure(c(1224540.00733145, 1224540.00733145, 1225040.00733145, 
    1225040.00733145, 1224540.00733145, 6047027.88033381, 6047527.88033381, 
    6047527.88033381, 6047027.88033381, 6047027.88033381), .Dim = c(5L, 
    2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
    structure(c(1225040.00733145, 1225040.00733145, 1225540.00733145, 
    1225540.00733145, 1225040.00733145, 6047027.88033381, 6047527.88033381, 
    6047527.88033381, 6047027.88033381, 6047027.88033381), .Dim = c(5L, 
    2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
    structure(c(1224040.00733145, 1224040.00733145, 1224540.00733145, 
    1224540.00733145, 1224040.00733145, 6047527.88033381, 6048027.88033381, 
    6048027.88033381, 6047527.88033381, 6047527.88033381), .Dim = c(5L, 
    2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
    structure(c(1224540.00733145, 1224540.00733145, 1225040.00733145, 
    1225040.00733145, 1224540.00733145, 6047527.88033381, 6048027.88033381, 
    6048027.88033381, 6047527.88033381, 6047527.88033381), .Dim = c(5L, 
    2L))), class = c("XY", "POLYGON", "sfg"))), class = c("sfc_POLYGON", 
"sfc"), precision = 0, bbox = structure(c(xmin = 1223540.00733145, 
ymin = 6046527.88033381, xmax = 1225540.00733145, ymax = 6048027.88033381
), class = "bbox"), crs = structure(list(input = NA_character_, 
    wkt = "PROJCS[\"RGF_1993_Lambert_93\",\n    GEOGCS[\"GCS_RGF_1993\",\n        DATUM[\"Reseau_Geodesique_Francais_1993\",\n            SPHEROID[\"GRS_1980\",6378137.0,298.257222101]],\n        PRIMEM[\"Greenwich\",0.0],\n        UNIT[\"Degree\",0.017453292519943295]],\n    PROJECTION[\"Lambert_Conformal_Conic_2SP\"],\n    PARAMETER[\"False_Easting\",700000.0],\n    PARAMETER[\"False_Northing\",6600000.0],\n    PARAMETER[\"Central_Meridian\",3.0],\n    PARAMETER[\"Standard_Parallel_1\",49.0],\n    PARAMETER[\"Standard_Parallel_2\",44.0],\n    PARAMETER[\"Latitude_Of_Origin\",46.5],\n    UNIT[\"Meter\",1.0]]"), class = "crs"), n_empty = 0L)), row.names = c(NA, 
10L), sf_column = "geometry", agr = structure(c(a = NA_integer_, 
b = NA_integer_), .Label = c("constant", "aggregate", "identity"
), class = "factor"), class = c("sf", "tbl_df", "tbl", "data.frame"
))
kx1ctssn

kx1ctssn1#

问题可能是由于我们想为两个不同的变量定义两个不同的fill尺度:a是分类的,b是连续的。要做到这一点,我们可以使用小(但非常有用)ggnewscale库:https://eliocamp.github.io/ggnewscale/,允许我们在同一图中重新定义新的比例 *:

install.packages("ggnewscale")
library(ggnewscale)

ggplot(data = toy) + 
   geom_sf(aes(geometry = geometry, fill = a)) + 
   scale_fill_manual(values = c("#FFFFFF00", "red")) +
   new_scale_fill() + 
   geom_sf(aes(geometry = geometry, fill = b, alpha = 0.5))

注意在绘制下一层之前的new_scale_fill()调用,这允许我们对第二层使用不同的比例。

bakd9h0s

bakd9h0s2#

一种解决方案是使用color选项
这将是没有意义的有多个填充规模在同一个地方与一些透明度,我认为这将使它难以阅读。在我看来,有了颜色,它就很容易阅读了

plot1 = ggplot() +
  geom_sf(data = toy,
          aes(geometry = geometry, fill = b, color=a)) +
  scale_fill_gradient(low="white", high="#0099FF") +
  scale_color_manual(values=c(alpha("#CCCCCC",0), alpha("#990033",1)))
plot1

xuo3flqw

xuo3flqw3#

对于这种特定的情况,您可能希望考虑作为替代方案的二元Map,可以使用{bivariate}创建:

library(sf)
library(ggplot2)
library(cowplot)
library(biscale)

# Prepare bivariate
toy2 <- bi_class(toy, x = a, y = b, style = "quantile", dim = 2)

map <- ggplot() +
  geom_sf(data = toy2, aes(fill = bi_class),
          color = NA, show.legend = FALSE) +
  bi_scale_fill(pal = "GrPink", dim = 2) +
  # bi_theme() +
  theme(plot.margin = margin(r=80))

legend <- bi_legend(pal = "GrPink",
            dim = 2,
            xlab = "a ",
            ylab = "b ",
            size = 8)

ggdraw() +
  draw_plot(map, 0, 0, 1, 1) +
  draw_plot(legend, 0.75, .5, 0.3, 0.3)

相关问题