R Highcharter - accessing point name - r

I created a highchart and hope to pass the point information (name, description etc...) into another function. However I can't seem to find a way to access the point name. Below is my current attempt.
# Generate chart
highchart() %>%
hc_chart(type = 'organization', inverted = TRUE) %>%
hc_title(text = sprintf('%s Org Chart', chartName)) %>%
hc_add_series(
name = chartName,
data = hierarchyList,
nodes = nodeList,
colorByPoint = FALSE,
color = '#D6E8F3',
dataLabels = list(color = 'black',
nodeFormat = getNodeFormat("{point.image}", "{point.name}", "{point.title}", "{point.description}", ...),
borderColor = 'white',
nodeWidth = 190
) %>%
hc_tooltip(outside = TRUE)
}
getNodeFormat <- function(image, name, title, description...) {
... #more codes here
}
In the getNodeFormat function, there are other computations and data accessing that requires the actual name string of the point. However, when I try to print out name in getNodeFormat, it displays "{point.name}" instead of the actual name of the point.
Other attempts instead of "{point.name}" are point.name, and {point.name}. None of them worked and the error stated "object not found".
Is there a way where I can access the point information directly? Any help will be appreciated!

Related

Is there a way to add a click event to open a url in a highcharter plot without shiny?

I am trying to make a map plot using highcharter in which the points can be clicked to access their associated url. I've created the following simplified version of what I am doing (I'm doing a mapbubble plot instead) to illustrate my code. What am I doing wrong? Note: I have also tried this.point.options.url in the javascript section of the code.
library(dplyr)
library(highcharter)
data("USArrests", package = "datasets")
USArrests = mutate(USArrests, "woe-name" = rownames(USArrests))
USArrests[["url"]] = c("https://www.google.com/", "https://www.wikipedia.org/")
hcmap(map = "countries/us/us-all", data = USArrests,
joinBy = "woe-name", value = "UrbanPop", name = "Urban Population",
allowPointSelect = TRUE) %>%
hc_plotOptions(
point = list(
events = list(
click = JS("function() {
window.open(point.url);
}"
)
)
)
)
There are 2 issues with your code:
The map tile is a map and not a point, thus, your option file should link the event handler to map and not point.
To access the urlyou have to use a parameter in your JS function via which you can access the url eventually.
hcmap(map = "countries/us/us-all", data = USArrests,
joinBy = "woe-name", value = "UrbanPop", name = "Urban Population",
allowPointSelect = TRUE) %>%
hc_plotOptions(
map = list(
events = list(
click = JS("function(self) {
window.open(self.point.url);
}"
)
)
)
)

Is there a way to prevent highcharter map from automatically choosing points?

I am trying to make to make a plot in highcharter that has the districts of my country as mappoints. My issue is that instead of highcharter displaying the information when my cursor is on a point, it automatically outputs the details of the nearest point even if my cursor is nowhere near the map (but is still within the frame displaying the map). Is there a way to resolve this?
Some details: nash_geo is a shape file (converted to geojson) of my country and centers_geojson is a dataframe (converted to geojson) containing the coordinates and names of the districts I am superimposing on the map.
highchart(type = "map") %>%
hc_add_series(mapData = nash_geo, showInLegend = FALSE) %>%
hc_add_series(data = centers_geojson, type = "mappoint",
dataLabels = list(enabled = FALSE),
name = "District", tooltip = list(pointFormat = "
{point.properties.district}"))

Highcharts X axis categories name gets only shows 1 character

I created a column and line chart with highcharts.
X axis category names have been appearing fine in other charts created with the same code but now I encounter an issue where the entire category name gets cut short with only the first letter only displayed in the chart.
For example a category should read 'Run' but it only shows up as 'R'.
I also noted that this occurs in charts where there is only 1 category to display (so like a lone column chart with only one column displayed).
Would appreciate some help here :)
highchart() %>%
hc_title(text = "Creative Performance") %>%
hc_yAxis_multiples(
list(title = list(text = "Spend"),labels=list(format ='${value:,.2f}'), min=0, max=max(data_creative_trueview$Spend), showFirstLabel= TRUE,showLastLabel=TRUE,opposite = FALSE),
list(title = list(text = "TrueView: CPCV"),min=0,max = max(data_creative_trueview$`TrueView: CPCV`)+0.5, labels = list(format = "${value:,.2f}"),showLastLabel = FALSE, opposite = TRUE)) %>%
hc_add_series(data = data_creative_trueview, type= "column", hcaes(y=Spend), name="Spend") %>%
hc_add_series(data = data_creative_trueview, type = "line", hcaes(y=`TrueView: CPCV`), name="TrueView: CPCV", yAxis=1) %>%
hc_xAxis(title="Creative", categories=data_creative_trueview$Creative) %>%
hc_tooltip(pointFormat = '${point.y:,.2f}') %>%
emphasized text hc_legend(align = "right", verticalAlign = "top")
I found an answer right after I posted this. Apparently this is a common issue. Categories must be provided as a list when there is only one category. Refer to: https://github.com/jbkunst/highcharter/issues/173
In my case xlabel={{"64"}} showed only 6 where as xlabel={{"64",}} gave me 64 which was what I have wanted.

Highcharter hc_add_series_map function : merging regions and displaying labels

I would like to merge regions in a map and not display intra borders. I also would like to always display the names of the newly grouped regions.
Is hc_add_series_map the right instruction for doing so ?
Thanks in advance for your help
(I do not use Java for programming, only R)
Here is an example :
mapdata <- get_data_from_map(download_map_data("countries/fr/fr-all-all"))
glimpse(mapdata)
set.seed(1234)
data_fake <- mapdata %>%
select(code = `hc-a2`) %>%
mutate(value = 1e5 * abs(rt(nrow(.), df = 10)))
glimpse(data_fake)
hcmap("countries/fr/fr-all-all", data = data_fake, value = "value",
joinBy = c("hc-a2", "code"), name = "Fake data",
dataLabels = list(enabled = TRUE, format = '{point.code}'),
borderColor = "#FAFAFA", borderWidth = 0.1,
tooltip = list(valueDecimals = 2))
enter image description here
I would like to add borders for 5 grouped "big" regions, either by deleting 'intra borders' in a region, or adding black lined borders for big regions.
Thanks in advance.

Having trouble mapping highcharter aesthetics to reactive object elements

I have a large shiny app that allows users to filter through an API and spark table aggregate (dumped to an .Rdata) simultaneously using the same set of initially selectized parameters. Fitting all this into a reproducible example is going to be tough, but, this is the function that is grouping and summming my metric of interest (try to resist asking me to paste in partitionFiltered()):
df <- reactive({partitionFiltered() %>%
dplyr::group_by(updatedTimeHour, direction) %>%
dplyr::mutate(count_dir = sum(n_flows)) %>%
dplyr::ungroup() %>%
dplyr::select(updatedTimeHour, direction, count_dir) %>%
dplyr::arrange(updatedTimeHour) %>%
unique()})
(Eventually, updatedTimeHour and direction will be replaced by input$periodicity and input$dimension, respectively, but that is beyond the scope of this question.)
The df() object looks like:
updatedTimeHour direction count_dir
6 1 525071.00
6 2 3491.00
6 0 498816.00
6 3 5374.00
7 2 2432.00
7 0 303818.00
7 1 340768.00
7 3 4852.00
8 1 1969048.00
My highcharter calls do not seem to be grouping and coloring the aesthetics as I would expect:
hc <- highchart() %>%
hc_add_series(data = df()$count_dir,
type = input$plot_type,
name = factor(df()$direction)
showInLegend = TRUE,
# ??group = df()$direction,
# ??color = df()$direction,
# ??x = df()$updatedTimeHour, y = df()$count_dir, color = df()$direction,
# ??hcaes(x = df()$updatedTimeHour, y = df()$count_dir, color = df()$direction)
) %>%
hc_xAxis(type = 'datetime',
# ??group = factor(df()$direction),
categories = df()$updatedTimeHour,
tickmarkPlacement = "on",
opposite = FALSE) %>%
hc_title(text = "NetFlows, by Hour",
style = list(fontWeight = "bold")) %>%
hc_exporting(enabled = TRUE, filename = "threat_extract")
As you can probably tell, I'm very confused about where/how to map the x-grouping udpatedTimeHour, or color the different direction levels appropriately and make sure their group ends up mapped correctly to the labels in the legend and hover.
I have also attempted to map these aesthetics using the hcaes() call I see included as an argument to hc_add_series() in some of the documentation, but I get errors thrown saying that that argument is not (any longer?) named in that hc_ function...
Any help is appreciated, and a related question is here.
You are trying to add as one series multiple objects that's the reason why is not working. Just changing a little bit your code and using the "magic" function hchart it should work:
df = data_frame(updatedTimeHour = c(6,6,6,6,7,7,7,7,8), direction = c(1,2,0,3,2,0,1,3,1), count_dir = rnorm(9))
type = "line"
hchart(df, type, hcaes(x = updatedTimeHour, y = count_dir, group = as.factor(direction))) %>%
hc_title(text = "NetFlows, by Hour",
style = list(fontWeight = "bold")) %>%
hc_exporting(enabled = TRUE, filename = "threat_extract")

Resources