R:ggplot2和geom_地图返回;x和单位必须具有长度>;0“;尽管值已转换为因子,但仍存在错误

R:ggplot2和geom_地图返回;x和单位必须具有长度>;0“;尽管值已转换为因子,但仍存在错误,r,ggplot2,sparql,shiny,maptools,R,Ggplot2,Sparql,Shiny,Maptools,我正在开发一个原始的闪亮应用程序,可以映射项目中的一些数据。我计算出了SPARQL查询,它生成了一个类似于下面提供的摘录的数据帧 dz_label overall.quantiles S010001 8 S010002 9 我有强化的形状文件,我正试图使用以下代码来映射它们: ggplot() + geom_map(data = dta.simd, aes(map_id = dz_label, fill =

我正在开发一个原始的闪亮应用程序,可以映射项目中的一些数据。我计算出了SPARQL查询,它生成了一个类似于下面提供的摘录的数据帧

dz_label overall.quantiles
S010001 8
S010002 9
我有强化的形状文件,我正试图使用以下代码来映射它们:

ggplot() + 
      geom_map(data = dta.simd, aes(map_id = dz_label, 
                                    fill = as.factor(dta.simd$overall.quantiles)), 
               map = shps.dzs2001) +
      geom_path(data = shps.dzs2001, aes(x=long, y=lat, group=group), 
                colour="black", size=0.25)
代码返回错误

Error: 'x' and 'units' must have length > 0
如果我理解类似的讨论(,),那么问题与值类有关。然而,我的理解是,
as.factor()
应该解决这个问题,但显然情况并非如此。应用程序的完整代码包含在下面。当然,我会感激你的帮助

服务器

# Libs
require(shiny); require(SPARQL); require(ggplot2); require(rgeos); require(maptools);
require(RCurl)

# Server function
shinyServer(function(input, output) {

  # Source the data

  ## Source the SPARQL data
  ### Define endpoint URL.
  endpoint <- "http://data.opendatascotland.org/sparql.csv"

  ### Create Query and download table for the SIMD rank
  query.simd <- "PREFIX stats: <http://statistics.data.gov.uk/id/statistical-geography/>
      PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
      PREFIX simd: <http://data.opendatascotland.org/def/simd/>
      PREFIX cube: <http://purl.org/linked-data/cube#>
      PREFIX stats_dim: <http://data.opendatascotland.org/def/statistical-dimensions/>
      PREFIX year: <http://reference.data.gov.uk/id/year/>

      SELECT DISTINCT
      ?dz_label
      ?overall_rank
      ?income_deprivation_rank
      ?employment_deprivation_rank
      ?health_deprivation_rank
      ?education_deprivation_rank
      ?access_deprivation_rank
      ?housing_deprivation_rank
      ?crime_deprivation_rank

      WHERE {

      GRAPH <http://data.opendatascotland.org/graph/simd/rank> {
      ?overall_rank_observation stats_dim:refArea ?dz .
      ?overall_rank_observation stats_dim:refPeriod year:2012 .
      ?overall_rank_observation simd:rank ?overall_rank .
      }

      GRAPH <http://data.opendatascotland.org/graph/simd/income-rank> {
      ?income_rank_observation stats_dim:refArea ?dz .
      ?income_rank_observation stats_dim:refPeriod year:2012 .
      ?income_rank_observation simd:incomeRank ?income_deprivation_rank .
      }

      GRAPH <http://data.opendatascotland.org/graph/simd/employment-rank> {
      ?employment_rank_observation stats_dim:refArea ?dz .
      ?employment_rank_observation stats_dim:refPeriod year:2012 .
      ?employment_rank_observation simd:employmentRank ?employment_deprivation_rank .
      }

      GRAPH <http://data.opendatascotland.org/graph/simd/health-rank> {
      ?health_rank_observation stats_dim:refArea ?dz .
      ?health_rank_observation stats_dim:refPeriod year:2012 .
      ?health_rank_observation simd:healthRank ?health_deprivation_rank .
      }

      GRAPH <http://data.opendatascotland.org/graph/simd/education-rank> {
      ?education_rank_observation stats_dim:refArea ?dz .
      ?education_rank_observation stats_dim:refPeriod year:2012 .
      ?education_rank_observation simd:educationRank ?education_deprivation_rank .
      }

      GRAPH <http://data.opendatascotland.org/graph/simd/geographic-access-rank> {
      ?access_rank_observation stats_dim:refArea ?dz .
      ?access_rank_observation stats_dim:refPeriod year:2012 .
      ?access_rank_observation simd:geographicAccessRank ?access_deprivation_rank .
      }

      GRAPH <http://data.opendatascotland.org/graph/simd/housing-rank> {
      ?housing_rank_observation stats_dim:refArea ?dz .
      ?housing_rank_observation stats_dim:refPeriod year:2012 .
      ?housing_rank_observation simd:housingRank ?housing_deprivation_rank .
      }

      GRAPH <http://data.opendatascotland.org/graph/simd/crime-rank> {
      ?crime_rank_observation stats_dim:refArea ?dz .
      ?crime_rank_observation stats_dim:refPeriod year:2012 .
      ?crime_rank_observation simd:crimeRank ?crime_deprivation_rank .
      }

    {
      SELECT ?dz ?dz_label WHERE
    {
      ?dz a <http://data.opendatascotland.org/def/geography/DataZone> .
      ?dz rdfs:label ?dz_label .
    }
    }
  }"

  ## Make the data table for SIMD
  dta.simd <- SPARQL(url = endpoint, query = query.simd, format = "csv")$results
  ### Clean the data frame
  dta.simd$dz_label <- gsub("Data Zone","", dta.simd$dz_label)

  # Readings shapefiles (pass only file that ends with shp)
  shps.dzs2001 <- readShapeSpatial("data/SG_DataZone_Bdry_2001.shp")

  ## Read shapefiles
  shps.dzs2001 <- fortify(shps.dzs2001, region= "DZ_CODE")

  # Switch on the gclibpermit
  gpclibPermit()

  # Make the plot
  output$distPlot <- renderPlot({

    ### Get the column index fumber
    col.ind <- match(input$firstvar, colnames(dta.simd))


    #### Calculate the breaks 
    ##### Take tha quantilies value from the UI
    ##### Obtain the breaks
    dta.simd$column <- as.numeric(as.character(dta.simd[,col.ind]))
    dta.simd$overall.quantiles <- ceiling(sapply(dta.simd$column,function(x) 
      sum(x-dta.simd$column>=0))/(length(dta.simd$column)/input$quannum))

    #### Graph the chart
    ggplot() + 
      geom_map(data = dta.simd, aes(map_id = dz_label, 
                                    fill = as.factor(dta.simd$overall.quantiles)), 
               map = shps.dzs2001) +
      geom_path(data = shps.dzs2001, aes(x=long, y=lat, group=group), 
                colour="black", size=0.25)

    ### Re-calculate summary stats
    summary.data <- head(dta.simd)

  })
  ### Do the summary tables
  output$summary.table <- renderDataTable({summary.data})
})
#Libs
要求(有光泽);require(SPARQL);要求(GG2);需要(rgeos);需要(地图工具);
需要(RCurl)
#服务器功能
shinyServer(功能(输入、输出){
#数据来源
##源SPARQL数据
###定义端点URL。
端点

我曾经遇到过一个类似的问题,并且能够以这种方式解决它。

你能举一个简单的例子吗?当然你可以用一个小数据样本来复制这个问题,而不是在一个闪亮的应用程序中。不过,有一件事可以尝试,那就是从
aes()
内部删除数据框的名称。使你的
填充=as.factor(总体.quantiles)
,或在绘图之前将列转换为因子。Gregor,感谢您的关注。我的想法是,最初的两段将接近一个最小的示例,但我决定发布整个内容,只是为了提供更多信息。前两段是一个不错的想法,但实际上它们不是一个充分的示例e因为我不能运行它们。如果不运行,我就不能测试任何东西,所以我能做的最好的事情就是仔细检查代码(没有明显的错误),然后让您尝试一些东西。我不认为这是重复的。正如注释中所述,我尝试了
As.factor()
但没有用。如果这是唯一的问题,那么问题现在就解决了。如果其他人遇到这个令人沮丧的错误消息,在我的情况下,我的数据框中的美国各州名称的样式与50个州的名称不同(小写并写出)。一旦我匹配了它们(使用tolower(state.name[match(stateOffices$state,state.abb)]),所有这些都工作正常。
require(shiny)

# Define list of options
choices.list <- list("Overall Rank" = "overall_rank", 
                     "Income" = "income_deprivation_rank",
                     "Employment" = "employment_deprivation_rank",
                     "Health" = "health_deprivation_rank",
                     "Education" = "education_deprivation_rank",
                     "Access to Services" = "access_deprivation_rank",
                     "Housing" = "housing_deprivation_rank",
                     "Crime" = "crime_deprivation_rank"
                     )

# Define UI for application that draws a histogram
shinyUI(fluidPage(

  # Application title
  titlePanel("Scottish Index of Multiple Deprivation 2012"),

  # Sidebar with a slider input for the number of bins
  sidebarLayout(
    sidebarPanel(
      selectInput(inputId = "firstvar", 
                  label = "Index domain to map",
                  choices = choices.list, 
                  selected = "income_deprivation_rank"),
      sliderInput("quannum", label = "Number of quantiles", 
                  min = 0, 
                  max = 100, 
                  value = 10)),

    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("distPlot", height = "700px"),
      dataTableOutput("summary.data"),
      h5("Notes"),
      p("The data is sourced live from the Open Data for Scotland Project. The shapefiles are sourced from the Scottish Neighbourhood Statistics.")
    )
  )
))
fill=factor(overall.quantiles)