Skip to content

Commit

Permalink
differences for PR #524
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Sep 18, 2024
1 parent 636dd39 commit 822273a
Show file tree
Hide file tree
Showing 16 changed files with 201 additions and 20 deletions.
Empty file removed .Rhistory
Empty file.
Empty file removed .here
Empty file.
20 changes: 10 additions & 10 deletions 04-tidyr.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,16 @@ interviews %>%
# A tibble: 10 × 4
key_ID village interview_date instanceID
<dbl> <chr> <dttm> <chr>
1 52 Chirodzo 2016-11-16 00:00:00 uuid:6db55cb4-a853-4000-9555-757b7fae2bcf
2 51 Chirodzo 2016-11-16 00:00:00 uuid:18ac8e77-bdaf-47ab-85a2-e4c947c9d3ce
3 44 Chirodzo 2016-11-17 00:00:00 uuid:f9fadf44-d040-4fca-86c1-2835f79c4952
4 68 Chirodzo 2016-11-16 00:00:00 uuid:ef04b3eb-b47d-412e-9b09-4f5e08fc66f9
5 46 Chirodzo 2016-11-17 00:00:00 uuid:35f297e0-aa5d-4149-9b7b-4965004cfc37
6 55 Chirodzo 2016-11-16 00:00:00 uuid:883c0433-9891-4121-bc63-744f082c1fa0
7 199 Chirodzo 2017-06-04 00:00:00 uuid:ffc83162-ff24-4a87-8709-eff17abc0b3b
8 10 Chirodzo 2016-12-16 00:00:00 uuid:8f4e49bc-da81-4356-ae34-e0d794a23721
1 70 Chirodzo 2016-11-16 00:00:00 uuid:1feb0108-4599-4bf9-8a07-1f5e66a50a0a
2 69 Chirodzo 2016-11-16 00:00:00 uuid:f86933a5-12b8-4427-b821-43c5b039401d
3 43 Chirodzo 2016-11-17 00:00:00 uuid:b4dff49f-ef27-40e5-a9d1-acf287b47358
4 50 Chirodzo 2016-11-16 00:00:00 uuid:4267c33c-53a7-46d9-8bd6-b96f58a4f92c
5 35 Chirodzo 2016-11-17 00:00:00 uuid:ff7496e7-984a-47d3-a8a1-13618b5683ce
6 9 Chirodzo 2016-11-16 00:00:00 uuid:846103d2-b1db-4055-b502-9cd510bb7b37
7 192 Chirodzo 2017-06-03 00:00:00 uuid:f94409a6-e461-4e4c-a6fb-0072d3d58b00
8 47 Chirodzo 2016-11-17 00:00:00 uuid:2d0b1936-4f82-4ec3-a3b5-7c3c8cd6cc2b
9 127 Chirodzo 2016-11-16 00:00:00 uuid:f6d04b41-b539-4e00-868a-0f62b427587d
10 54 Chirodzo 2016-11-16 00:00:00 uuid:273ab27f-9be3-4f3b-83c9-d3e1592de919
10 64 Chirodzo 2016-11-16 00:00:00 uuid:28cfd718-bf62-4d90-8100-55fafbe45d06
```

We notice that the layout or format of the `interviews` data is in a format that
Expand Down Expand Up @@ -524,7 +524,7 @@ this data frame to our `data_output` directory.


``` r
write_csv (interviews_plotting, file = "data_output/interviews_plotting.csv")
write_csv(interviews_plotting, file = "data_output/interviews_plotting.csv")
```


Expand Down
31 changes: 26 additions & 5 deletions 05-ggplot2.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@ source: Rmd

:::: instructor

- This lesson is a broad overview of ggplot2 and focuses on (1) getting familiar
with the layering system of ggplot2, (2) using the argument `group` in the
`aes()` function, (3) basic customization of the plots.
- This episode is a broad overview of ggplot2 and focuses on (1) getting
familiar with the layering system of ggplot2, (2) using the argument `group`
in the `aes()` function, (3) basic customization of the plots.
- The episode depends on data created in the Data Wrangling with tidyr
episode. If you did not get to or through all of the tidyr episode,
you can have the learners access the data by either downloading it or
quickly creating it using the tidyr code below. You will probably want to
copy the code into the Etherpad.
- If you did skip the tidyr episode, you might want to go over the exporting
data section in that episode.

::::::::::::

Expand Down Expand Up @@ -63,11 +70,23 @@ dttm (1): interview_date
```

If you were unable to complete the previous lesson or did not save the data,
then you can create it now.
then you can create it now. Either download it using `read_csv()` (Option 1)
or create it with the **dplyr** and **tidyr** code (Option 2).

::: tab

### Option 1: Download the data


``` r
interviews_plotting <- read_csv("https://raw.githubusercontent.com/datacarpentry/r-socialsci/main/episodes/data/interviews_plotting.csv")
```

### Option 2: Create the data


``` r
## Not run, but can be used to load in data from previous lesson!
## Can be used to load in data from previous lesson!
interviews_plotting <- interviews %>%
## pivot wider by items_owned
separate_rows(items_owned, sep = ";") %>%
Expand All @@ -88,6 +107,8 @@ interviews_plotting <- interviews %>%
mutate(number_items = rowSums(select(., bicycle:car)))
```

:::

## Plotting with **`ggplot2`**

**`ggplot2`** is a plotting package that makes it simple to create complex plots
Expand Down
34 changes: 31 additions & 3 deletions data/download_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,44 @@ if (!dir.exists("data"))
if (! file.exists("data/SAFI_clean.csv")) {
download.file("https://ndownloader.figshare.com/files/11492171",
"data/SAFI_clean.csv", mode = "wb")

# Clean data
df <- read.csv("data/SAFI_clean.csv",
stringsAsFactors = FALSE)

# Remove white space
df$respondent_wall_type <- trimws(df$respondent_wall_type, which = "both")
# Replace duplicate ids
df[[2, 1]] <- 2
df[[53, 1]] <- 53

write.csv(df, "data/SAFI_clean.csv", row.names = FALSE)
}

# Plotting data -----------------------------------------------------------

# Create plotting data for ggplot episode
library(tidyr)
library(dplyr)

if (! file.exists("data/interviews_plotting.csv")) {
# Copy code from ggplot episode to create data
interviews_plotting <- df %>%
separate_rows(items_owned, sep = ";") %>%
replace_na(list(items_owned = "no_listed_items")) %>%
mutate(items_owned_logical = TRUE) %>%
pivot_wider(names_from = items_owned,
values_from = items_owned_logical,
values_fill = list(items_owned_logical = FALSE)) %>%
separate_rows(months_lack_food, sep = ";") %>%
mutate(months_lack_food_logical = TRUE) %>%
pivot_wider(names_from = months_lack_food,
values_from = months_lack_food_logical,
values_fill = list(months_lack_food_logical = FALSE)) %>%
mutate(number_months_lack_food = rowSums(select(., Jan:May))) %>%
mutate(number_items = rowSums(select(., bicycle:car)))

write.csv(interviews_plotting, "data/interviews_plotting.csv", row.names = FALSE)
}


Loading

0 comments on commit 822273a

Please sign in to comment.