0

I am trying to build a map that contains a few points overlayed onto rivers. I am using code adapted from https://milospopovic.net/map-rivers-with-sf-and-ggplot2-in-r/, but I downloaded the North America map and substituted

  st_polygon(list(cbind(
    c(-84.9, -84.9, -84.0, -84.0, -84.9),
    c(34.4, 35.05, 35.05, 34.4, 34.4)
    ))),
  crs = crsLONGLAT)

to create my bounding box.

test.point <- data.frame("x"= -84.73405, "y"= 35.00988)
p <- 
    ggplot()+
    geom_point(data= test.point, aes(x=x, y=y), color= "black", fill= "black") +
    geom_sf(data=na_riv, aes(color=factor(ORD_FLOW), size=width)) +
    coord_sf(crs = 4087,
      xlim = c(bbox["xmin"], bbox["xmax"]), 
      ylim = c(bbox["ymin"], bbox["ymax"])) +
    labs(x= "", subtitle="",
         title="Rivers of the Southeast",
         caption="©2022\nSource: ©World Wildlife Fund, Inc. (2006-2013)\n HydroSHEDS database http://www.hydrosheds.org") +
    scale_color_manual(
        name = "",
        values = c('#081f6b', '#08306b', '#08519c', '#2171b5', '#4292c6', '#6baed6', '#9ecae1', '#c6dbef', '#deebf7')) +
    scale_size(range=c(0, .3)) +
    scale_alpha_manual(values=c("2"= 1, "3" = 1, "4" = 1, "5" = .7, "6" = .6, "7" = .4, "8" = .3, "9" = .2, "10" = .1)) +
    theme_bw() +
    theme(text = element_text(family = "georg"),
      panel.background = element_blank(), 
      legend.background = element_blank(),
      legend.position = "none",
      panel.border = element_blank(),
      panel.grid.minor = element_blank(),
      panel.grid.major = element_blank(),
      plot.title = element_text(size=40, color="#2171b5", hjust=0.5, vjust=0),
      plot.subtitle = element_text(size=14, color="#ac63a0", hjust=0.5, vjust=0),
      plot.caption = element_text(size=10, color="grey60", hjust=0.5, vjust=10),
      legend.text = element_text(size=9, color="grey20"),
      legend.title = element_text(size=10, color="grey20"),
      strip.text = element_text(size=12),
      plot.margin = unit(c(t=1, r=0, b=-1, l=-1),"lines"))
p

The above code yields a pretty map of all of the rivers but the point doesn't show up. :(

jstalker
  • 3
  • 2
  • 1
    Does it show up if you draw the point _after_ drawing the map? At the moment it looks like you are drawing the map over the point, since geom_sf comes after geom_point in your code. – Allan Cameron Sep 28 '22 at 23:22
  • No, it doesn't. I had the geom_point after the geom_sf for nearly all of my troubleshooting and happened to move it in a desperate attempt before reaching out for help. – jstalker Sep 29 '22 at 14:16

1 Answers1

1

Here is one way to get around this. Basically you can convert the points you want to highlight a a simple feature geometry:

test.point <- data.frame(x = c(-84.73405, -84.6), y = c(35.00988, 34.6))

new_test.point <- st_sfc(st_multipoint(as.matrix(test.point)), crs = crsLONGLAT)

Then, I just filtered the data to have only the rivers present in the area of interested:

nariv = nariv %>% mutate(inter = as.integer(st_intersects(geometry, bbox)))

nariv_filt = nariv %>% filter(!is.na(inter))

And create the plot:

enter image description here

ggplot() +
    geom_sf(data = nariv_filt, aes(color = factor(ORD_FLOW), size = width, alpha = width)) +
    geom_sf(data = new_test.point, color="red", size=5)+
    coord_sf(crs = prj, xlim = c(bb["xmin"], bb["xmax"]),ylim = c(bb["ymin"], bb["ymax"])) +
    geom_point(data = test.point, aes(x=x, y=y), color="red", size=5)+
    labs(y = "", subtitle = "", x = "", title = "Rivers of North and Central America") +
    scale_color_manual(name = "",
        values = c(
            "#08306b", "#1c4680", "#305d94", "#4574a7",
            "#5d8cb9", "#77a4cb", "#deebf7", "#deebf7", "#deebf7"
        ), drop = F) +
    scale_alpha(range = c(0.3, 0.8)) +
    scale_size(range = c(0, 3)) +
    theme_minimal() +
    theme(
        text = element_text(family = "Montserrat"),
        panel.background = element_blank(),
        legend.background = element_blank(),
        legend.position = "none",
        panel.border = element_blank(),
        #panel.grid.minor = element_blank(),
        #panel.grid.major = element_blank(),
        #plot.title = element_text(size = 100, color = "#2171b5", hjust = 0.5, vjust = -22),
        #plot.subtitle = element_text(size = 14, color = "#ac63a0", hjust = 0.5, vjust = 0),
        #plot.caption = element_text(size = 40, color = "grey60", hjust = 0.5, vjust = 120),
        #axis.title.x = element_text(size = 10, color = "grey20", hjust = 0.5, vjust = -6),
        legend.text = element_text(size = 9, color = "grey20"),
        legend.title = element_text(size = 10, color = "grey20"),
        strip.text = element_text(size = 12),
        #plot.margin = unit(c(t = -2.5, r = -10, b = -10, l = -10), "lines"),
        axis.title.y = element_blank(),
        axis.ticks = element_blank(),
        #axis.text.x = element_blank(),
        #axis.text.y = element_blank()
    )
Bushidov
  • 713
  • 4
  • 16