具有特定点颜色和大小的GGPLOT SF变量因子,带有SF几何。

发布于 2025-02-12 15:54:16 字数 1656 浏览 1 评论 0 原文

我有一个,带有lat lon ( SHP_4283< -sf :: ST_TRANSFORM(SHP,CRS = 4283)) 和3个变量,我想绘制单独的 $ bistrate 要分离颜色和 $ GEOMETRY 位置的因素。

使用geom_sf ..

    ggplot() +
  
  geom_sf(data = subset(shp_4283, Substrate == "Sand", show.legend = "point"), #aes(shape = YOU), 
              color = "yellow", size = 1) +
  geom_sf(data = subset(shp_4283, Substrate == "Mixed reef and sand", show.legend = "point"), #aes(shape = YOU),
             color = "green", size = 1) +
  geom_sf(data = subset(shp_4283, Substrate == "None modelled with certainty", show.legend = "point"), #aes(shape = YOU),
             color = "grey", size = 2) +
  geom_sf(data = subset(shp_4283, Substrate == "Reef", show.legend = "point"), #aes(shape = YOU),
             color = "black", size = 2, show.legend = T) +
    coord_sf()

绘图有效,但没有传说为no aes() set ..但是,由于“ x [j]中的错误:无效的下标类型'list'列表'”

i i了解要创建一个新的 df 过滤每个因素及其几何形状,然后从..

df <- shp_4283 %>%
  # Your filter
  filter(Substrate == "Reef") %>%
  # 2 Extract coordinates
  st_coordinates() %>%
  # 3 to table /tibble
  as.data.frame() %>% 
**is this where i would code the 'column names' so that each 
filtered $Substrate factor in a new df would be labelled appropriately?**

但是否有一个 geom _ .. 用使用SF DF绘制单独可变因子的方法它是几何..和传说将颜色映射到因子?

i have a shp file , with lat lon
( shp_4283 <- sf::st_transform(shp, crs = 4283) )
and 3 variables, of which i would like to plot the separate
$Substrate factors to separate colours and to their $geometry locations.

with geom_sf..

    ggplot() +
  
  geom_sf(data = subset(shp_4283, Substrate == "Sand", show.legend = "point"), #aes(shape = YOU), 
              color = "yellow", size = 1) +
  geom_sf(data = subset(shp_4283, Substrate == "Mixed reef and sand", show.legend = "point"), #aes(shape = YOU),
             color = "green", size = 1) +
  geom_sf(data = subset(shp_4283, Substrate == "None modelled with certainty", show.legend = "point"), #aes(shape = YOU),
             color = "grey", size = 2) +
  geom_sf(data = subset(shp_4283, Substrate == "Reef", show.legend = "point"), #aes(shape = YOU),
             color = "black", size = 2, show.legend = T) +
    coord_sf()

the plot works, but with no legend as no aes() set.. but then further errors occur due to "Error in x[j] : invalid subscript type 'list'"

I understand to create a new df filtering each factor and its geometry to then plot from..

df <- shp_4283 %>%
  # Your filter
  filter(Substrate == "Reef") %>%
  # 2 Extract coordinates
  st_coordinates() %>%
  # 3 to table /tibble
  as.data.frame() %>% 
**is this where i would code the 'column names' so that each 
filtered $Substrate factor in a new df would be labelled appropriately?**

but is there a geom_.. way to plot separate variable factor from the sf df with its geometry.. and the legend mapping the color to the factor?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪若未夕 2025-02-19 15:54:16

geom_sf()&amp; subset()&amp;显式层调用
逐渐较大的数据组之上

Col = c('green','grey','black','yellow') #define data-group colours


jr_map <-  ggplot() +
        geom_sf(data = shp_4283, aes(color = Substrate)) + # to map all data-groups to the plot, and more importantly, the legend.. legend appears now.. but not correct color-mapping
# individually map the separate groups, by factor, add point size, and legend visibility
  geom_sf(data = subset(shp_4283, Substrate == "Sand", show.legend = "point"), color = "yellow", size = 1) +
  geom_sf(data = subset(shp_4283, Substrate == "Mixed reef and sand", show.legend = "point"), color = "green", size = 1) +
  geom_sf(data = subset(shp_4283, Substrate == "None modelled with certainty", show.legend = "point"), color = "grey", size = 2) +
  geom_sf(data = subset(shp_4283, Substrate == "Reef", show.legend = "point"), color = "black", size = 2) +
# retrieve geometry 
  coord_sf() +
# add label for map projection/CRS
  annotate(geom = "text", x = 115.095:115.1, y = -33.62:-33.6, label = "crs = 4283", 
           fontface = "italic", color = "grey22", size = 2.5) + 
  #geom_text(data=bb_df, aes(x=115,y=35), fill='black', color='black', alpha=.1, angle=35) + # incase an on-map descriptive label needed
# add north arrow
annotation_north_arrow(location = "bl", which_north = "true", 
                         pad_x = unit(0.25, "cm"), 
                         pad_y = unit(0.25, "cm"),
                         height = unit(0.6, "cm"), width = unit(0.6, "cm"),
                         style = north_arrow_fancy_orienteering) +
  theme_bw() + # remove 'azure' ocean color
  ggtitle("Jolly-Rog Drops" , subtitle = "Geographe Bay, Western Australia") +
  xlab("Longitude") +
  ylab("Latitude") +
  ## Legend : CUSTOM
  #Title, colors (Col specified manually at start..ordered according to $Substrate listing), 
  scale_color_manual(name = "Substrate", values = Col) +
  guides(color = guide_legend(override.aes = list(shape = c(2,2,2,2), size = 1, fill = Col), nrow = 2, byrow = TRUE, legend.text = element_text(size=0.2)) ) + # size and shape NOT WORKING!? HELP..
  #theme(legend.position = c(0, 0))                                 
  theme(legend.position = "bottom")

jr_map

要查看最小的数据组最后,在此帖子的 ( geom_解释

不同的变量因子( $ bistrate )是1。地理绘制,它们的2.各自的可见性很容易调节,取决于 layer ,它们是从初始编码的。 ggplot()呼叫(最远/最后最突出)。然后, geom_sf()点(不是 geom_point()),通过 color shape )和<强>点的大小 alpha = 也可用)。

geom_sf() & subset() & explicit layer call
to view the smallest data-group last, on top of the progressively larger data-groups beneath

Col = c('green','grey','black','yellow') #define data-group colours


jr_map <-  ggplot() +
        geom_sf(data = shp_4283, aes(color = Substrate)) + # to map all data-groups to the plot, and more importantly, the legend.. legend appears now.. but not correct color-mapping
# individually map the separate groups, by factor, add point size, and legend visibility
  geom_sf(data = subset(shp_4283, Substrate == "Sand", show.legend = "point"), color = "yellow", size = 1) +
  geom_sf(data = subset(shp_4283, Substrate == "Mixed reef and sand", show.legend = "point"), color = "green", size = 1) +
  geom_sf(data = subset(shp_4283, Substrate == "None modelled with certainty", show.legend = "point"), color = "grey", size = 2) +
  geom_sf(data = subset(shp_4283, Substrate == "Reef", show.legend = "point"), color = "black", size = 2) +
# retrieve geometry 
  coord_sf() +
# add label for map projection/CRS
  annotate(geom = "text", x = 115.095:115.1, y = -33.62:-33.6, label = "crs = 4283", 
           fontface = "italic", color = "grey22", size = 2.5) + 
  #geom_text(data=bb_df, aes(x=115,y=35), fill='black', color='black', alpha=.1, angle=35) + # incase an on-map descriptive label needed
# add north arrow
annotation_north_arrow(location = "bl", which_north = "true", 
                         pad_x = unit(0.25, "cm"), 
                         pad_y = unit(0.25, "cm"),
                         height = unit(0.6, "cm"), width = unit(0.6, "cm"),
                         style = north_arrow_fancy_orienteering) +
  theme_bw() + # remove 'azure' ocean color
  ggtitle("Jolly-Rog Drops" , subtitle = "Geographe Bay, Western Australia") +
  xlab("Longitude") +
  ylab("Latitude") +
  ## Legend : CUSTOM
  #Title, colors (Col specified manually at start..ordered according to $Substrate listing), 
  scale_color_manual(name = "Substrate", values = Col) +
  guides(color = guide_legend(override.aes = list(shape = c(2,2,2,2), size = 1, fill = Col), nrow = 2, byrow = TRUE, legend.text = element_text(size=0.2)) ) + # size and shape NOT WORKING!? HELP..
  #theme(legend.position = c(0, 0))                                 
  theme(legend.position = "bottom")

jr_map

also thanks to this post (geom_ explanation)

the different variable factors ($Substrate) are 1. geo-plotted, and their 2. respective visibility, is easily adjustable, dependent on the layer at which they are coded from initial ggplot() call (farthest/last most prominent). The geom_sf() points (not geom_point()) are then made prominent through colour, shape, and size of point (alpha = also available).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文