problem with arrows plotted in a map

44 views
Skip to first unread message

gafna jeff

unread,
Sep 18, 2020, 9:34:14 AM9/18/20
to ggplot2
Hello everybody,

Am plotting centroid change in my study area using ggplot2 package. Am plotting the shift in my species centroids from current scenario to scenario 4.5 in 2050s, from 4.5 in 2050s to 4.5 in 2070s, from 4.5 in 2070s to 8.5 in 2050s etc (short data frame in my script). But I tend to get arrows that project outside my study area. What am I doing wrong. Lastly, how could I add a legend to this diagram. Below is my code 

library(ggplot2)

library(maptools)

library(plyr)

library(ggplot2)

library(rgdal)

###Get the map of Kenya 

Kenya  <- getData("GADM",country="KEN",level=1)

##get my study region in Kenya. The region is called Samburu county

samburu_county <-Kenya[Kenya@data$NAME_1 == "Samburu",]

plot(samburu_county)

###try with ggplot2 package 

AG <- fortify(samburu_county)

names(AG)

##Create a data frame showing the centroid shift coordinates

d=data.frame(x=c(36.7879059,36.7161243,36.68916147,36.68338123), y=c(1.224791,1.1976621,1.19689772,1.20994116), vx=c(36.7161243,36.68916147,36.68338123,36.64277344), vy=c(1.1976621,1.19689772,1.20994116,1.12825521))

#plot using ggplot2 package

ggplot() + geom_polygon(data=AG, aes(long, lat, group = group), colour = alpha("darkred", 1/2), size = 0.7, fill = 'skyblue', alpha = .3) + geom_segment(data = d, mapping = aes(x=x, y=y, xend=x+vx, yend=y+vy), arrow=arrow(), size=2, color="blue") + geom_point(data=d, mapping=aes(x=x, y=y), size=4, shape=21, fill="white") 




Reply all
Reply to author
Forward
0 new messages