简体   繁体   English

高图表 map 中的格式工具提示用于动态变量,一些使用逗号,另一些使用百分比

[英]Format tooltip in highchart map for dynamic variables, some with commas and others with percents

I've written a function to pull data into a highcharter cloropleth map.我已经写了一个 function 来将数据提取到一个高容量的 cloropleth map 中。 I'm having a very difficult time trying to figure out how to format the values going into the map based on whether they are counts (needing format with commas and no decimal places) or percents(needing to be multiplied by 100 and a % sign added as prefix)我很难弄清楚如何格式化输入 map 的值,具体取决于它们是计数(需要带逗号且没有小数位的格式)还是百分比(需要乘以 100 和 % 符号)添加为前缀)

The function takes in a dataframe x and selects the variable corresponding to the label assigned to the desired variable which is provided as y. function 接受 dataframe x 并选择与分配给所需变量的 label 对应的变量,该变量提供为 y。 In this reprex you can change out "Count" for "Percent" to change the map.在此表示中,您可以将“计数”更改为“百分比”以更改 map。 In my actual dashboard, the map changes dynamically based on which variable label is selected through a dropdown input selector using flexdashboard.在我的实际仪表板中,map 根据通过使用 flexdashboard 的下拉输入选择器选择的变量 label 动态变化。

I think that the solution should be to create an if statement within the hc_tooltip() to apply a comma format with no decimals if the value is > 1 or multiply the value by 100 and add a % prefix if value < 1.我认为解决方案应该是在 hc_tooltip() 中创建一个 if 语句,如果值 > 1,则应用不带小数的逗号格式,或者将值乘以 100,如果值 < 1,则添加 % 前缀。

I've tried reading highcharter documentation to attempt to figure this out but I really have zero formal programming training and don't know any javascript so the highcharter documentation is very hard for me to understand.我已经尝试阅读 highcharter 文档以试图弄清楚这一点,但我的正式编程培训确实为零,并且不知道任何 javascript 所以我很难理解 highcharter 文档。 It seems like one way it could be done is to use a JS function within hc_tooltip(), something like hc_tooltip(formatter = JS("function() {}). I can't figure out how to reference the values of {point.value} within a function like that. I've found a few stackoverflow posts that seem to be similar to my problem but I don't understand how to modify my code to make them work. One post I've been trying to understand is this one: Tooltip content within a highchart in Rshiny似乎可以做到的一种方法是在 hc_tooltip() 中使用 JS function,类似于 hc_tooltip(formatter = JS("function() {})。我不知道如何引用 {point .value} 在像这样的 function 中。我发现一些 stackoverflow 帖子似乎与我的问题相似,但我不明白如何修改我的代码以使其工作。我一直试图理解的一篇文章是这个吗: Rshiny 中高图表中的工具提示内容

I've tried using scales::percent() and scales::comma() on the appropriate values in the dataframe before they are passed to the map (like right after creating df) but it seems like hc_add_series_map() doesn't like it when the variables are passed in that way.在将它们传递给 map 之前,我尝试在 dataframe 中的适当值上使用 scales::percent() 和 scales::comma() (就像在创建 df 之后一样),但看起来 hc_add_series_map() 不喜欢当变量以这种方式传递时。

Please help me understand how to solve this or provide a solution that I can use to understand.请帮助我了解如何解决此问题或提供我可以用来理解的解决方案。 In my short time learning to code in R I think this might be the thing I've struggled with the longest and I'm feeling pretty dispirited right now that I can't figure this out.在我学习 R 编码的短时间内,我认为这可能是我一直在努力的最长时间的事情,我现在感到很沮丧,因为我无法弄清楚这一点。

library(tidyverse) 
library(utils) 
library(stats) 
library(labelled)
library(highcharter)
library(shiny) 
library(expss)

x<- structure(list(state = structure(c("Alabama", "Alaska", "Arizona", 
                                       "Arkansas", "California", "Colorado", "Connecticut", "Delaware", 
                                       "District Of Columbia", "Florida", "Georgia", "Hawaii", "Idaho", 
                                       "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", 
                                       "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", 
                                       "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", 
                                       "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", 
                                       "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", 
                                       "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", 
                                       "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", 
                                       "Wyoming"), label = "State"), count = structure(c(88972, 18217, 
                                                                                         99224, 43942, 451964, 79864, 74454, 20558, 24895, 327488, 140264, 
                                                                                         28621, 26095, 204099, 127416, 59616, 60374, 72304, 62918, 27603, 
                                                                                         86983, 147895, 149864, 116927, 48100, 119469, 18994, 32954, 48781, 
                                                                                         24601, 137461, 29405, 382436, 143585, 15428, 229085, 49448, 68338, 
                                                                                         229429, 25817, 72222, 18926, 107276, 350382, 36801, 20696, 109693, 
                                                                                         108594, 34110, 105354, 8107), label = "Count"), pctState = structure(c(0.0174, 
                                                                                                                                                                0.0036, 0.0194, 0.0086, 0.0883, 0.0156, 0.0146, 0.004, 0.0049, 
                                                                                                                                                                0.064, 0.0274, 0.0056, 0.0051, 0.0399, 0.0249, 0.0117, 0.0118, 
                                                                                                                                                                0.0141, 0.0123, 0.0054, 0.017, 0.0289, 0.0293, 0.0229, 0.0094, 
                                                                                                                                                                0.0234, 0.0037, 0.0064, 0.0095, 0.0048, 0.0269, 0.0057, 0.0748, 
                                                                                                                                                                0.0281, 0.003, 0.0448, 0.0097, 0.0134, 0.0448, 0.005, 0.0141, 
                                                                                                                                                                0.0037, 0.021, 0.0685, 0.0072, 0.004, 0.0214, 0.0212, 0.0067, 
                                                                                                                                                                0.0206, 0.0016), label = "Percent")), row.names = c(NA, -51L), class = "data.frame")

y<- "Count". #change y to "Percent" to get the other value in the dataframe for display

get_map <- function(x, y){
  data("usgeojson")
 
  z<- look_for(x, y, details= FALSE, labels = TRUE,
               ignore.case = FALSE)
  a<- z$variable
  
  df<- x %>%
    select(all_of(a))
  
  metric1<- y
  value1<-  colnames(df)
 
  highchart() %>%
    hc_add_series_map(usgeojson, x,
                      name = metric1,
                      value = value1,
                      joinBy = c("woename", "state"))%>%
    hc_mapNavigation(enabled = T) %>%
    hc_tooltip(pointFormat = "{point.value}"
               ) %>%
    hc_colorAxis(min= min(df),
                 max= max(df),
                 startOnTick= FALSE,
                 endOnTick= FALSE)
}

get_map(x, y)

I finally figured this out.我终于想通了。 It took a lot of time this last week but it works now.上周花了很多时间,但现在可以了。 The solution has two parts.解决方案有两个部分。 One part was to add formatting instructions for highcharter to my dashboard before entering the function to format values with a thousands separator一部分是在输入 function 之前将 highcharter 的格式化指令添加到我的仪表板,以使用千位分隔符格式化值

lang<-getOption("highcharter.lang")
lang$thousandsSep<-","
options(highcharter.lang = lang)

The second part was to re-write my function with some ifelse statements to separate the dataframe into the variables that needed to be formatted by percent and those that didn't.第二部分是用一些 ifelse 语句重写我的 function 以将 dataframe 分成需要按百分比格式化的变量和不需要格式化的变量。

get_map <- function(x, y){
  data("usgeojson")
  
  z<- look_for(x, y, details= FALSE, labels = TRUE,
               ignore.case = FALSE)
  a<- z$variable
  
  df<- x %>%
    select(all_of(a))
  
  decimals<-ifelse(mean(df[,1])>1,0,1)
  
  df[1]<-ifelse(mean(df[,1])>1,df[1],df[1]*100)    
  df[2]<-x[1]
  
  value1<-  colnames(df[1])
  highchart() %>%
    hc_add_series_map(usgeojson, df,
                      name = y,
                      value = value1,
                      joinBy = c("woename", "state"))%>%
    hc_mapNavigation(enabled = T) %>%
    hc_tooltip(valueDecimals= decimals,valueSuffix=ifelse(mean(df[,1])>100 | value1== "popratio","","%")
    ) %>% 
    hc_colorAxis(min= min(df[1]),
                 max= max(df[1]),
                 startOnTick= FALSE,
                 endOnTick= FALSE)
}

Ultimately it didn't actually require any JS function, although I imagine it still could have been solved that way as well.最终它实际上并不需要任何 JS function,尽管我想它仍然可以通过这种方式解决。 It was difficult to figure out the highcharter settings and convert them into syntax that worked with R.很难弄清楚 highcharter 设置并将它们转换为适用于 R 的语法。

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

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