在r(合并函数)中绘制点的轨迹

时间:2018-12-04 09:03:19

标签: r ggplot2 leaflet

rm(list=ls())
setwd("C:/users/bok/Desktop")

library(leaflet)
library(readxl)
library(ggplot2)

myData<-read_excel("gggg.xlsx", sheet="Sheet4")


leaflet(structure(list(lat=as.numeric(myData$x), lon=as.numeric(myData$y)))) %>%
  addPolylines(lng=~lon, lat=~lat)%>%
  ggplot(myData, aes(x,y))+
  annotate("rect",xmin=Inf,xmax=0,ymin=Inf,ymax=0, fill= "red")  + 
  annotate("rect",xmin=-Inf,xmax=0,ymin=-Inf,ymax=0 , fill= "blue") + 
  annotate("rect",xmin=0,xmax=Inf,ymin=0, ymax=-Inf, fill= "yellow") + 
  annotate("rect",xmin=0,xmax=-Inf,ymin=Inf,ymax=0, fill= "green") + 
  geom_point()+xlim(-10,10)+ylim(-10,10)

myData只是x,y坐标。.结果只是带有彩色背景点的象限。 如何合并两个功能leafet()和ggplot()?

enter image description here

1 个答案:

答案 0 :(得分:0)

myData<-read_excel("gggg.xlsx", sheet="Sheet4")


myfunc<-function(df){
  ggplot(df, aes(x,y))+
  annotate("rect",xmin=Inf,xmax=0,ymin=Inf,ymax=0, fill= "red")  + 
  annotate("rect",xmin=-Inf,xmax=0,ymin=-Inf,ymax=0 , fill= "blue") + 
  annotate("rect",xmin=0,xmax=Inf,ymin=0, ymax=-Inf, fill= "yellow") + 
  annotate("rect",xmin=0,xmax=-Inf,ymin=Inf,ymax=0, fill= "green") + 
  geom_point()+xlim(-10,10)+ylim(-10,10)
}


leaflet(structure(list(lat=as.numeric(myData$x), lon=as.numeric(myData$y)))) %>%
  addTiles(myfunc(myData)) %>%
  addPolylines(lng=~lon, lat=~lat, color=~col)