忽略 AddCircles 函数中的 FillOpacity 参数

问题描述 投票:0回答:1

我正在开发一个 R Shiny 项目,以可视化世界上所有的机场。 下面包含的代码是我的项目的示例。 从功能上讲,该应用程序按预期工作 - 当您选择不同的国家/地区时,数据表和地图会更新。 但是,我对地图上的点有一些疑问。 由于点的半径取决于从机场出发的航线数量,因此在可能有多个大型机场的人口稠密地区(例如:芝加哥),一些较大的点与较小的点重叠。 我在 AddCircles 函数中使用了 FillOpacity 参数以使其更加透明,以便您可以看到潜在的遮罩点。 然而,当我在下面的代码中使用它时,似乎这个参数被忽略了:

#Dataset 1: Routes
routes=read.csv(url("https://raw.githubusercontent.com/jpatokal/openflights/master/data/routes.dat"))
dim(routes)
#67662  9
str(routes)

#Dataset #2: Airports
airports=read.csv(url("https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports-extended.dat"))
dim(airports)
#12667 14

#Rename columns
colnames(routes)=c("Airline","AirlineID","IATA","SourceAP_ID","DestinationAirport","DestAP_ID","Codeshare","Stops","Equipment")
colnames(airports)=c("AirportID","Name","City","Country","IATA","ICAO","Latitude","Longitude","Altitude","Timezone","DST","TzDatabaseTz","Type","Source")

#Join datasets on Source Airport
#-------------------------------#
#IATA in airports
#SourceAirport in routes (IATA)

fullair=merge(x=routes,y=airports,by="IATA",all.x=TRUE)

fullair2=subset(fullair,fullair$Type=="airport")

library(dplyr)

#Make a new unique ID by combining IATA and Destination Airport
fullair2$UniqueID=paste0(fullair2$IATA,"_",fullair2$DestinationAirport)

#Create column that assigns number of unique routes to single airport
fullair3=fullair2 %>%
  group_by(IATA) %>%
  mutate(Count=n_distinct(UniqueID)) %>%
  ungroup()
fullair3=as.data.frame(fullair3)



#Get rid of duplicates
fullair3=fullair3[!duplicated(fullair3[c("UniqueID")]),]


library(rowr)
library(sqldf)
library(RSQLite)


#-----------Number of Routes from Destination------------#
SpitOutNum=sqldf("select IATA,count(*)
                 from fullair3
                 group by IATA")
SpitOutNum=as.data.frame(SpitOutNum)
colnames(SpitOutNum)=c("IATA","DestinationCount")
fullair3=merge(x=fullair3,y=SpitOutNum,by="IATA",all.x=TRUE)



#Create the full name
fullair3$NamePart1=paste("(",fullair3$IATA,")",sep ="")
fullair3$FullName=paste(fullair3$Name, fullair3$NamePart1)
fullair3$NamePart1=NULL

fullair3$Name2Part1=paste("(",fullair3$DestinationAirport,")",sep ="")
fullair3$DestFullName=paste(fullair3$DestAirportName, fullair3$Name2Part1)

fullair3$Name2Part1=paste("(",fullair3$DestinationAirport,")",sep ="")
fullair3$DestFullName=paste(fullair3$DestAirportName, fullair3$Name2Part1)


table2=sqldf("select Country, FullName as 'Airport Name', City, count(*) as 'Number of Routes'
             from fullair3
             group by Country, FullName, City
             order by count(*) desc")


#Sort by FullName
fullair3= fullair3[order(fullair3$FullName),]

#-----------------------------------------------------
library(shinydashboard)
library(shiny)
library(leaflet)
library(leaflet.extras)
library(rgdal)
library(sp)
library(raster)
library(DT)
library(markdown)
library(geosphere)
library(htmltools)


airportchoices=unique(fullair3$FullName)
countrychoices=unique(fullair3$Country)

countrychoices=as.character(countrychoices)
countrychoices=sort(countrychoices)


# Define UI for application
ui <- fluidPage(
  dashboardPage(
    dashboardHeader(title="Airport Analysis"),
    dashboardSidebar(
      sidebarMenu(
        menuItem("Country Maps",
                 tabName = "CountryMaps",
                 icon=icon("flag")
        ))
    ),
    dashboardBody(
      tabItems(
        tabItem(
          tabName = "CountryMaps",
          tags$style(type="text/css","#country_airports {height:calc(100vh - 80px) !important;}"),
          fluidRow(column(4),
                   column(8, selectInput(inputId = "countryselect",label="Select a country:",choices=countrychoices)
                   )),
          DTOutput("countrydata"),
          leafletOutput("country_airports")
          
        )
      )
    )
  )
)
# Define server logic 
server <- function(input, output) {
  
  #----------COUNTRY FILTERING---------#  
  
  CountryData=reactive({
    filteredData=subset(fullair3,Country == input$countryselect)
    return(filteredData)
  })
  
  CountryDataTable=reactive({
    filteredDataTable=subset(table2,Country==input$countryselect)
    filteredDataTable$Country=NULL
    return(filteredDataTable)
  })
  
  
  #-------------------COUNTRY PLOTS-------------------#
  output$countrydata=renderDT({
    
    data_table = CountryDataTable()
    
    
    datatable(data_table,options=list(pageLength=5,
                                      lengthMenu=c(5,10,15,20)
    ),rownames = FALSE)
  })
  
  output$country_airports=renderLeaflet({
    
    data=CountryData()
    
    pal=colorNumeric("Yellow",data$DestinationCount)
    
    
    leaflet(data=data) %>% 
      addTiles(group="CartoDB.Positron")  %>%
      
      addProviderTiles(providers$CartoDB.Positron,
                       options = tileOptions(minZoom =0, maxZoom = 13),
                       group = "CartoDB.Positron") %>%
      
      
      addCircles(radius = ~data$DestinationCount*1000, 
                 weight = 1, 
                 color = "black", 
                 fillColor = ~pal(data$DestinationCount),
                 fillOpacity = 0.4,
                 popup = paste0("Airport Name: ", data$Name, "<br>",
                                "City: ", data$City, "<br>",
                                "Destination Count: ",data$DestinationCount,"<br>"
                 ),
                 label = ~as.character(data$IATA),
                 group = "Points") 
    
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

有人对如何解决这个问题有任何建议吗?

r shiny r-leaflet
1个回答
4
投票

我认为

fillOpacity = 0.4
太高,无法获得芝加哥附近点的透明度。我在下图中设置了透明度
fillOpacity = 0.01
。如果 alpha 值较低,则需要更多数量的点才能使点完全不透明。我认为,例如,机场 ORD 的行数为 0.4 且超过 200 行,就达到了这个阈值。

enter image description here

您可以尝试解决这个问题,每个机场只绘制一个点(即数据集中只有一行)。

例如使用

fillOpacity = 0.4
并在
leaflet
函数之前添加此代码

data <- data %>% 
  dplyr::distinct(IATA, Latitude, Longitude, DestinationCount, Name, City)

我拿到了下图中的地图

enter image description here

在评论中回答您的进一步问题,似乎一种可能的方法是使用 z-index 参数,但我发现 SVG(如 Circles)不支持 z-index 并且顺序基于元素已添加到地图中。所以我尝试按以下方式更改上面的代码:

data <- data %>% 
  dplyr::distinct(IATA, Latitude, Longitude, DestinationCount, Name, City) %>% 
  dplyr::arrange(desc(DestinationCount))

得到了这个结果

enter image description here

您也可以尝试使用

addCircleMarkers
代替
addCircles
,仅更改
radius = ~data$DestinationCount*0.3
。如果放大得足够大,您会看到 ORD 和 MDW 没有重叠。

© www.soinside.com 2019 - 2024. All rights reserved.