本文介绍了在Shiny的地图上绘制散点图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在地图上绘制散点图?我设法绘制了散点图,但是我希望将其绘制在地图上.我相信一种选择是使用传单包,因为我具有经度和纬度坐标,但是我不知道如何使用它.请,如果您有其他选择,请随时.你能帮我解决这个问题吗??可执行代码如下.
how do I plot my scatterplot on a map? I managed to plot my scatterplot, however I wanted it to be plotted on a map. I believe that an option is to use the leaflet package, since I have the Latitude and Longitude coordinates, but I don't know how to use it. Please, if you have other options feel free. Could you help me with this problem ?? The executable code is below.
非常感谢!
library(shiny) library(ggplot2) library(rdist) library(geosphere) library(kableExtra) library(readxl) library(tidyverse) library(DT) #database df<-structure(list(Properties = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35), Latitude = c(-23.8, -23.8, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, + -23.9, -23.9, -23.9, -23.9, -23.9,-23.9,-23.9,-23.9,-23.9,-23.9,-23.9,-23.9,-23.9,-23.9,-23.9,-23.9,-23.9,-23.9,-23.9,-23.9,-23.9), Longitude = c(-49.6, -49.6, -49.6, -49.6, -49.6, -49.6, -49.6, -49.6, -49.6, -49.6, -49.7, + -49.7, -49.7, -49.7, -49.7, -49.6, -49.6, -49.6, -49.6,-49.6,-49.6,-49.6,-49.6,-49.6,-49.6,-49.6,-49.6,-49.6,-49.6,-49.6,-49.6,-49.6,-49.6,-49.6,-49.6), Waste = c(526, 350, 526, 469, 285, 175, 175, 350, 350, 175, 350, 175, 175, 364, + 175, 175, 350, 45.5, 54.6,350,350,350,350,350,350,350,350,350,350,350,350,350,350,350,350)), class = "data.frame", row.names = c(NA, -35L)) function.clustering<-function(df,k,Filter1,Filter2){ if (Filter1==2){ Q1<-matrix(quantile(df$Waste, probs = 0.25)) Q3<-matrix(quantile(df$Waste, probs = 0.75)) L<-Q1-1.5*(Q3-Q1) S<-Q3+1.5*(Q3-Q1) df_1<-subset(df,Waste>L[1]) df<-subset(df_1,Waste<S[1]) } #cluster coordinates<-df[c("Latitude","Longitude")] d<-as.dist(distm(coordinates[,2:1])) fit.average<-hclust(d,method="average") #Number of clusters clusters<-cutree(fit.average, k) nclusters<-matrix(table(clusters)) df$cluster <- clusters #Localization center_mass<-matrix(nrow=k,ncol=2) for(i in 1:k){ center_mass[i,]<-c(weighted.mean(subset(df,cluster==i)$Latitude,subset(df,cluster==i)$Waste), weighted.mean(subset(df,cluster==i)$Longitude,subset(df,cluster==i)$Waste))} coordinates$cluster<-clusters center_mass<-cbind(center_mass,matrix(c(1:k),ncol=1)) #Coverage coverage<-matrix(nrow=k,ncol=1) for(i in 1:k){ aux_dist<-distm(rbind(subset(coordinates,cluster==i),center_mass[i,])[,2:1]) coverage[i,]<-max(aux_dist[nclusters[i,1]+1,])} coverage<-cbind(coverage,matrix(c(1:k),ncol=1)) colnames(coverage)<-c("Coverage_meters","cluster") #Sum of Waste from clusters sum_waste<-matrix(nrow=k,ncol=1) for(i in 1:k){ sum_waste[i,]<-sum(subset(df,cluster==i)["Waste"]) } sum_waste<-cbind(sum_waste,matrix(c(1:k),ncol=1)) colnames(sum_waste)<-c("Potential_Waste_m3","cluster") #Output table data_table <- Reduce(merge, list(df, coverage, sum_waste)) data_table <- data_table[order(data_table$cluster, as.numeric(data_table$Properties)),] data_table_1 <- aggregate(. ~ cluster + Coverage_meters + Potential_Waste_m3, data_table[,c(1,7,6,2)], toString) #Scatter Plot suppressPackageStartupMessages(library(ggplot2)) df1<-as.data.frame(center_mass) colnames(df1) <-c("Latitude", "Longitude", "cluster") g<-ggplot(data=df, aes(x=Longitude, y=Latitude, color=factor(clusters))) + geom_point(aes(x=Longitude, y=Latitude), size = 4) Centro_View<- g + geom_text(data=df, mapping=aes(x=eval(Longitude), y=eval(Latitude), label=Waste), size=3, hjust=-0.1)+ geom_point(data=df1, mapping=aes(Longitude, Latitude), color= "green", size=4) + geom_text(data=df1, mapping = aes(x=Longitude, y=Latitude, label = 1:k), color = "black", size = 4) plotGD<-print(Centro_View + ggtitle("Scatter Plot") + theme(plot.title = element_text(hjust = 0.5))) return(list( "Data" = data_table_1, "Plot" = plotGD )) } ui <- bootstrapPage( navbarPage(theme = shinytheme("flatly"), collapsible = TRUE, "Clustering", tabPanel("General Solution", sidebarLayout( sidebarPanel( radioButtons("filtro1", h3("Select properties"), choices = list("All properties" = 1, "Exclude properties" = 2), selected = 1), tags$b(h5("(a) Choose other filters")), tags$b(h5("(b) Choose clusters")), sliderInput("Slider", h5(""), min = 2, max = 8, value = 5) ), mainPanel( tabsetPanel( tabPanel("Solution", plotOutput("ScatterPlot")))) )))) server <- function(input, output, session) { Modelclustering<-reactive(function.clustering(df,input$Slider,1,1)) output$ScatterPlot <- renderPlot({ Modelclustering()[[2]] }) observeEvent(input$Slider,{ updateSelectInput(session,'select', choices=unique(df[df==input$Slider])) }) } shinyApp(ui = ui, server = server)非常感谢!
推荐答案我可以想到一些可能对您有所帮助的事情.
I can think of a couple things that may help you.
library(shiny) library(ggplot2) useri <- shinyUI(pageWithSidebar( headerPanel("Reactive Plot"), sidebarPanel( selectInput('x','X-Axis',names(iris)), selectInput('y','Y-Axis',names(iris)), selectInput('color','Color',c('None',names(iris[5])))), mainPanel(uiOutput("plotui"),dataTableOutput("plot_brushed_points")))) serveri <- shinyServer(function(input,output) { output$plot <- renderPlot({ p <- ggplot(iris,aes_string(x=input$x, y=input$y))+geom_point()+theme_bw() if(input$color != 'None') p <- p + aes_string(color=input$color) print(p) }) output$plotui <- renderUI(plotOutput("plot",brush = brushOpts("plot_brush"))) output$plot_brushed_points <- renderDataTable(brushedPoints(iris,input$plot_brush,input$x,input$y), options=list(searching=FALSE, paging = FALSE)) }) shinyApp(useri, serveri)
还...
library(shiny) library(shinydashboard) library(shinyjs) library(glue) ui <- dashboardPage( dashboardHeader(), dashboardSidebar(selectInput("cols", NULL, c(2, 3, 4, 6, 12), 4)), dashboardBody( useShinyjs(), div( box(solidHeader = TRUE, title = "Box", width = 4, status = "info", sliderInput("sld", "n:", 1, 100, 50), plotOutput("plt") ), id = "box-parent") )) server <- function(input, output) { observe({ cols <- req(input$cols) runjs(code = glue('var $el = $("#box-parent > :first");', '$el.removeClass(function (index, className) {{', 'return (className.match(/(^|\\s)col-sm-\\d+/g) || []).join(" ")', '}});', '$el.addClass("col-sm-{cols}");')) }) output$plt <- renderPlot(plot(rnorm(input$sld), rnorm(input$sld))) } shinyApp(ui, server)
更多推荐
在Shiny的地图上绘制散点图
发布评论