简体   繁体   English

闪亮的传单图例标记与地图标记相同

[英]Shiny Leaflet legend markers same as map markers

I'm creating a legend to differentiate two sets of circle markers, which are distinguished by fillOpacity = 1 (filled in) or fillColor = "white" (empty), corresponding to an df$aircraft column with two levels. 我正在创建一个图例,以区分两组圆形标记,它们分别由fillOpacity = 1(填充)或fillColor =“ white”(空)来区分,分别对应于具有两个级别的df $ aircraft列。 I want the legend to have a filled and empty marker to match up with each. 我希望图例有一个填充的和空的标记以与每个标记匹配。

This seems like a simple thing, but the other question asking this did not receive an answer. 这似乎很简单,但是另一个询问此问题的问题没有得到答案。 I've tried modifying the function/CSS styling found here , but haven't been successful in changing the opacity of each legend marker separately, or in keeping the stroke color (only the inside should be empty). 我尝试过修改此处找到的功能/ CSS样式,但是在分别更改每个图例标记的不透明度或保持笔触颜色(仅内部应为空)方面没有成功。

Here's code to generate a simple map, with the markers looking how I'd want the legend to look (legend does not show up): 这是生成简单地图的代码,其中的标记看起来像我希望图例显示的样子(图例不显示):

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)

Thanks for any help. 谢谢你的帮助。

I've created something for you but it's slightly a messed up css. 我为您创建了一些东西,但是它有点混乱了。

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)

Output Screenshot: 输出截图:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM