highcharter libraryhighcharter libraryDownload a template .Rmd of this
activity. Put it in a folder
Assignment_11 in COMP_STAT_112.
At the beginning of the COVID-19 pandemic, the CDC created a flatten the curve graphic, which illustrated the importance of staying socially isolated to preven the spread of COVID. Throughout the pandemic, the New York Times and various government organizations used interactive data visualizations that allowed people to see the number of local cases and death rates. These graphics have played a crucial role in allowing people to make informed decisions about protecting themselves. Unfortunately, these visualizations have been largely inaccessible to people who are blind.
As you are learning in this class, data visualizations are an powerful way to quickly draw insights from data and gain a better understanding of the world. However, people who are blind are consistently locked out from accessing them. In this module, we will learn how to make our designs less disabling to this population.
Example 1 (Accessing a Data Visualizations Non-Visually) Chat with a classmate and brainstorm ways that someone who is blind might access the data within a data visualization. You can think about ways that they might already be doing so and future technology that could help. Get creative!
Globally over 2.2. billion people have some kind of visual impairment. Most people with visual impairments have some functional vision, this can include color blindness or blurry vision, so it’s important to keep those people in mind when you design your visualizations.
A few tips for designing accessible graphics:
Exercise 1 Copy and paste the data visualization below (showing how flipper and bill length differ across three species of penguins) into this color blindness simulator and look at with the Monochromacy lens. Update the color scale so the points are distinguishable in grayscale.
data(penguins, package = "palmerpenguins")
penguins %>%
ggplot(aes(x = flipper_length_mm, y = bill_length_mm, color = species)) +
geom_point()
We are going to be using the Highcharter library to create interactive and accessible graphics. Similar to Leaflet, the HighCharter library is an R wrapper for a JavaScript library (Highcharts). Because there is JavaScript underneath, that means we can use Highcharter to make interactive instead of static graphics.
To install the Highcharter library, run the following code in the console. NOTE Do not install the Highcharter library from CRAN or the packages menu, because the current version does not support the accessibility module!
remotes::install_github("jbkunst/highcharter@8ff41366c8c411b497b5378d27be48617360f81f")
If the current version of highcharter is already installed, you can uninstall it by navigating to the Packages frame in the lower right corner of the RStudio IDE, scrolling down to highcharter and clicking the x on the right-hand side of the row (see image below).
First, let’s take a look at what a highcharter plot looks like using the same penguin data we used above. Make sure to load the library before running the code below.
library(highcharter)
hchart(
penguins,
"scatter",
hcaes(x = flipper_length_mm, y = bill_length_mm, group = species)
)
Example 2 Chat with someone next to you. How do the default highcharter and ggplots differ for the penguin scatterplot? If you are feeling brave, try right-clicking the graph and selecting Inspect element to see how the images are represented in code.
img element in html and the highcharter graphic is represented as a scalable vector graphic (SVG), with different elements representing each of the points, text elements, etc… This is good news, both because you can make the elements interactive and abecause you can surface information about the elements to assistive technology such as a screen reader, so someone who is blind does not have to rely on alt text alone.As you can see the way to write code for highcharter is a little different than ggplot. You use the form:
hchart(<data>, <type_of_chart>, hcaes(<aesthetics>), ...)
where
data is the data to chart. Note that you can not pipe in data as you would with ggplot.type_of_chart is a string (uses "") to specify the type of chart. This value can be: line, spline, area, heatmap, treemap, etc.aesthetics is the mapping to use for plot. Note that highcharter will map to color if you use group or value.You can add more layers to the plot using hc_add_series().
Example 3 Use highcharter to recreate the ggplot line graph shown below showing the most popular female names. You can pipe to hc_title() and hc_subtitle() to add in titles.
PopularFemaleNames <- babynames %>%
filter(sex == "F") %>%
group_by(year) %>%
slice_max(n)
PopularFemaleNames %>%
ggplot(aes(x= year, y = n, color = name)) +
geom_line() +
#geom_point() +
labs(title = "Most popular name each year", subtitle = "For babies born in the U.S. and assigned the female sex at birth")
PopularFemaleNames %>%
hchart("line", hcaes(x= year, y = n, group = name)) %>%
hc_title(text = "Most popular name each year") %>%
hc_subtitle(text = "For babies born in the U.S. and assigned the female sex at birth")
Android or Apple phone - navigate to this webpage (typing without a physical keyboard can be quite frustrating ) - set up accessibility shortcut - single touch to read aloud (can also be sent to a Braille display), can swipe through the options on the screen - double tap to select or activate an item - rotor - importance of headings and link text on the screen (some websites links are tagged here)
note on Desktop screen reader- VoiceOver or NVDA, windows built=in?
Screen reader and keyboard navigation. Can also see graph in table form. Can export data as well
show what it looks like with inspect –svg vs img
hchart(
penguins,
"scatter",
hcaes(x = flipper_length_mm, y = bill_length_mm, group = species))
hchart(
penguins,
"scatter",
hcaes(x = flipper_length_mm, y = bill_length_mm, group = species)) %>%
hc_add_dependency(name = "modules/accessibility.js") %>%
hc_add_dependency(name = "modules/exporting.js") %>%
hc_add_dependency(name = "modules/export-data.js")
work through an example
Note about bars vs columns in highcharter
PopularNames <- babynames %>%
filter(sex == "F") %>%
group_by(name) %>%
summarize(total = sum(n)) %>%
arrange(desc(total)) %>%
head(10)
hchart(PopularNames, "column", hcaes(x = name, y = total))
hchart(PopularNames, "column", hcaes(x = name, y = total)) %>%
hc_add_dependency(name = "modules/series-label.js") %>%
hc_add_dependency(name = "modules/accessibility.js") %>%
hc_add_dependency(name = "modules/exporting.js") %>%
hc_add_dependency(name = "modules/export-data.js")
link to other ones, can do almost anything that Highcharts library can.
There is a lot more documentation about Highcharts. This is a good guide on how to convert from Highcharts (JavaScript) to Highcharter (R) notation.
In order to access your files from the phone/iPad, you will actually be publishing your html file to RStudio RPubs
Exercise 2 (Accessibility Fun) Create an accessible graph! You can pick a graph that you’ve created in a previous class or create something new. First convert it to a highcharter graph, then figure out how to make it accessible!
PopularNames %>%
hchart("point", hcaes(x = name, y = total)) %>%
hc_plotOptions(
series = list(
point = list(
events = list(
click = JS("function() {
this.sonify({
duration: 3000,
order: 'sequential',
pointPlayTime: 'x',
afterSeriesWait: 1000,
instruments: [{
instrument: 'sine',
instrumentMapping: {
volume: 0.8,
duration: 250,
pan: 'x',
frequency: 'y'
},
// Start at C5 note, end at C6
instrumentOptions: {
minFrequency: 520,
maxFrequency: 1050
}
}]
})
}"))))) %>%
hc_add_dependency(name = "modules/sonification.js")
You can play around with the numbers for:
highchart() |>
hc_series(
list(
name = "Tokyo",
data = c(7.0, 6.9, 9.5, 14.5, 18.4, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6)
),
list(
name = "London",
data = c(3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8)
)
) %>%
#hc_tooltip(formatter = JS("function () { return 'hello world'}"))
hc_plotOptions(
series = list(
point = list(
events = list(
click = JS("function() {
this.sonify({
duration: 3000,
order: 'sequential',
pointPlayTime: 'x',
afterSeriesWait: 1000,
instruments: [{
instrument: 'squareMajor',
instrumentMapping: {
volume: 0.8,
duration: 250,
pan: 'x',
frequency: 'y'
},
// Start at C5 note, end at C6
instrumentOptions: {
minFrequency: 520,
maxFrequency: 1050
}
}]
})
}"))))) %>%
hc_add_dependency(name = "modules/sonification.js")
highchart() |>
hc_series(
list(
name = "Tokyo",
data = c(7.0, 6.9, 9.5, 14.5, 18.4, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6)
),
list(
name = "London",
data = c(3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8)
)
) %>%
#hc_tooltip(formatter = JS("function () { return 'hello world'}"))
hc_plotOptions(
series = list(
events = list(
click = JS("function() {
this.sonify({
duration: 3000,
pointPlayTime: 'x',
instruments: [{
instrument: 'squareMajor',
instrumentMapping: {
volume: 0.8,
duration: 250,
pan: 'x',
frequency: 'y'
},
// Start at C5 note, end at C6
instrumentOptions: {
minFrequency: 120,
maxFrequency: 350
}
}]
})
}")))) %>%
hc_add_dependency(name = "modules/sonification.js")
PopularFemaleNames %>%
hchart("line", hcaes(x= year, y = n, group = name)) %>%
hc_title(text = "Most popular name each year") %>%
hc_subtitle(text = "For babies born in the U.S. and assigned the female sex at birth") %>%
hc_plotOptions(
series = list(
events = list(
click = JS("function() {
this.sonify({
duration: 3000,
pointPlayTime: 'x',
instruments: [{
instrument: 'triangleMajor',
instrumentMapping: {
volume: 0.8,
duration: 250,
pan: 'x',
frequency: 'y'
},
// Start at C5 note, end at C6
instrumentOptions: {
minFrequency: 120,
maxFrequency: 350
}
}]
})
}")))) %>%
hc_add_dependency(name = "modules/sonification.js")
Exercise 3 (Sonification Fun) Create a sonified graph! You can pick a scatter or line plot graph that you’ve created in a previous class (e.g. for ) or create something new and add sonification!
Thanks to Mara Averick for her detailed blog posts on incorporating accessibility into highcharter, which inspired this module, mrjoh3 for the github issue that helped me determine which older version of highcharter to use for accessibility, and the team behind Highcharts and Highcharter for adding such awesome and heavily researched accessibility features.
#more line graphs
babynames %>%
filter(name %in% c("Lauren"), sex == "F") %>%
ggplot(aes(x= year, y = n)) +
geom_line() +
labs(title = "Popularity of the name 'Lauren' over the years", subtitle = "For babies born in the U.S. and assigned the female sex at birth", y = "Count", x = "")
babynames %>%
filter(name == "Lauren", sex == "F") %>%
hchart("line", hcaes(x= year, y = n))
#more bar plots
PopularNames <- babynames %>%
filter(sex == "F") %>%
group_by(name) %>%
summarize(total = sum(n)) %>%
arrange(desc(total)) %>%
head(10)
PopularNames %>%
ggplot(aes(x = name, y = total)) +
geom_col()