【问题标题】:ggvis Map & Tooltipsggvis 地图和工具提示
【发布时间】:2014-11-14 17:07:22
【问题描述】:

我正在尝试在美国地图的工具提示上分层,但无论我将鼠标悬停在哪里……它都会显示相同的数据。另外,数据有误。我在想它是通过因子值而不是字符值。我尝试从电影浏览器示例中获取提示 - http://shiny.rstudio.com/gallery/movie-explorer.html - 但是,它没有像我希望的那样工作。我应该调查任何提示或线索?

更新:我确定您只能将被调用的参数传递给ggvis 函数。所以,如果我的工具提示功能包括regionlonglat,它们都会出现在工具提示中。由于PopulationIncome 没有出现在函数中的任何位置,因此它没有传递它们。我仍然不知道如何进行,但任何想法都会很棒! :)

library(ggplot2)
library(shiny)
library(ggvis)
library(dplyr)

shinyApp(

  ui = fluidPage(
    #numericInput("n", "n", 1),
    ggvisOutput("map")
  ),

  server = function(input, output) {

    statesData <- reactive({

      states <- data.frame(state.x77)
      states$region <- row.names(state.x77) %>% tolower
      row.names(states) <- NULL

      all_states <- map_data("state") %>%
        mutate(region = tolower(region)) %>%
        left_join(states)

      all_states_unique <- all_states %>%
        select(region, Population, Income, Illiteracy, Life.Exp, Murder, HS.Grad, Frost, Area) %>%
        unique

      states_tooltip <- function(x) {
        if (is.null(x)) return(NULL)
        if (is.null(x$region)) return(NULL)

        # Pick out the movie with this ID
        allStates <- isolate(all_states_unique)
        state <- allStates[allStates$region == x$region, ]

        paste0("<b>", state$region, "</b><br>",
               state$Population, "<br>",
               state$Income

        )
      }

      all_states %>%
        arrange(group, order) %>%
        ggvis(x = ~long, y = ~lat) %>%
        layer_paths(fill = ~region, stroke := .2) %>%
        add_tooltip(states_tooltip, "hover")

    })

    statesData %>% bind_shiny('map')    

  }

)

【问题讨论】:

标签: r tooltip shiny dplyr ggvis


【解决方案1】:

为要从中提取工具提示数据的数据框添加索引:

 state$id <- 1:nrow(state)

ggvis 采用“key”参数来促进这种工具提示:

 ggvis(x = ~long, y = ~lat, key := ~id) %>%

我试着找出那个电影的例子,但没有发现它很有帮助。这总是对我有用:

 add_tooltip(function(x) {
             row <- state[state$id == x$key,]
             paste0("<b>", row[,"region"], "</b><br>",
                    row[,"Population"], "<br>",
                    row[,"Income"]
                    )})

至于工具提示总是出现相同的问题,我不确定,但认为这是由于您在 ggvis 命令中的层顺序。有一个类似的问题,我在散点图的顶部有一些多边形。当我想要的是显示工具提示的各个点时,它一直试图为多边形(覆盖整个图表)绘制工具提示。通过在 ggvis 命令中颠倒它们的顺序(即 layer_points() %>% layer_shapes()),我让它工作了。

【讨论】:

  • 嗨砖,感谢您的回复!这并不能完全解决我的需要,但它确实提供了一些指导。
  • 嘿,Brick,终于通过更多的修补让它工作了。谢谢!
【解决方案2】:

我意识到这已经很晚了,但供将来参考和其他偶然发现此页面的人参考。如果您的数据框已使用 fortify 进行转换并具有 group 变量,那么这可能相当于州级别。然后可以使用组来过滤工具提示,就像在 ggvis 命令中一样。这让我可以访问我想要的其他变量。

在我的问题中,我无法使用关键解决方案,因为我正在创建情节以应对多年。因此,要更改您在 states_tooltip 上面的内容将变为:

       states_tooltip <- function(x){
 row <- allstates[allstates$group==x$group,]  %>%
     select(region, Population, Income)  %>% unique
 paste0("<b>", row[,"region"], "</b><br>",
                row[,"Population"], "<br>",
                row[,"Income"]
                )})   

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多