Help with fillOpacity - Leaflet Maps

Hello,

I am working on a Shiny project to visualize the accessibility of all airports in the world. The code I've included below is a just a snapshot of the entire analysis, but I've included this chunk, because I'm having issues with an argument in my AddCircles function: fillOpacity. This argument seems to be ignored and as a result when you look at points on the maps, larger circles will overlap smaller ones and you won't be able to see them. For example, I have the circle for Chicago O'Hare dwarfing everything else in the area and there are some other notable airports (eg: Midway) that I want the user to be able to see.

Here is my code:

#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

#Give Better Names to 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)



fullair2$Source=NULL
fullair2$Type=NULL
fullair2$Codeshare=NULL
fullair2$Equipment=NULL
fullair2$Stops=NULL
fullair2$Airline=NULL
fullair2$AirlineID=NULL
fullair2$SourceAP_ID=NULL
fullair2$DestAP_ID=NULL
fullair2$AirportID=NULL
fullair2$Altitude=NULL
fullair2$Timezone=NULL
fullair2$DST=NULL
fullair2$ICAO=NULL
head(fullair2)

#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)

#-------------------Grab Region and/or Country-------------------#

library(stringi)
fullair3$Region=stri_extract(fullair3$TzDatabaseTz, regex='[^/]*')


#----------Find out where these "\\N" need to go----------#

#Regional Access Measure
test=subset(fullair3,fullair3$Region=="\\N")
dim(test)
#0    13
sqldf("select Country, count(*) as Count
      from test
      group by Country
      order by Count desc")

#Fix 1: Europe Region
fullair3$Region=ifelse(fullair3$Region=="\\N" & (fullair3$Country=="Turkey"|
                                                   fullair3$Country=="Russia"|
                                                   fullair3$Country=="France"),"Europe",fullair3$Region)
#Fix 2: Asia
fullair3$Region=ifelse(fullair3$Region=="\\N" & (fullair3$Country=="Qatar"|
                                                   fullair3$Country=="Pakistan"|
                                                   fullair3$Country=="India"|
                                                   fullair3$Country=="China"|
                                                   fullair3$Country=="Saudi Arabia"|
                                                   fullair3$Country=="Japan"|
                                                   fullair3$Country=="Philippines"|
                                                   fullair3$Country=="Indonesia"),"Asia",fullair3$Region)

#Fix 3: America Region
fullair3$Region=ifelse(fullair3$Region=="\\N" & (fullair3$Country=="Canada"|
                                                   fullair3$Country=="Mexico"|
                                                   fullair3$Country=="Chile"|
                                                   fullair3$Country=="Ecuador"|
                                                   fullair3$Country=="Guadeloupe"|
                                                   fullair3$Country=="Greenland"),"America",fullair3$Region)

#Fix 4: Pacific
fullair3$Region=ifelse(fullair3$Region=="\\N" & (fullair3$Country=="French Polynesia"|
                                                   fullair3$Country=="Kiribati"|
                                                   fullair3$Country=="Papua New Guinea"),"Pacific",fullair3$Region)

#Fix 5: Africa
fullair3$Region=ifelse(fullair3$Region=="\\N" & (fullair3$Country=="Algeria"|
                                                   fullair3$Country=="Uganda"|
                                                   fullair3$Country=="Nigeria"|
                                                   fullair3$Country=="Tanzania"|
                                                   fullair3$Country=="Zambia"|
                                                   fullair3$Country=="Congo (Kinshasa)"|
                                                   fullair3$Country=="Congo (Brazzaville)"),"Africa",fullair3$Region)

#Fix 6: Indian
fullair3$Region=ifelse(fullair3$Region=="\\N" & (fullair3$Country=="Maldives"),"Indian",fullair3$Region)
#Fix 7: Australia
fullair3$Region=ifelse(fullair3$Region=="\\N" & (fullair3$Country=="Australia"),"Australia",fullair3$Region)


#-----------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)
dim(fullair3)
#37445    12


#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)

#Make destination columns
SpitOutNum2=sqldf("select IATA, City, Country, Region, Name, DestinationCount, Longitude, Latitude
                  from fullair3
                  group by IATA,City, Country, Region, Name")
colnames(SpitOutNum2)=c("DestinationAirport","DestCity","DestCountry","DestRegion","DestAirportName","DestCount","DestLong","DestLat")
fullair3=merge(x=fullair3,y=SpitOutNum2,by="DestinationAirport",all.x=TRUE)

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


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


#TABLE2 - Country
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")

#Fix some more issues
fullair3$Region=ifelse(fullair3$IATA=="PWE","Asia",fullair3$Region)
fullair3$Region=ifelse(fullair3$IATA=="SUK","Asia",fullair3$Region)
fullair3$Region=ifelse(fullair3$IATA=="DEE","Asia",fullair3$Region)
fullair3$Region=ifelse(fullair3$IATA=="EKS","Asia",fullair3$Region)
fullair3$Region=ifelse(fullair3$IATA=="NGK","Asia",fullair3$Region)

#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)
regionchoices=unique(fullair3$Region)

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

regionchoices=as.character(regionchoices)
regionchoices=sort(regionchoices)


# Define UI for application
ui <- fluidPage(
  dashboardPage(
    dashboardHeader(title="Airport Analysis"),
    dashboardSidebar(
      sidebarMenu(
        menuItem("Region Maps",
                 tabName = "RegionMaps",
                 icon=icon("globe")
        ),
        menuItem("Country Maps",
                 tabName = "CountryMaps",
                 icon=icon("flag")
        ))
    ),
    dashboardBody(
      tabItems(
        tabItem(
          tabName = "RegionMaps",
          tags$style(type="text/css","#region_airports {height:calc(100vh - 80px) !important;}"),
          fluidRow(column(4),
                   column(8, selectInput(inputId = "regionselect",label="Select a region:",choices=regionchoices)
                   )),
          DTOutput("regiondata"),
          leafletOutput("region_airports")
        ),
        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) {
  
  #----------REGION FILTERING---------#  
  RegionData=reactive({
    filteredData=subset(fullair3,Region == input$regionselect)
    return(filteredData)
  })
  
  RegionDataTable=reactive({
    filteredDataTable=subset(table1,Region==input$regionselect)
    filteredDataTable$Region=NULL
    return(filteredDataTable)
  })
  #----------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)
  })
  
  
  
  #-------------------REGION PLOTS-------------------#
  
  output$regiondata=renderDT({
    
    data_table = RegionDataTable()
    
    datatable(data_table,options=list(pageLength=5,
                                      lengthMenu=c(5,10,15,20)
    ),rownames = FALSE)
  })
  
  output$region_airports=renderLeaflet({
    
    data=RegionData()
    
    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") %>%
      
      addProviderTiles(providers$CartoDB.DarkMatter,
                       options = tileOptions(minZoom =0, maxZoom = 13),
                       group = "CartoDB.DarkMatter") %>%
      
      addProviderTiles(providers$Esri.WorldImagery,
                       options = tileOptions(minZoom =0, maxZoom = 13),
                       group = "Esri.WorldImagery") %>%
      
      
      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") %>%
      
      addLayersControl(
        baseGroups = c("CartoDB.Positron","CartoDB.DarkMatter","Esri.WorldImagery"),
        options = layersControlOptions(collapsed = TRUE))
  })
  
  
  
  #-------------------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") %>%
      
      addProviderTiles(providers$CartoDB.DarkMatter,
                       options = tileOptions(minZoom =0, maxZoom = 13),
                       group = "CartoDB.DarkMatter") %>%
      
      addProviderTiles(providers$Esri.WorldImagery,
                       options = tileOptions(minZoom =0, maxZoom = 13),
                       group = "Esri.WorldImagery") %>%
      
      
      
      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") %>%
      
      addLayersControl(
        baseGroups = c("CartoDB.Positron","CartoDB.DarkMatter","Esri.WorldImagery"),
        options = layersControlOptions(collapsed = TRUE))
    
  })
  
  
}

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

I've looked all over StackOverflow and have not been able to find a solution. Does anyone know why this argument is being ignored? I would appreciate any kind of help.

Thank you!

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.