How can I get highchart graphs to display in RMarkdown? - r

I am creating an html document and want to include interactive visualizations. When I use highcharter in RStudio, my graph displays perfectly, but when I put the same code in RMarkdown, I keep getting errors: "could not find function 'hchart'; could not find function 'hcaes'".
I have been using this page as a guide, but I can't get my graphs to display:
https://www.kaggle.com/nulldata/beginners-guide-to-highchart-visual-in-r/code
This is the code I've tried using:
highcharter::hchart(TEST,
type = "column",
hcaes(x = `Service.Month`,
y = value,
group = variable)) %>%
hc_exporting(enabled = TRUE) %>%
hc_xAxis(title = list(text = "Service Month")) %>%
hc_yAxis(title = list(text = "Total Number")) %>%
hc_tooltip(crosshairs = TRUE,
backgroundColor = "#FCFFC5",
shared = TRUE) %>%
hc_title(text = "Title")

Related

How to create a tooltip chart in r using highcharter?

I am using highcharter library and referred to below link to create an interactive tooltip chart in a bubble chart
https://jkunst.com/blog/posts/2019-02-04-using-tooltips-in-unexpected-ways/
Plot image:
Using gapminder data as shown in link I was able to reproduce the same but when I use my other data then the tool tip chart doesn't appear.
Code for my other data:
libs
library(tidyverse)
library(highcharter)
data
grouped_cases_df <- read.csv("https://raw.githubusercontent.com/johnsnow09/covid19-df_stack-code/main/grouped_cases.csv")
tt_base <- grouped_cases_df %>%
arrange(desc(Date)) %>%
distinct(Country.Region, .keep_all = TRUE)
tt_base
tt_inner <- grouped_cases_df %>%
select(Country.Region, Date, Daily_cases) %>%
nest(-Country.Region) %>%
mutate(
data = map(data, mutate_mapping, hcaes(x = Date, y = Daily_cases), drop = TRUE),
data = map(data, list_parse)
) %>%
rename(tt_nestdata = data)
tt_inner
tt_daily <- left_join(tt_base, tt_inner, by = "Country.Region")
tt_daily
hchart(
tt_daily,
"point",
hcaes(x = Active, y = Confirmed, name = Country.Region,
size = Daily_cases, group = continent, name = Country.Region)
) %>%
hc_yAxis(type = "logarithmic") %>%
hc_tooltip(
useHTML = TRUE,
headerFormat = "<b>{point.key}</b>",
pointFormatter = tooltip_chart(accesor = "tt_nestdata")
) %>%
hc_title(text = "Active Vs Confirmed Cases as of latest Date") %>%
hc_subtitle(text = "Size of bubble based on Deaths <br> (ttchart: population growth)")
Issue: Getting blank tooltip chart for every country.
I also tried by changing Country.Region to as.factor() but didn't help. I am not sure whats wrong with this.
It's needed make two changes:
The tooltip data needs to be ready to highcharter. So you need to transform the Date column from text to date then to a numeric value which highcharts can interpret as date:
mutate(Date = highcharter::datetime_to_timestamp(lubridate::ymd(Date)))
Then, in the hc_opts argument in the tooltip_chart function you need to specify the x Axis treat the values as date.
pointFormatter = tooltip_chart(accesor = "tt_nestdata", hc_opts = list(xAxis = list(type = "datetime")))
Then:

Highcharter hover showing incorrect point

I'm having an issue where when I create a highcharter graph for Shiny, the hover functionality doesn't work properly. I have used highcharter a ton and have never had this issue, but for some reason on this dashboard, anytime I hover over a point it either doesn't register the hover, or it provides the tooltip for a different point. Screenshot example here. Anyone have this issue before? The red arrow shows where my mouse was when taking this screenshot.
My code is below (note, there are two graphs in the fluidRow(), but removing one and making the graph take the whole row does not fix the issue).:
`############
Code in UI
fluidRow(align = "center",splitLayout(cellWidths = c("50%","50%"),
withLoader(highchartOutput(outputId = "lineChart1", height = 500, width = 700),type = "html",loader = "dnaspin"),
withLoader(highchartOutput(outputId = "lineChart2", height = 450, width = 700),type = "html",loader = "dnaspin")
)
)
`
#
Code for the actual graph (in the Server)
output$lineChart1 <- renderHighchart({
impactData <- read.csv("IMPACT_dash.csv")
colnames(impactData) <- c("Week","notAtAll","notButWill","hasSomewhat",
"hasConsiderably","hasDevastating")
highchart() %>%
hc_add_series(data = impactData, "line", color = "#404040", hcaes(x = Week, y = notAtAll),
name = "It has not and will probably never impact my life at all") %>%
hc_add_series(data = impactData, "line", color = "#8f8f8f", hcaes(x = Week, y = notButWill),
name = "It has not but will probably impact my life in the future") %>%
hc_add_series(data = impactData, color = "#91ad80", "line", hcaes(x = Week, y = hasSomewhat),
name = "It has impacted my life somewhat") %>%
hc_add_series(data = impactData, color = "#8fc96b", "line", hcaes(x = Week, y = hasConsiderably),
name = "It has impacted my life considerably") %>%
hc_add_series(data = impactData, color = "#83d64f", "line", hcaes(x = Week, y = hasDevastating),
name = "It has had a devastating impact on my life") %>%
hc_plotOptions(
series = list(
showInLegend = T,
pointFormat = "{point.y}%"
), column = list(colorByPoint = T)
) %>%
hc_yAxis(title = list(text = "Percent"),
#lables = list(format("{value}%"),
max = 100) %>%
hc_xAxis(title = list(text = "Week"), categories = impactData$Week) %>%
hc_title(text = "") %>%
hc_tooltip(pointFormat = "{point.y}%")
})
`
Sample Data:
Found the answer. It was because in my dashboard I had set it to show the page as zoomed out at 80% when opened. This caused issues with the hovers in the graph. The code that caused the issue is below

R Highcharter: On x-axis, Date is not showing correctly when dataframe has only one result

I'm building a shiny app that displays actual vs planned expenditure on a monthly basis. I've created controls that allow the user to select a specific project. But in some projects, there are only planned expenditure for a single month is there. For those projects, the Date is not coming properly on the X-Axis.
He is the code that I've written:
renderHighchart({
highchart() %>%
hc_chart(type = "column") %>%
hc_xAxis(categories = planned_vs_actual()$documentDate, title = list(text = "<b>Date</b>"), type = "datetime") %>%
hc_add_series(name="Planned Expenditure",
data = planned_vs_actual()$PlannedExpenditure) %>%
hc_add_series(name="Actual Expenditure",
data = planned_vs_actual()$ActualExpenditure) %>%
hc_tooltip(borderWidth = 1.5,
pointFormat = paste('<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b><br/>')) %>%
hc_legend(enabled = TRUE) %>%
hc_title(text = "Planned vs Actual Expenditure (In Crores)") %>%
hc_subtitle(text = dataPeriod) %>%
hc_yAxis(title = list(text = "<b>Amount <br>(In Crores)</br></b>"))%>%
hc_add_theme(custom_theme)
})
Finally found the solution on this link: https://github.com/jbkunst/highcharter/issues/395
Just need to make this change:
hc_xAxis(categories = as.list(planned_vs_actual()$documentDate), title = list(text = "<b>Date</b>"), type = "datetime")
Put the date in as.list() function to show it properly on x-axis.

The type 'variwide' in highcharter does not work

I would like to create a variwide chart in Highcharter to use in R,but the result is empty。
library(data.table)
library(highcharter)
labor_cost <- data.table(country =c('挪威','丹麦','比利时','瑞典','法国','荷兰','芬兰','德国','奥地利','爱尔兰','意大利','英国','西班牙','希腊','葡萄牙','捷克共和国','波兰','罗马尼亚'),cost=c(50.2,42,39.2,38,35.6,34.3,33.2,33,32.7,30.4,27.8,26.7,21.3,14.2,13.7,10.2,8.6,5.5),gdp=c(335504,277339,421611,462057,2228857,702641,215615,3144050,349344,275567,1672438,2366911,1113851,175887,184933,176564,424269,169578))
highchart() %>%
hc_title(text = "欧洲各国人工成本", align="center")%>%
hc_xAxis(type = "category",title = list(text = "* 柱子宽度与 GDP 成正比")) %>%
hc_legend(enabled = FALSE) %>%
hc_add_series(data = labor_cost,type = "variwide",hcaes(x = country,width= gdp),dataLabels = list(enabled = TRUE,format="€{point.y:.0f}"),colorByPoint = TRUE)
The code executes normally,no errors appear.

Rename the "slices" in Pie chart using Highcharts in shiny R

Used the below code to make a pie chart in shiny R interface using Highcharts but was unable to name the slices. I have used a dataset stored in csv format from my app folder.
output$Hist<-renderHighchart({
data<-read.csv(paste0(getwd(),"/data.csv"),header=TRUE,stringsAsFactors=FALSE)
highchart() %>%
hc_chart(type = "pie") %>%
hc_plotOptions(
series = list(showInLegend = TRUE)
) %>%
hc_add_series(data = data$Count, name = data$X) %>%
hc_tooltip(valueDecimals = 0,
pointFormat = "Count: {point.y}") %>%
hc_legend(enabled = TRUE)%>%
Image of the chart:
Like this:
library(highcharter)
data <- data.frame(X=c("name1","name2"),Count =c(20,30))
highchart() %>%
hc_add_series_labels_values(data$X, data$Count, name = "Pie",colorByPoint = TRUE, type = "pie")

Resources