我创建了一个图例来区分两组圆圈标记,这两组圆圈标记的区别在于fillOpacity = 1(填充)或fillColor =" white" (空),对应于具有两个级别的df $飞机列。我希望图例中有一个填充的空标记与每个标记相匹配。
这看起来很简单,但是the other question问这个没有得到答案。我已尝试修改功能/ CSS样式found here,但未能成功分别更改每个图例标记的不透明度,或保持笔触颜色(仅内部应为空)
以下代码生成一个简单的地图,标记显示我希望图例看起来如何(图例未显示):
library(shiny)
library(leaflet)
# create data
df<-data.frame(x=runif(10,20,21), y=runif(10,0,1))
df$aircraft[1:5] <- "C130"
df$aircraft[5:10] <- "B200"
# create map
map = leaflet() %>% addTiles()
# set up shiny app
ui <- leafletOutput("myMap")
server <- function(input, output, session){
df_c <- filter(df,df$aircraft == "C130")
df_b <- filter(df,df$aircraft == "B200")
output$myMap = renderLeaflet({map %>%
addCircleMarkers(df_c$x,df_c$y, radius=10, opacity = 1, fillColor = "white") %>%
addCircleMarkers(df_b$x,df_b$y, radius=10, opacity = 1, fillOpacity = 1)
%>%
addLegend(colors = c("blue", "blue"), labels = c("C130", "B200"))
})
}
shinyApp(ui, server)
感谢您的帮助。
答案 0 :(得分:1)
我已经为你创造了一些东西,但它有点混乱了。
library(shiny)
library(leaflet)
library(magrittr)
# create data
df<-data.frame(x=runif(10,20,21), y=runif(10,0,1))
df$aircraft[1:5] <- "C130"
df$aircraft[5:10] <- "B200"
# create map
map = leaflet() %>% addTiles()
# set up shiny app
ui <- bootstrapPage( tags$style(type = "text/css", "html, body {width:100%;height:100%}",
"
.leaflet-top .leaflet-control {
margin: 0px;
}
.leaflet-right {
margin-right: 40px;
}
.full{
background-color: blue;
border-radius: 50%;
width: 20px;
height: 20px;
float: left;
}
.circle {
background-color: #FFF;
border: 3px solid blue;
border-radius: 50%;
height: 20px;
width: 20px;
}
.leaflet-control i{
margin-right: 25px;
}
"),
leafletOutput("myMap"))
server <- function(input, output, session){
df_c <- filter(df,df$aircraft == "C130")
df_b <- filter(df,df$aircraft == "B200")
output$myMap = renderLeaflet({map %>%
addCircleMarkers(df_c$x,df_c$y, radius=10, opacity = 1, fillColor = "white") %>%
addCircleMarkers(df_b$x,df_b$y, radius=10, opacity = 1, fillOpacity = 1) %>%
addLegend(colors = c("blue"), labels = c("B200"), className='full') %>%
addLegend(colors = c("white"), labels = c("C130"), className = 'circle')
})
}
shinyApp(ui, server)
输出截图: