首页 > 解决方案 > 使用 ggplot 和 geom_sf 在地图上发布更改点(填充和形状)

问题描述

使用 ggplot2 (v 3.2.1) 和 sf (v. 0.7-7) 我可以轻松地绘制原始数据的地图,其中包含我选择的形状的彩色点,没有问题:

  P1 <- ggplot() +
  geom_sf(data = Map.nb) +
  geom_sf(data = Df_raw, shape = 22, size = 2, fill = 'purple') +
  theme_bw()

在此处输入图像描述 在上面绘制的原始数据中,每个位置都有多行数据(例如,每个站点三个陷阱)。我创建了第二个数据框,使用 dplyr (v. 0.8.3) 总结每个站点的数据

Df.summarize <- Df_raw %>%
  group_by(plot_id) %>% 
  summarise(moth_per_plot = sum(moth_count), traps = n_distinct(trap)) %>% 
  mutate (avg_moth_per_trap = moth_per_plot/traps)

如果我绘制汇总数据,它映射得很好,但我不能改变点的形状或填充。

P2 <- ggplot() +
  geom_sf(data = Map.nb) +
  geom_sf(data = Df.summarize, shape = 22, size = 2, fill = 'purple') +
  theme_bw()

在此处输入图像描述 奇怪的是,如果我fill = 'purple'col = 'purple'点的轮廓替换颜色会改变(如预期的那样)。我不知道为什么我可以改变col但不能fillshape

有谁知道如何更改汇总数据图中的填充和形状?或者有什么解释为什么它适用于我的原始数据而不适用于汇总数据?

注意: class(Df.summarize)是 'sf', 'tbl_df', 'tbl' & 'data.frame' class(Df.raw)是 'sf' & 'data.frame'

我已经将类转换Df.summarize为 match Df.raw,但这没有帮助。

编辑str()每个数据框:

str(Df_raw)
Classes ‘sf’ and 'data.frame':  390 obs. of  3 variables:
 $ plotid    : chr  "4592 6665" "4566 6698" "4546 6653" "4571 6617" ...
 $ moth_count: num  22 41 4 20 129 1 8 2 95 35 ...
 $ SHAPE     :sfc_POINT of length 390; first list element:  'XY' num  2487757 7436473
 - attr(*, "sf_column")= chr "SHAPE"
 - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA
  ..- attr(*, "names")= chr  "plotid" "moth_count"


str(Df_summarized)
Classes ‘sf’, ‘tbl_df’, ‘tbl’ and 'data.frame': 156 obs. of  5 variables:
 $ plotid           : chr  "4538 6587" "4538 6715" "4545 6561" "4546 6653" ...
 $ moth_per_plot    : num  7 135 17 12 2 18 59 152 13 19 ...
 $ traps            : int  3 3 2 2 2 3 3 3 3 3 ...
 $ SHAPE            :sfc_GEOMETRY of length 156; first list element:  'XY' num [1:3, 1:2] 2548882 2548899 2548920 7376785 7376824 ...
 $ avg_moth_per_trap: num  2.33 45 8.5 6 1 ...
 - attr(*, "sf_column")= chr "SHAPE"
 - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA
  ..- attr(*, "names")= chr  "plotid" "moth_per_plot" "traps" "avg_moth_per_trap"

标签: rggplot2dplyrsf

解决方案


推荐阅读