I am trying to add inset of my polygon into a map.
Page 11 of this pdf (https://journal.r-project.org/archive/2013-1/kahle-wickham.pdf) shows what im trying to get. Unfortunately I am new in R and can`t understand how it works even with this example... In my case it shows up this error: Error in f(...) : could not find function "viewport" which I dont understand. My code
swd <- read.csv2("C:/MapyR/rozmieszczenieswd/swdwsp.csv",header= TRUE, sep = ";", encoding = "unknow")
swd$Lat <- as.numeric((strtrim(swd$Lat,8)))
swd$Long <- as.numeric((strtrim(swd$Long,8)))
polska <- get_map(
location=c("Olsztyn"),
zoom=12,
maptype="roadmap"
)
olsztyn <- subset(swd, Lat>=53.72 & Lat<= 53.84 & Long>=20.40 & Long<=20.55,select = rodzaj:Long)
mapa <- ggmap(polska, extent = "device", legend ="bottomright")
punkty <- stat_density_2d( aes(x=Long,y=Lat,fill= ..level..,alpha=..level..)
,data=olsztyn
,size=2
,geom = "polygon"
)
nowa <- mapa + inset(
grob = ggplotGrob(ggplot()+punkty + theme_inset()),
xmin = 20.50, xmax = 20.55, ymin = 53.72, ymax = 53.76 #I tryed many versions of x,y
)
sciezka <- file.path("C:","MapyR","proby density",paste("inset",".png"), sep="")
png(file=sciezka, width = 2500 , height=1567)
print(nowa)
dev.off()
This is how far I can get with my code, Still I cant add this inset: