我有一个SHP文件,带有lat长度(shp_4283 <- sf::st_transform(shp, crs = 4283)
)还有3个变量,我想把它们分别画出来$Substrate
因子分离颜色和它们的$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()
的情节工作,但没有传说作为没有aes()
设置。但是由于"Error in x[j]: invalid下标类型'list'">
我理解创建一个新的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与它的几何…还有将颜色映射到因子的图例?
geom_sf(),子集(),显式层调用在
下面逐渐增大的数据组之上,最后查看最小的数据组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_ explanation)
各变量因子($Substrate
)为1。地理绘图,和他们的2。各自的可见性,很容易调整,取决于层它们从初始ggplot()
调用(最远/最后最突出)开始编码。然后通过颜色突出geom_sf()
点(不是geom_point()
)。形状,,size(alpha =
也可用).