2017-02-02 53 views
0

我想刪除區域外的小段(data = station_EVHOE)。 the map here我繪製的線段座標

爲此,我畫了一個黑色的線段,劃定了相關的區域(這是右邊的區域)。 所以我想刪除左側區域的點。

我的代碼與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)) 

那麼,是不是可以提取黑色分割的座標,爲了改掉正確的區域外點?

+1

當然可以,但你必須這樣做GGPLOT2之外。請參閱'rgeos'和'sp :: over'。例如。 http://stackoverflow.com/questions/19002744/spover-for-point-in-polygon-analysis –

回答

1

這裏基於這一點的想法: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)) 
+0

它的工作原理!我只需在操作「(station_EVHOE $latitude_début+ 47.7975)」中將符號「+」更改爲「 - 」。非常感謝你! – Loulou

+0

你說得對,我更新了.. – timat