April 23, 2024
<-
is the assignment operator
#
is the comment operator
library()
and name of library
library(readr)
install.packages("readr")
Use glimpse()
to see the columns and data types:
# load libraries
library(readr)
library(dplyr)
dem_summary <- read_csv("data/dem_summary.csv")
glimpse(dem_summary)
Rows: 6
Columns: 5
$ region <chr> "The West", "Latin America", "Eastern Europe", "Asia", "Afri…
$ polyarchy <dbl> 0.8709230, 0.6371358, 0.5387451, 0.4076602, 0.3934166, 0.245…
$ gdp_pc <dbl> 37.913054, 9.610284, 12.176554, 9.746391, 4.410484, 21.134319
$ flfp <dbl> 52.99082, 48.12645, 50.45894, 50.32171, 56.69530, 26.57872
$ women_rep <dbl> 28.12921, 21.32548, 17.99728, 14.45225, 17.44296, 10.21568
Or use View()
or click on the name of the object in your Environment tab to see the data in a spreadsheet:
05:00
Let’s start with the first two, the data and the aesthetic…
This gives us the axes without any visualization:
Now let’s add a geom. In this case we want a bar chart so we add geom_col()
.
That gets the idea across but looks a little depressing, so…
…let’s change the color of the bars by specifying fill = "steelblue"
.
Note how color of original bars is simply overwritten:
Now let’s add some labels with the labs()
function:
And that gives us…
Next, we reorder the bars with fct_reorder()
from the forcats
package.
Note that we could also use the base R reorder()
function here.
This way, we get a nice, visually appealing ordering of the bars according to levels of democracy…
Now let’s change the theme to theme_minimal()
.
Gives us a clean, elegant look.
Note that you can also save your plot as an object to modify later.
Which gives us…
Now let’s add back our labels…
So now we have…
And now we’ll add back our theme…
Voila!
Change the theme. There are many themes to choose from.
glimpse()
the data10:00
# load data
dem_women <- read_csv("data/dem_women.csv")
# filter to 2022
dem_women_2022 <- dem_women |>
filter(year == 2022)
# create histogram
ggplot(dem_women_2022, aes(x = flfp)) +
geom_histogram(fill = "steelblue") +
labs(
x = "Percentage of Working Aged Women in Labor Force",
y = "Number of Countries",
title = "Female labor force participation rates, 2022",
caption = "Source: World Bank"
) + theme_minimal()
Change number of bins (bars) using bins
or binwidth
arguments (default number of bins = 30):
At 50 bins…
At 100 bins…probably too many!
Using binwidth
instead of bins
…
Setting binwidth
to 2…
Which gives us…
x =
in aes()
geom_histogram
10:00