Rename the "slices" in Pie chart using Highcharts in shiny R - 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")

Related

Highcharter - Having one or more of the grouping elements pre-selected

I'm producing a set of charts using highcharter. I have items, which each have variants, and units sold by variant. I'm looking for a method by which I can choose which variants are pre-selected to appear on the chart.
Below is an example of the chart I produced:
library(tidyverse)
library(highcharter)
library(viridis)
df <- tibble(item_name = c('beer','beer','soft drink','soft_drink'),
units = c(15,50,25,10),
variant_name = c('blonde','white','coke','lemonade'))
cols = as.vector(scales::viridis_pal(option = "turbo", direction = 1)(length(unique(df$variant_name))))
df %>%
group_by(item_name) %>%
arrange(desc(units)) %>%
ungroup() %>%
hchart(
"column", hcaes(x = item_name, y = units, group = variant_name),
stacking = "normal"
) %>%
hc_colors(c(cols))
I would like to be able to pre-select, let's say 'blonde' and 'coke'. Other variants would have to be selected by clicking on the variant name in the chart:
I haven't been able to find a way to do that so far, the documentation for highcharts only points to doing so when you have multiple series.
You could write your own JS function to load the elements. Where you can specify the chart.series.load, check this link for extra info. Here is a reproducible example:
library(tidyverse)
library(highcharter)
library(viridis)
df <- tibble(item_name = c('beer','beer','soft drink','soft_drink'),
units = c(15,50,25,10),
variant_name = c('blonde','white','coke','lemonade'))
cols = as.vector(scales::viridis_pal(option = "turbo", direction = 1)(length(unique(df$variant_name))))
df %>%
group_by(item_name) %>%
arrange(desc(units)) %>%
ungroup() %>%
hchart(
"column", hcaes(x = item_name, y = units, group = variant_name),
stacking = "normal"
) %>%
hc_chart(events = list(load = JS("function() {
var chart = this;
chart.series[1].setVisible(true)
chart.series[2].setVisible(false)
chart.series[3].setVisible(false)
chart.series[4].setVisible(false)
}"))) %>%
hc_colors(c(cols))
Output:

R Shiny App HighCharter OHLC/Candlesticks default zoom period

I am using highcharter library for a shiny app to make candlesticks chart of an XTS. Code in server.R to generate the chart is given below (sry, this code is not reproducible) By default the chart generated shows data for all period. I want the zoom level to be changed to 1 month. It is equivalent to clicking on "1m" in zoom options. How can I do that?
library(highcharter)
output$ohlcPlot <- renderHighchart({
if (IsValidNSESymbol(input$x1StockCode)) {
df <- loadStockPrices()
highchart(type = "stock") %>%
hc_add_series(data = df,
name = "OHLC",
type = "candlestick") %>%
hc_colors(color = "red")
}
})
You can add %>% hc_rangeSelector(selected = 0) to keep month value as default where 0 is the position of the zoom option.
For eg with AAPL stock.
library(highcharter)
quantmod::getSymbols('AAPL',src = 'yahoo',from = "2013-01-01", to = "2017-12-31")
highchart(type = "stock") %>%
hc_add_series(data = AAPL,
name = "OHLC",
type = "candlestick") %>%
hc_colors(color = "red") %>%
hc_rangeSelector(selected = 0)

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:

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.

How can I get highchart graphs to display in RMarkdown?

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")

Resources