0

I want to remove little segments (data = station_EVHOE) that are outside a zone. the map here

For this, I drew a black segment which demarcates the relevant zone (which is the region on the right). So I want to remove points which are in the left zone.

My code is made with ggplot :

 d <- ggplot() + 
  coord_map(xlim = c(-12,-1), ylim = c(43,52)) +
  geom_polygon(aes(x=longitude, y=latitude), data = coast_EVHOE) + 
  geom_segment(aes(x = longitude_début, y = latitude_début, xend = longitude_fin, yend = latitude_fin, colour = as.factor(annee)), data = station_EVHOE) + 
  geom_segment(aes(x = -4.374794, y = 47.7975, xend = -7.8694, yend = 43.773630))

So, is it possible to extract the coordinates of the black segment, in order to remove points outside of the right area ?

Loulou
  • 703
  • 5
  • 17
  • 1
    Sure, but you have to do it outside of ggplot2. See `rgeos` and `sp::over`. E.g. http://stackoverflow.com/questions/19002744/spover-for-point-in-polygon-analysis – Roman Luštrik Feb 02 '17 at 13:22

1 Answers1

1

Here an idea based on this: https://math.stackexchange.com/questions/274712/calculate-on-which-side-of-a-straight-line-is-a-given-point-located

#determine which station are on the right side of the line
#I use only one point, you can adapt to check if the two point of the station are on the right side of the plot

station_EVHOE$right_side = 
  ((station_EVHOE$longitude_début + 4.374794)*(43.773630 - 47.7975)) - 
  ((station_EVHOE$latitude_début - 47.7975)*(-7.8694 + 4.374794)) < 0

d <- ggplot() + 
  coord_map(xlim = c(-12,-1), ylim = c(43,52)) +
  geom_polygon(aes(x=longitude, y=latitude), data = coast_EVHOE) + 

# plot only the station at the right side of the line
  geom_segment(aes(x = longitude_début, y = latitude_début, xend = longitude_fin, yend = latitude_fin, colour = as.factor(annee)), data = station_EVHOE[station_EVHOE$right_side,]) + 
  geom_segment(aes(x = -4.374794, y = 47.7975, xend = -7.8694, yend = 43.773630))
Community
  • 1
  • 1
timat
  • 1,480
  • 13
  • 17
  • It works ! I just had to change the sign "+" into "-" in the opération "(station_EVHOE$latitude_début + 47.7975)". Thank you so much timat ! – Loulou Feb 02 '17 at 15:02