This data attempts to illustrate a controversial theory suggesting that the emergence of democratic political systems has depended largely on nations having low rates of infectious disease, from the Global Infectious Diseases and Epidemiology Network and Democratization: A Comparative Analysis of 170 Countries
Load the required packages and dataset from our course datasets link
# load required packageslibrary(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.5
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ ggplot2 3.5.2 ✔ tibble 3.2.1
✔ lubridate 1.9.4 ✔ tidyr 1.3.1
✔ purrr 1.0.4
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# load disease and democracy datadisease_democ <-read_csv("disease_democ.csv")
Rows: 168 Columns: 4
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (2): country, income_group
dbl (2): democ_score, infect_rate
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Change the font
R’s basic fonts are fairly limited (run: names(postscriptFonts())) to view those available). Using extrafont in three easy steps The first step is to install extrafont, and then import the fonts from your system into the extrafont database:
# R's basic fonts are fairly limited. View those available by running this codenames(postscriptFonts())
The default gray theme of ggplot2 has a rather academic look. You can use one of the ggplot2 built-in themes, and then customize the fonts.
disease_democ_chart <-ggplot(disease_democ, aes(x = infect_rate, y = democ_score)) +labs(x="Infectious Disease Prevalence Score",y="Democratization Score",title="Comparison of Disease Prevalence and Democratization Score") +theme_minimal(base_family ="serif")disease_democ_chart
Add a layer with points
This code will add a geom layer with points to the template:
disease_democ_chart +geom_point()
Customize the two layers we’ve added to the chart
The following code modifies the two geom layers to change their appearance.
disease_democ_chart +geom_point(size =3,alpha =0.5) +geom_smooth(method = lm, se =FALSE, color ="red")
`geom_smooth()` using formula = 'y ~ x'
Customize again, coloring the points by income group
You can make a dashed line by: linetype = “dotdash”, or equivalently, lty = 2
disease_democ_chart +geom_point(size =3, alpha =0.5, aes(color = income_group)) +geom_smooth(method = lm, se =FALSE, color ="black", lty =2, linewidth =0.3)
`geom_smooth()` using formula = 'y ~ x'
Color the entire chart by income group
Notice how the aes function colors the points by values in the data, rather than setting them to a single color. ggplot2 recognizes that income_group is a categorical variable, and uses its default qualitative color palette.
Now run this code, to see the different effect of setting the aes color mapping for the entire chart, rather than just one geom layer.
Because here we mapped the variable income group to color for the whole chart, and not just the points, it also affects the geom_smooth layer, so a separate trend line, colored the same as the points, is calculated for each income_group.
Set the axis ranges, and use a different color palette
You can apply ColorBrewer qualitative palettes by using the scale_color_brewer function. Add the text you want to appear as a legend title using name.
# set the axis ranges, change color paletteggplot(disease_democ, aes(x = infect_rate, y = democ_score, color=income_group)) +labs(x="Infectious Disease Prevalence Score",y="Democratization Score",title="Comparison of Disease Prevalence and Democratization Score") +theme_minimal(base_size =14, base_family ="serif") +geom_point(size =3, alpha =0.5) +geom_smooth(method=lm, se=FALSE, lty =2, linewidth =0.3)+scale_color_brewer(name="Income group", palette ="Set1")
`geom_smooth()` using formula = 'y ~ x'
Notice how slopes for each income group are different. What might that indicate?
DS Labs Datasets
###Use the package DSLabs (Data Science Labs) There are a number of datasets in this package to use to practice creating visualizations
# install.packages("dslabs") # these are data science labslibrary("dslabs")data(package="dslabs")list.files(system.file("script", package ="dslabs"))
Note that the package dslabs also includes some of the scripts used to wrangle the data from their original source:
US murders
This dataset includes gun murder data for US states in 2010. I use this dataset to introduce the basics of R program.
library(ggthemes) # a package of new themeslibrary(ggrepel) # will help us add text labels to pointsdata("murders")
Work with the Murders Dataset
Calculate the average murder rate for the country
Once we determine the per million rate to be r, this line is defined by the formula: y=rx, with y and x our axes: total murders and population in millions respectively.
In the log-scale this line turns into: log(y)=log(r)+log(x).
Create a static graph for which each point is labeled
Use the data science theme. Plot the murders with the x-axis as population for each state per million, the y-axis as the total murders for each state.
Color by region, add a linear regression line: use geom_smooth(method=“lm”, se=FALSE)
Scale the x- and y-axes by a factor of log 10, add axes labels and a title.
You can use the command nudge_x argument, if you want to move the text slightly to the right or to the left:
murders |>ggplot(aes(x = population/10^6, y = total, label = abb)) +geom_point(aes(color = region), size =2) +geom_smooth(method ="lm", se =FALSE, lty=2, color ="gray") +geom_text_repel(nudge_x =0.005) +scale_x_log10("Populations in millions (log scale)") +scale_y_log10("Total number of murders (log scale)") +ggtitle("US Gun Murders in 2010") +scale_color_discrete(name="Region") +theme_bw()
`geom_smooth()` using formula = 'y ~ x'
Warning: The following aesthetics were dropped during statistical transformation: label.
ℹ This can happen when ggplot fails to infer the correct grouping structure in
the data.
ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
variable into a factor?
If you want to show different line slopes for each region
murders |>ggplot(aes(x = population/10^6, y = total, label = abb)) +geom_point(aes(color = region), size =2) +geom_smooth(aes(color = region), method ="lm", se =FALSE, lty=2) +geom_text_repel(nudge_x =0.005) +scale_x_log10("Populations in millions (log scale)") +scale_y_log10("Total number of murders (log scale)") +ggtitle("US Gun Murders in 2010") +scale_color_discrete(name="Region") +theme_bw()
`geom_smooth()` using formula = 'y ~ x'
Warning: The following aesthetics were dropped during statistical transformation: label.
ℹ This can happen when ggplot fails to infer the correct grouping structure in
the data.
ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
variable into a factor?
Gapminder Dataset
This dataset includes health and income outcomes for 184 countries from 1960 to 2016. It also includes two character vectors, OECD and OPEC, with the names of OECD and OPEC countries from 2016.
Name the regions: The West, East Asia, Latin America, Sub-Saharan Africa, and Others
data("gapminder")
library(tidyverse)# create a list of "western" countries to be used in the following code with case_whenwest <-c("Western Europe","Northern Europe","Southern Europe","Northern America","Australia and New Zealand")
gapminder1 <- gapminder |>mutate(group =case_when( region %in% west ~"The West", region %in%c("Eastern Asia", "South-Eastern Asia") ~"East Asia", region %in%c("Caribbean", "Central America", "South America") ~"Latin America", continent =="Africa"& region !="Northern Africa"~"Sub-Saharan Africa",TRUE~"Others"))
Remove all na values from “group”, “fertility”, and “life_expectancy” using !is.na (works the same as na.rm = TRUE) please do not ever use na.omit()
mutate the population to be a value per million
change the theme of the plot
Use the command: geom_text(aes(x=7, y=82, label=year), cex=12, color=“grey”) to label the two plots at the top right inside the plots (by their years).
The next dataset contains yearly counts for Hepatitis A, measles, mumps, pertussis, polio, rubella, and smallpox for US states. *Original data courtesy of Tycho Project**. Use it to show ways one can plot more than 2 dimensions.
Focus on just measles filter out Alaska and Hawaii
mutate the rate of measles by taking the count/(population10,00052)/weeks_reporting
draw a vertical line for 1963, which is when the measles vaccination was developed
[1] Hepatitis A Measles Mumps Pertussis Polio Rubella
[7] Smallpox
Levels: Hepatitis A Measles Mumps Pertussis Polio Rubella Smallpox
# Focus only on the "Measles" diseaseus_contagious_diseases |>filter(!state%in%c("Hawaii","Alaska") & disease =="Measles") |>mutate(rate = count / population *10000/(weeks_reporting/52)) |>mutate(state =reorder(state, rate)) |>ggplot(aes(year, state, fill = rate)) +geom_tile(color ="grey50") +scale_x_continuous(expand=c(0,0)) +scale_fill_gradientn(colors =brewer.pal(9, "Reds"), trans ="sqrt") +geom_vline(xintercept=1963, col ="blue") +theme_minimal() +labs(title ="State Rates of Incidents of Measles",caption ="Source: Tycho Project",x ="", # remove x axis labely ="") # remove y axis label
Fivethirtyeight 2016 Poll Data
This data includes poll results from the US 2016 presidential elections aggregated from HuffPost Pollster, RealClearPolitics, polling firms and news reports. The dataset also includes election results (popular vote) and electoral college votes in results_us_election_2016. Use this dataset to explore inference.
Focus on polls for Clinton and Trump after July 2016
Plot a scatterplot of the enddate to the percentage in the polls
# A tibble: 1,412 × 4
enddate pollster candidate percentage
<date> <fct> <fct> <dbl>
1 2016-11-06 ABC News/Washington Post Clinton 47
2 2016-11-07 Google Consumer Surveys Clinton 38.0
3 2016-11-06 Ipsos Clinton 42
4 2016-11-07 YouGov Clinton 45
5 2016-11-06 Gravis Marketing Clinton 47
6 2016-11-06 Fox News/Anderson Robbins Research/Shaw & Co… Clinton 48
7 2016-11-06 CBS News/New York Times Clinton 45
8 2016-11-05 NBC News/Wall Street Journal Clinton 44
9 2016-11-07 IBD/TIPP Clinton 41.2
10 2016-11-06 Monmouth University Clinton 50
# ℹ 1,402 more rows
Warning: Removed 22 rows containing non-finite outside the scale range
(`stat_smooth()`).
Warning: Removed 22 rows containing missing values or values outside the scale range
(`geom_point()`).
Working with HTML Widgets and Highcharter
Set your working directory to access your files
# load required package - scaleslibrary(scales)
Attaching package: 'scales'
The following object is masked from 'package:purrr':
discard
The following object is masked from 'package:readr':
col_factor
Make a range of simple charts using the highcharter package
Highcharter is a package within the htmlwidgets framework that connects R to the Highcharts and Highstock JavaScript visualization libraries. For more information, see https://github.com/jbkunst/highcharter/
Also check out this site: https://cran.r-project.org/web/packages/highcharter/vignettes/charting-data-frames.html
Install and load required packages
Now install and load highcharter, plus RColorBrewer, which will make it possible to use ColorBrewer color palettes.
Also load dplyr and readr for loading and processing data.
Rows: 5275 Columns: 10
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (5): iso2c, iso3c, country, region, income
dbl (5): year, gdp_percap, population, birth_rate, neonat_mortal_rate
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Make a version of the “China’s rise” chart from unit 3 assignment
The arrange step is important, because highcharter needs the data in order when drawing a time series - otherwise any line drawn through the data will follow the path of the data order, not the correct time order.
In the code above, the function highchart() creates a chart.
Clicking on the legend items allows you to remove or add series from the chart.
Highcharts works by adding data “series” to a chart, and from R you can add the variables from a data frame all in one go using the hc_add_series function.
Inside this function we define the data frame to be used, with data, the type of chart, the variables to be mapped to the x and y axes, and the variable to group the data: here this draws a separate line for each country in the data.
Go to the github site provided above for the chart types available in Highcharts.
Now let’s begin customizing the chart.
Use a ColorBrewer palette
Using RColorBrewer, we can set a palette, and then use it in highcharter.
# define color palettecols <-brewer.pal(4, "Set1")highchart() |>hc_add_series(data = big4,type ="line", hcaes(x = year,y = gdp_tn, group = country)) |>hc_colors(cols)
The first line of code sets a palette with four colors, using the “Set1” palette from ColorBrewer. This is then fed to the function hc_colors() to use those colors on the chart.
By default we have a tooltip for each series, or line, and the numbers run to many decimal places.
We can change to one tooltip for each year with “shared = TRUE”, and round all the numbers to two decimal places with pointFormat = “{point.country}: {point.gdp_tn:.2f}
`summarise()` has grouped output by 'year'. You can override using the
`.groups` argument.
Make an area chart using default options
# basic area chart, default optionshighchart () |>hc_add_series(data = regions,type ="area",hcaes(x = year,y = gdp_tn, group = region))
This is an area chart, but the areas are plotted over one another, rather than stacked.
The following code fixes that, and customizes the chart in other ways. It uses the same ColorBrewer palette, with seven colors, that we used in unit 3.
We have already encountered the main functions used here. The key changes are in the hc_plotOptions() function:
stacking = “normal” creates the stacked chart. See what happens if you use stacking = “percent”.
lineWidth and lineColor set the width and color for the lines under marker = list() the code states = list(hover = list(enabled = FALSE)) turns off the hovering effect for each marker on the chart, so that the markers no longer reappear when hovered or tapped.
In the hc_legend() function, layout = “vertical” changes the layout so that the legend items appear in a vertical column.
Food Stamps Data - Combine Two Types
food_stamps<-read_csv("food_stamps.csv")
Rows: 47 Columns: 3
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
dbl (3): year, participants, costs
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.