R语言 使用ggplot2绘图时出现错误:“由lat * pi中的错误引起”

rryofs0p  于 2023-03-10  发布在  其他
关注(0)|答案(1)|浏览(412)

当用户在我的RShiny应用程序中输入特定数据时,我尝试创建ggplot 2区域图。我将空间数据框与数据合并,如下所示,我认为工作正常:

# inputting data in RShiny app:
output$contents <- renderTable({
    file <- input$uploaded_data
    ext <- tools::file_ext(file$datapath)
    
    req(file)
    
    counties_and_trash <- read_csv(file$datapath)
    
    mean_each_type <- counties_and_trash %>%
      group_by(COUNTY) %>%
      summarise(mean_plastics = mean(PLASTICS, na.rm = TRUE),
                mean_papers = mean(PAPERS, na.rm = TRUE),
                mean_metals = mean(METALS, na.rm = TRUE))
})

# merging spdf and data:
spdf <- geojson_read('County_Boundaries_of_NJ.geojson',  what = "sp")
merged <- merge(spdf, mean_each_type, by.x = 'COUNTY', by.y = 'mean_plastics')

请注意,到目前为止,我只尝试在区域分布图中绘制“mean_plastics”。其他的(“mean_papers”和“mean_metals”)目前无关紧要。我尝试在RShiny应用程序中输出ggplot 2区域分布图,如下所示:

output$plot <- renderPlot({
    ggplot(merged, aes(x = x, y = y)) + 
    geom_polygon(aes(fill = mean_plastics), data = mean_each_type, x = 'COUNTY', y = 'mean_plastics') +
    theme_void() +
    coord_map()
  })

当我尝试输出它时,我得到了以下错误:

Warning: Error in geom_polygon: Problem while converting geom to grob.
ℹ Error occurred in the 1st layer.
Caused by error in `lat * pi`:
! non-numeric argument to binary operator

我确信“output$plot...”代码块有问题,但我不知道如何修复它。
如果需要更多的信息,我可以提供。如果我解释得不够好,我道歉,这是我第一次做一个更复杂的R项目。谢谢你的帮助!

编辑:我在下面添加了可重复性最低的示例。

要使代码工作,您必须下载的唯一文件位于以下链接(下载的文件应具有名称“County_Boundaries_of_NJ.geojson”):
download link for NJ county boundaries geojson
下面是最小的可重复代码:

library(shiny)
library(tidyverse)
library(ggplot2)
library(geojsonio)
library(broom)
library(sp)
library(sf)

ui <- fluidPage(
  fluidRow(
    h1(strong('NJ Beachsweep Mapping with R'), align = 'center'),
    column(6,
           # this empty column is just to put the other column in the right place
    ),
    column(6,
           tableOutput("contents")
    ),
    mainPanel(
      plotOutput("plot")
    )
  )
)

server <- function(input, output, session) {
  counties_and_trash <- structure(list(COUNTY = c("Atlantic", "Atlantic", "Bergen", "Burlington", 
                                                  "Burlington", "Essex", "Middlesex", "Cape May", "Cape May", "Cape May", 
                                                  "Monmouth", "Monmouth", "Monmouth", "Monmouth", "Monmouth", "Monmouth", 
                                                  "Ocean"), PLASTICS = c(340, 300, 325, 467, 545, 354, 433, 325, 
                                                                         324, 653, 768, 457, 486, 944, 356, 457, 568), PAPERS = c(260, 
                                                                                                                                  210, 453, 223, 235, 356, 324, 274, 540, 346, 475, 462, 342, 354, 
                                                                                                                                  435, 346, 234), METALS = c(45, 35, 123, 124, 224, 124, 134, 342, 
                                                                                                                                                             230, 243, 324, 125, 323, 122, 334, 421, 401)), row.names = c(NA, 
                                                                                                                                                                                                                          -17L), spec = structure(list(cols = list(COUNTY = structure(list(), class = c("collector_character", 
                                                                                                                                                                                                                                                                                                        "collector")), PLASTICS = structure(list(), class = c("collector_double", 
                                                                                                                                                                                                                                                                                                                                                              "collector")), PAPERS = structure(list(), class = c("collector_double", 
                                                                                                                                                                                                                                                                                                                                                                                                                  "collector")), METALS = structure(list(), class = c("collector_double", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                      "collector"))), default = structure(list(), class = c("collector_guess", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            "collector")), delim = ","), class = "col_spec"), class = c("spec_tbl_df", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "tbl_df", "tbl", "data.frame"))
  mean_each_type <- counties_and_trash %>%
    group_by(COUNTY) %>%
    summarise(mean_plastics = mean(PLASTICS, na.rm = TRUE),
              mean_papers = mean(PAPERS, na.rm = TRUE),
              mean_metals = mean(METALS, na.rm = TRUE))
  
  output$contents <- renderTable({
    counties_and_trash
  })
  
  spdf <- geojson_read('County_Boundaries_of_NJ.geojson',  what = "sp") 
  merged <- merge(spdf, mean_each_type, by.x = 'COUNTY', by.y = 'mean_plastics')
  
  output$plot <- renderPlot({ # this block of code is where the issue most likely is
    ggplot(merged, aes(x = x, y = y)) + 
      geom_polygon(aes(fill = mean_plastics)) +
      theme_void() +
      coord_map()
  })
}
shinyApp(ui = ui, server = server)

太感谢你了!

db2dz4w8

db2dz4w81#

您的代码存在多个问题,但没有一个与shiny相关。首先,您读取的shapefile是sp对象。对于这种类型的对象,简单地合并数据或通过geom_polygon绘图都不起作用。而是转换为sf对象,该对象的行为与标准 Dataframe 相当相同,并且可以使用geom_sf轻松绘图。其次,在mean_plastics上进行合并是没有意义的,因为在空间数据集中没有这样的列。第三,只有当两个数据集中有相同的键而不仅仅是相同的列名时,合并才起作用,也就是说,您必须在mean_each_type数据集中将COUNTY列转换为大写形式。

library(shiny)
library(tidyverse)
library(geojsonio)
library(sp)
library(sf)

ui <- fluidPage(
  fluidRow(
    h1(strong("NJ Beachsweep Mapping with R"), align = "center"),
    column(
      6,
      plotOutput("plot")
    ),
    column(
      6,
      tableOutput("contents")
    )
  )
)

server <- function(input, output, session) {
  mean_each_type <- counties_and_trash %>%
    group_by(COUNTY) %>%
    summarise(
      mean_plastics = mean(PLASTICS, na.rm = TRUE),
      mean_papers = mean(PAPERS, na.rm = TRUE),
      mean_metals = mean(METALS, na.rm = TRUE)
    ) |>
    mutate(COUNTY = toupper(COUNTY))

  output$contents <- renderTable({
    counties_and_trash
  })

  spdf <- geojson_read("County_Boundaries_of_NJ.geojson", what = "sp")
  # Convert to sf
  spdf <- sf::st_as_sf(spdf)

  merged <- merge(spdf, mean_each_type, by = "COUNTY")

  output$plot <- renderPlot({ # this block of code is where the issue most likely is
    ggplot(merged) +
      # Use geom_sf
      geom_sf(aes(fill = mean_plastics)) +
      theme_void()
  })
}
shinyApp(ui = ui, server = server)

相关问题