get_chapters.Rd
Loads chapters into memory from a DataCamp Repository
get_chapters(path)
path | - path to root path of DataCamp Repository |
---|
named list with each chapter.md file in a separate slot.
chapter_file_path <- system.file("extdata/", package="decampr") chapter_list <- get_chapters(chapter_file_path) chapter_list[[1]]#> [1] "---\ntitle : The Magic of ggplot2\ndescription : Learn how ggplot2 turns variables into statistical graphics\n---\n\n--- type:NormalExercise lang:r xp:100 skills:1 key:8323fcbca1\n## Quick Data Frame Introduction\n\nBefore we start: please join the [RBootcamp OHSU Group!](https://www.datacamp.com/groups/de163cc541d0d9bde4956157d17dbedfb1149225/invite)\n\nA `data.frame` is basically a table-like format which has the following properties: \n\n- Columns can each have a different type (`numeric`, `character`, `boolean`, `factor`)\n- Columns are called \"variables\"\n- Rows correspond to a single observation (ideally)\n- Can be subset or filtered based on criteria\n\nIndividual variables within a `data.frame` can be accessed with the `$` operator (such as `gap1992$pop`). We won't use this very often, as the `tidyverse` lets us access the variables without it, as you'll see.\n\n*** =instructions\nRun `colnames()` and `head()` on the `gap1992` data to see what's in each column. Then see how many rows there are in the dataset using `nrow()`. Run these in console before you submit your answer.\n\n*** =pre_exercise_code\n```{r}\nlibrary(dplyr)\nlibrary(gapminder)\nlibrary(ggplot2)\ngap1992 <- gapminder %>% filter(year == 1992)\n```\n\n*** =sample_code\n```{r}\n##run head on gap1992\nhead(----)\n##run colnames here on gap1992\ncolnames(----)\n##run nrow() on gap1992\nnrow(-----)\n```\n\n*** =solution\n```{r}\n##run head on gap1992\nhead(gap1992)\n##run colnames here on gap1992\ncolnames(gap1992)\n##run nrow() on gap1992\nnrow(gap1992)\n```\n\n*** =sct\n```{r}\nsuccess_msg(\"Great! You learned some basics about `data.frame`s! Let's move on.\")\ntest_function(\"colnames\", incorrect_msg = \"did you use colnames(gap1992)?\")\ntest_function(\"nrow\", incorrect_msg = \"did you use nrow(gap1992)\")\n```\n\n\n--- type:MultipleChoiceExercise lang:r xp:100 skills:1 key:d599f92ec8\n## Thinking about aesthetics\nNow that we've learned a little about the `data.frame`, we can get to the fun part: making graphs.\n\nThe first thing we are going to is think about how we represent variables in a plot. \n\nHow do we visually represent a variable in our dataset? Take a look at this graph. What variable is mapped to `y`, and what is mapped to `x`, and what is mapped to `color`?\n\n***=pre_exercise_code\n```{r}\nlibrary(dplyr)\nlibrary(gapminder)\nlibrary(ggplot2)\ngap1992 <- gapminder %>% filter(year == 1992)\n\nggplot(gap1992, aes(x = log(gdpPercap), y = lifeExp, size=pop, color=continent)) +\n geom_point() + ggtitle(\"Gapminder for 1992\")\n```\n\n*** =instructions\n- x = gdpPercap, y = log(lifeExp), color = continent\n- x = continent, y = year, color = pop\n- y = lifeExp, x = log(gdpPercap), color = continent\n\n*** =hint\nLook at the y-axis.\n\n*** =sct\n```{r}\nmsg1 = \"You have things reversed, and you're taking the log of the wrong variable\"\nmsg2 = \"Wrong variables. Go back and look at what's being mapped\"\nmsg3 = \"Correct! We are displaying lifeExp as our y variable and log(gdpPercap) as our x variable\"\n\ntest_mc(correct = 3, feedback_msgs=c(msg1, msg2, msg3))\n```\n\n\n--- type:NormalExercise lang:r xp:100 skills:1 key:bfe1375688\n## Mapping variables to produce geometric plots\n\nA statistical graphic consists of:\n\n+ A `mapping` of variables in `data` to\n+ `aes()`thetic attributes of\n+ `geom_`etric objects.\n\nIn code, this is translated as:\n\n```{r}\nggplot(data = gap1992, mapping = aes(x = log(gdpPercap), y=log(pop))) +\n geom_point()\n```\n\nLet's take the above example code apart. A `ggplot2` call always starts with the `ggplot()` function. In this function, we need two things:\n\n1. `data` - in this case, `gap1992`.\n2. `mapping` - An aesthetic mapping, using the `aes()` function. \n\nIn order to map our variables to aesthetic properties, we will need to use `aes()`, which is our `aes()`thetic mapping function. In our example, we map `x` to `log(gdpPercap)` and `y` to `log(pop)`.\n\nFinally, we can superimpose our geometry on the plot using `geom_point()`.\n\n*** =instructions\nBased on the graph, map the appropriate variables to the `x`, and `y` aesthetics. Run your plot. Remember, you can try plots out in the console before you submit your answer.\n\n*** =hint\nLook at the graph. If you need the variable names, you can always use `head()` or `colnames()` on the `gap1992` dataset.\n\n*** =pre_exercise_code\n```{r}\nlibrary(dplyr)\nlibrary(gapminder)\nlibrary(ggplot2)\ngap1992 <- gapminder %>% filter(year == 1992)\n\nggplot(gap1992, aes(x = log(gdpPercap), y = lifeExp, size=pop, color=continent)) +\n geom_point() + ggtitle(\"Gapminder for 1992\")\n```\n\n*** =sample_code\n```{r}\nggplot(data = gap1992, \n mapping = aes(\n x = , \n y = \n )) + \ngeom_point()\n```\n\n*** =solution\n```{r}\nggplot(data=gap1992, \n mapping = aes(\n x = log(gdpPercap), \n y = lifeExp \n )) + \ngeom_point()\n\n```\n\n*** =sct\n```{r}\nsuccess_msg(\"Wunderbar! Now you're on your way to recreating that gapminder plot\")\ntest_ggplot(check_aes = TRUE, aes_fail_msg = \"Not quite. Make sure you're mapping the right variables to the right aesthetics.\")\n```\n\n\n--- type:MultipleChoiceExercise lang:r xp:50 skills:1 key:e507076f4e\n## More about aes\nFor `geom_point()`, there are lots of other aesthetics. The important thing to know is that\naesthetics are properties of the `geom`. If you need to know the aesthetics that you can \nmap to a `geom`, you can always use `help()` (such as `help(geom_point)`).\n\nI'd ask you to look at `help(geom_point)`, but the documentation is not correct on Datacamp. \nInstead, look here: [http://ggplot.yhathq.com/docs/geom_point.html](http://ggplot.yhathq.com/docs/geom_point.html)\nand look at all the aesthetic mappings. \n\nWhich of the following is *not* a mappable aesthetic to `geom_point()`?\n\n*** =instructions\n- `x`\n- `shape`\n- `linetype`\n\n*** =pre_exercise_code\n```{r}\nlibrary(ggplot2)\n```\n\n*** =sct\n```{r}\nsuccess_msg(\"Great! Now you know where to look for mappable aesthetics.\")\nmsg1 = \"Nope. This is a mappable aesthetic to `geom_point().`\"\nmsg3 = \"Correct. `linetype` is not mappable to `geom_point()`. Points don't have a `linetype`, do they?\"\ntest_mc(correct = 3, feedback_msgs=c(msg1, msg1, msg3))\n```\n\n\n--- type:NormalExercise lang:r xp:100 skills:1 key:f0a09d682e\n## Points versus lines\n\nThe great thing about `ggplot2` is that it's easy to swap representations. \nInstead of x-y points, we can plot the data as a line graph by swapping `geom_line()`\nfor `geom_point()`.\n\n*** =instructions\nFirst run the code to see the plot with points. Change the `geom_point()` in the following graph to `geom_line()`. What happened?\nHow did the visual presentation of the data change?\n\n*** =pre_exercise_code\n```{r}\nlibrary(dplyr)\nlibrary(gapminder)\nlibrary(ggplot2)\ngap1992 <- gapminder %>% filter(year == 1992)\n```\n\n*** =sample_code\n```{r}\nggplot(gap1992, aes(x = log(gdpPercap), y = lifeExp, color=continent)) +\n geom_point() \n```\n\n*** =solution\n```{r}\nggplot(gap1992, aes(x = log(gdpPercap), y = lifeExp, color=continent)) +\n geom_line() \n```\n\n*** =sct\n```{r}\nsuccess_msg(\"Great! Now you know how to swap representations in ggplot2. Let's move on.\")\ntest_function(\"geom_line\", incorrect_msg=\"You need to change the geom.\")\n```\n\n\n--- type:NormalExercise lang:r xp:100 skills:1 key:13ea4fbf3d\n## Geoms are layers on a ggplot\n\nWe are not restricted to a single geom on a graph! You can think of geoms\nas layers on a graph. Thus, we can use the `+` symbol to add geoms to our\nbase `ggplot()` statement. \n\n*** =instructions\nAdd both `geom_line()` and `geom_point()` to the following ggplot. Are the results what you expected?\n\n*** =pre_exercise_code\n```{r}\nlibrary(dplyr)\nlibrary(gapminder)\nlibrary(ggplot2)\ngap1992 <- gapminder %>% filter(year == 1992)\n```\n\n*** =sample_code\n```{r}\nggplot(gap1992, aes(x = log(gdpPercap), y = lifeExp, color=continent)) +\n## add code here\n\n```\n\n*** =solution\n```{r}\nggplot(gap1992, aes(x = log(gdpPercap), y = lifeExp, color=continent)) +\n## add code here\n geom_line() + geom_point()\n```\n\n*** =sct\n```{r}\ntest_function(\"geom_line\", incorrect_msg=\"you need to add geom_line()\")\ntest_function(\"geom_point\", incorrect_msg=\"you need to add geom_point()\")\n```\n\n\n--- type:MultipleChoiceExercise lang:r xp:100 skills:1 key:349a622cb7\n## Quick review about ggplot2\n\nWhat does the `+` in a `ggplot` statement do? \n\nFor example:\n\n```{r}\nggplot(gap1992, aes(x = log(gdpPercap), y = lifeExp, color=continent)) +\n geom_line() + geom_point()\n```\n\n*** =instructions\n- adds one `data.frame` to another `data.frame` \n- allows you to chain data and geoms together into a single statistical graphic\n- allows you to add variables together in a `data.frame`\n\n*** =hint\n`+` combines things, but doesn't add them together\n\n*** =sct\n```{r}\nmsg1 = \"This is not the case. Go back and look at the ggplot code.\"\nmsg2 = \"Correct! This is how we can add data and layer geoms together\"\nmsg3 = \"Look at the ggplot code and see if we are manipulating data or not. Are we?\"\ntest_mc(correct = 2, feedback_msgs=c(msg1, msg2, msg3))\n```\n\n\n--- type:NormalExercise lang:r xp:300 skills:1 key:01ef5c54c5\n## Final Challenge: Recreate this Gapminder Plot\n\nYour final challenge is to completely recreate this graph using the `gap1992` data.\n\n***=pre_exercise_code\n```{r}\nlibrary(dplyr)\nlibrary(gapminder)\nlibrary(ggplot2)\ngap1992 <- gapminder %>% filter(year == 1992)\n\nggplot(gap1992, aes(x = log(gdpPercap), y = lifeExp, size=pop, color=continent)) +\n geom_point() + ggtitle(\"Gapminder for 1992\")\n```\n\n*** =instructions\n- If you need to remember variable names, you can always call `head(gap1992)` or `colnames(gap1992)` in the console.\n- Recreate the above graphic by mapping the right variables to the right aesthetic elements. Remember, you can try plots out in the console before you submit your answer.\n\n*** =sample_code\n```{r}\nggplot(gap1992, aes(x = , \n y = , \n color = ,\n size =\n )) + ggtitle(\"Gapminder for 1992\") +\n```\n\n*** =solution\n```{r}\nggplot(gap1992, aes(x = log(gdpPercap), \n y = lifeExp, \n color = continent,\n size = pop\n )) + ggtitle(\"Gapminder for 1992\") + \n geom_point()\n```\n\n*** =sct\n```{r}\nsuccess_msg(\"Now you know the basics of ggplot and aesthetics. Congrats!\")\ntest_ggplot(check_aes=TRUE, aes_fail_msg = \"Not quite. Go back and map the variables to the correct aesthetics.\")\n```\n\n\n--- type:NormalExercise lang:r xp:0 skills:1 key:fe7e851b1f\n## What you learned in this chapter\n\n- Basic `ggplot2` syntax.\n- Plotting x-y data using `ggplot2` using both `geom_point()` and `geom_bar()`.\n- Mapping variables in a dataset to visual properties using `aes()`\n- `geom`s correspond to layers in a graph.\n- That `ggplot2` can make some pretty cool graphs\n- That you can do this!\n\n*** =instructions\nJust move on to the next chapter! (CTRL+K)\n\n*** =hint\n\n*** =pre_exercise_code\n```{r}\n\n```\n\n*** =sample_code\n```{r}\n\n```\n\n*** =solution\n```{r}\n\n```\n\n*** =sct\n```{r}\n\n```\n\n\n--- \n"