I'm trying to create a bar chart showing Cost of Living indexed to the U.S. value (100) in the Highcharter package in R. So I want the bars the start at 100 and either extend to the right or the left of 100. I have the basic bar chart down, but I can't figure out how to move the center line of the chart to 100. I'm trying to get it to look something like this: http://drawingwithnumbers.artisart.org/moving-the-center-line-of-a-bar-chart-with-a-gantt-chart/
Here's the data I'm working with:
States <- c('Tennessee','Michigan','Indiana','Ohio','Kentucky','West Virginia','North Carolina','Virginia','Pennsylvania','Delaware','New Jersey','Maryland','New York')
Cost_of_Living <- c(88.7,88.9,90,90.8,90.9,91.1,94.9,100.7,101.7,108.1,125.1,129.7,139.1)
costliving <- data.frame(States, Cost_of_Living)
And here's the code I have for the bar chart, just the basic bar chart
costliving_graph <- highchart() %>%
hc_title(text = "Cost of Living by State (2020)") %>%
hc_add_series (costliving, "bar", hcaes(x = States, y = Cost_of_Living)) %>%
hc_xAxis(categories = costliving$States) %>%
hc_yAxis(title = list(text = "Indexed to the U.S. (U.S. Value = 100)"))%>%
hc_plotOptions (bar = list(colorByPoint = TRUE)) %>%
hc_legend (enabled = FALSE)%>%
hc_tooltip(pointFormat = "MSA Regional Price Parities: {point.y}", headerFormat ="")
costliving_graph
Any help would be great! Thanks in advance
After checking examples on highcharter website I changed the charttype to columnrange and added new vars low and high to the dataframe. Try this:
library(highcharter)
library(dplyr)
States <- c('Tennessee','Michigan','Indiana','Ohio','Kentucky','West Virginia','North Carolina','Virginia','Pennsylvania','Delaware','New Jersey','Maryland','New York')
Cost_of_Living <- c(88.7,88.9,90,90.8,90.9,91.1,94.9,100.7,101.7,108.1,125.1,129.7,139.1)
costliving <- data.frame(States, Cost_of_Living) %>%
mutate(low = pmin(Cost_of_Living, 100),
high = pmax(Cost_of_Living, 100))
costliving_graph <- highchart() %>%
hc_title(text = "Cost of Living by State (2020)") %>%
hc_add_series(costliving, "columnrange", hcaes(x = States, y = Cost_of_Living, low = low, high = high)) %>%
hc_xAxis(categories = costliving$States) %>%
hc_yAxis(title = list(text = "Indexed to the U.S. (U.S. Value = 100)"))%>%
hc_plotOptions (bar = list(colorByPoint = TRUE)) %>%
hc_legend (enabled = FALSE)%>%
hc_tooltip(pointFormat = "MSA Regional Price Parities: {point.y}", headerFormat ="")
costliving_graph
Related
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:
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.
I tried to make interactive map using R shiny which will show plot of male and female citizens in some cities. Data frame sample is shown below.
df1 <- read.table(header = TRUE, text = "city,year,male,female,long,lat
A,2017,1038,876,35.54331,139.12333
A,2018,1281,911,35.54331,139.12333
B,2017,832,517,35.14189,140.664113
B,2018,914,589,35.14189,140.664113", sep = ",")
df2 <- df1
The interactive map is built by using leaflet package and if the city marker is clicked, a plot which built by highchart will be shown.
output$chart <- renderHighchart({
df2 <- df1[df1$city == click_marker(),]
hchart() %>%
hc_add_series(df2, "column", hcaes(x = year, y = male, group = city, name = "Male")) %>%
hc_add_series(df2, "column", hcaes(x = year, y = female, group = city, name = "Female")) %>%
hc_xAxis(title = list(text = "Year")) %>%
hc_yAxis(title = list(text = "Amount (Thousands)"))
})
highchartOutput('chart')
I expect the output is a plot that show amount of male and female in the city for each year given but the output that I got is "argument object is missing, with no default."
What if you change hchart to highchart and the names to lowercase? Apparently they have to be found in the data.frame.
highchart() %>%
hc_add_series(df2, "column", hcaes(x = year, y = male, group = city, name = "male")) %>%
hc_add_series(df2, "column", hcaes(x = year, y = female, group = city, name = "female")) %>%
hc_xAxis(title = list(text = "Year")) %>%
hc_yAxis(title = list(text = "Amount (Thousands)"))
I want to plot a multiple stacked bar chart, but I don't know how to combine the r code.
Closing Date Non Current Assets Current Assets Non Current Liabilities
2 2013/12 13637344 13078654 9376243
3 2014/12 14075507 12772388 8895126
4 2015/12 14578093 14226181 9715914
5 2016/12 10911628 10205708 9810157
6 2017/12 10680998 10950779 13493110
Current Liabilities
2 5075985
3 4963856
4 5992229
5 8859263
6 4094183
I can plot multiple bar chart by:
highchart() %>%
hc_chart(type = "column") %>%
hc_xAxis(categories = bs.table$`Closing Date`) %>%
hc_add_series(name="Non Current Assets",data = bs.table$`Non Current
Assets`) %>%
hc_add_series(name="Current Assets",data = bs.table$`Current Assets`) %>%
hc_add_series(name="Non Current Liabilities",data = bs.table$`Non Current Liabilities`) %>%
hc_add_series(name="Current Liabilities",data = bs.table$`Current Liabilities`) %>%
hc_add_theme(hc_theme_ft())
and only one stacked bar like this:
highchart() %>%
hc_chart(type = "column") %>%
hc_title(text = "MyGraph") %>%
hc_plotOptions(column = list(
dataLabels = list(enabled = FALSE),
stacking = "normal",
enableMouseTracking = FALSE)
) %>%
hc_series(list(name="Current Assets",data=bs.table$`Current Assets`),
list(name="Non Current Assets",data=bs.table$`Non Current Assets`))
my desired output is like this:
I try to combine the code by adding:
%>% hc_series(list(name="Current Liabilities",data=bs.table$`Current Liabilities`),
list(name="Non Current Liabilities",data=bs.table$`Non Current Liabilities`))
on the second code but it doesn't work, it added up the same bar. Please advice.
You can specify which bars belong in which groups by adding stack to each series, like this:
library(highcharter)
bs.table = data.frame(
Closing.Date = paste(2013:2017, 12, sep = "/"),
Non.Current.Assets = c(13637344, 14075507, 14578093, 10911628, 10680998),
Current.Assets = c(13078654, 12772388, 14226181, 10205708, 10950779),
Non.Current.Liabilities = c(9376243, 8895126, 9715914, 9810157, 13493110),
Current.Liabilities = c(5075985, 4963856, 5992229, 8859263, 4094183)
)
highchart() %>%
hc_chart(type = "column") %>%
hc_plotOptions(column = list(stacking = "normal")) %>%
hc_xAxis(categories = bs.table$Closing.Date) %>%
hc_add_series(name="Non Current Assets",
data = bs.table$Non.Current.Assets,
stack = "Assets") %>%
hc_add_series(name="Current Assets",
data = bs.table$Current.Assets,
stack = "Assets") %>%
hc_add_series(name="Non Current Liabilities",
data = bs.table$Non.Current.Liabilities,
stack = "Liabilities") %>%
hc_add_series(name="Current Liabilities",
data = bs.table$Current.Liabilities,
stack = "Liabilities") %>%
hc_add_theme(hc_theme_ft())
(To group by current vs. non-current instead of assets vs. liabilities, just rename stack in each series appropriately.)
I am struggling in adding one level drilldown in my grouped column chart made using highcharter. To explain, I am taking using the "vaccines" dataset available in highcharter library :
My code (similar) that creates the grouped column chart :
library (highcharter)
library(dplyr)
df <- na.omit(vaccines[vaccines$year %in% c("1928", "1929"),])
df <- ddply(df, c("state", "year"), summarise, count = sum(count))
hc <- hchart(df, type = "column", hcaes(x = state, y = count, group = year)) %>%
hc_xAxis(title = list(text = "States")) %>%
hc_yAxis(title = list(text = "Vaccines")) %>%
hc_chart(type = "Vaccines", options3d = list(enabled = TRUE, beta = 0, alpha = 0)) %>%
hc_title(text = "Demo Example") %>%
hc_subtitle(text = "Click on the on Year to see the Vaccine drill down")
hc
It creates this grouped chart perfectly
I now want to add one level drill down to the chart where I can select the "Year" and corresponding drill down data of the vaccine selected is presented. Can you please help with the best/easiest way to do it considering I have the individual drill down data also in data frames.
Regards,
Nikhil