Skip to content

Commit

Permalink
[fix datacarpentry#60] name all plot chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
fmichonneau committed May 18, 2018
1 parent 8828c5e commit 4023e0e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 26 deletions.
7 changes: 4 additions & 3 deletions _episodes_rmd/02-starting-with-data.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ Let's extract the `memb_assoc` column from our data frame, convert it into a
factor, and use it to look at the number of interview respondents who were or
were not members of an irrigation association:

```{r, purl=TRUE}
```{r factor-plot-default-order, purl=TRUE}
## create a vector from the data frame column "memb_assoc"
memb_assoc <- interviews$memb_assoc
## convert it into a factor
Expand All @@ -344,7 +344,7 @@ been recorded, and encoded as missing data. They do not appear on the plot.
Let's encode them differently so they can counted and visualized in our plot.


```{r, purl=TRUE}
```{r factor-plot-reorder, purl=TRUE}
## Let's recreate the vector from the data frame column "memb_assoc"
memb_assoc <- interviews$memb_assoc
## replace the missing data with "undetermined"
Expand All @@ -368,7 +368,8 @@ plot(memb_assoc)
> recreate the barplot such that "Undetermined" is last (after "Yes")?
>
> > ## Solution
> > ```{r}
> >
> > ```{r factor-plot-exercise}
> > levels(memb_assoc) <- c("No", "Undetermined", "Yes")
> > memb_assoc <- factor(memb_assoc, levels = c("No", "Yes", "Undetermined"))
> > plot(memb_assoc)
Expand Down
46 changes: 23 additions & 23 deletions _episodes_rmd/04-ggplot2.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ ggplot(data = <DATA>, mapping = aes(<MAPPINGS>)) + <GEOM_FUNCTION>()
- use the `ggplot()` function and bind the plot to a specific data frame using
the `data` argument

```{r, eval=FALSE, purl=FALSE}
```{r ggplot-steps-1, eval=FALSE, purl=FALSE}
ggplot(data = interviews_plotting)
```

- define a mapping (using the aesthetic (`aes`) function), by selecting the variables to be plotted and specifying how to present them in the graph, e.g. as x/y positions or characteristics such as size, shape, color, etc.

```{r, eval=FALSE, purl=FALSE}
```{r ggplot-steps-2, eval=FALSE, purl=FALSE}
ggplot(data = interviews_plotting, aes(x = no_membrs, y = number_items))
```

Expand All @@ -91,15 +91,15 @@ let's use `geom_point()` first:

```{r first-ggplot, purl=FALSE}
ggplot(data = interviews_plotting, aes(x = no_membrs, y = number_items)) +
geom_point()
geom_point()
```

The `+` in the **`ggplot2`** package is particularly useful because it allows
you to modify existing `ggplot` objects. This means you can easily set up plot
templates and conveniently explore different types of plots, so the above plot
can also be generated with code like this:

```{r, first-ggplot-with-plus, eval=FALSE, purl=FALSE}
```{r first-ggplot-with-plus, eval=FALSE, purl=FALSE}
# Assign plot to a variable
interviews_plot <- ggplot(data = interviews_plotting, aes(x = no_membrs, y = number_items))
Expand All @@ -121,7 +121,7 @@ interviews_plot +
> the new layer and will return an error message.
{: .callout}

```{r, ggplot-with-plus-position, eval=FALSE, purl=FALSE}
```{r ggplot-with-plus-position, eval=FALSE, purl=FALSE}
## This is the correct syntax for adding layers
interviews_plot +
geom_point()
Expand Down Expand Up @@ -231,7 +231,7 @@ hidden?
>
> > ## Solution
> >
> > ```{r}
> > ```{r violin-plot}
> > ggplot(data = interviews_plotting, aes(x = respondent_wall_type, y = rooms)) +
> > geom_violin(alpha = 0) +
> > geom_jitter(alpha = 0.5, color = "tomato")
Expand All @@ -246,7 +246,7 @@ hidden?
> layer on a jitter layer to show actual measurements.
>
> > ## Solution
> > ```{r}
> > ```{r boxplot-exercise}
> > ggplot(data = interviews_plotting, aes(x = respondent_wall_type, y = liv_count)) +
> > geom_boxplot(alpha = 0) +
> > geom_jitter(alpha = 0.5)
Expand All @@ -257,7 +257,7 @@ hidden?
> respondent is a member of an irrigation association (`memb_assoc`).
>
> > ## Solution
> > ```{r}
> > ```{r boxplot-exercise-factor}
> > ggplot(data = interviews_plotting, aes(x = respondent_wall_type, y = liv_count)) +
> > geom_boxplot(alpha = 0) +
> > geom_jitter(aes(alpha = 0.5, color = memb_assoc))
Expand All @@ -271,15 +271,15 @@ Barplots are also useful for visualizing categorical data. By default,
`geom_bar` accepts a variable for x, and plots the number of instances each
value of x (in this case, wall type) appears in the dataset.
```{r}
```{r barplot-1}
ggplot(data = interviews_plotting, aes(x = respondent_wall_type)) +
geom_bar()
```
We can use the `fill` asthetic for the `geom_bar()` geom to color bars by
the portion of each count that is from each village.

```{r}
```{r barplot-stack}
ggplot(data = interviews_plotting, aes(x = respondent_wall_type)) +
geom_bar(aes(fill = village))
```
Expand All @@ -290,7 +290,7 @@ correspond to each village and put them side-by-side by using the `position`
argument for `geom_bar()` and setting it to "dodge".


```{r}
```{r barplot-dodge}
ggplot(data = interviews_plotting, aes(x = respondent_wall_type)) +
geom_bar(aes(fill = village), position = "dodge")
```
Expand All @@ -304,7 +304,7 @@ representing the percent of each house type in each village. We will remove
houses with cement walls, as there was only one in the dataset.


```{r}
```{r wall-type-data}
percent_wall_type <- interviews_plotting %>%
filter(respondent_wall_type != "cement") %>%
count(village, respondent_wall_type) %>%
Expand All @@ -314,7 +314,7 @@ percent_wall_type <- interviews_plotting %>%
Now we can use this new data frame to create our plot showing the
percentage of each house type in each village.

```{r}
```{r barplot-wall-type}
ggplot(percent_wall_type, aes(x = village, y = percent, fill = respondent_wall_type)) +
geom_bar(stat = "identity", position = "dodge")
```
Expand All @@ -329,7 +329,7 @@ ggplot(percent_wall_type, aes(x = village, y = percent, fill = respondent_wall_t
>
> > ## Solution
> >
> > ```{r}
> > ```{r barplot-memb-assoc}
> > percent_memb_assoc <- interviews_plotting %>%
> > filter(!is.na(memb_assoc)) %>%
> > count(village, memb_assoc) %>%
Expand All @@ -353,7 +353,7 @@ relatively few lines of code. We will add more informative x and y axis
labels to our plot of proportion of house type by village and also add
a title.
```{r}
```{r barplot-wall-types-labelled}
ggplot(percent_wall_type, aes(x = village, y = percent, fill = respondent_wall_type)) +
geom_bar(stat = "identity", position = "dodge") +
ylab("Percent") +
Expand All @@ -374,7 +374,7 @@ plot into multiple plots based on a factor included in the dataset. We
will use it to split our barplot of housing type proportion by village
so that each village has it's own panel in a multi-panel plot:

```{r}
```{r barplot-faceting}
ggplot(percent_wall_type, aes(x = respondent_wall_type, y = percent)) +
geom_bar(stat = "identity", position = "dodge") +
ylab("Percent") +
Expand All @@ -390,7 +390,7 @@ Usually plots with white background look more readable when printed. We can set
the background to white using the function `theme_bw()`. Additionally, you can remove
the grid:

```{r, purl=FALSE}
```{r barplot-theme-bw, purl=FALSE}
ggplot(percent_wall_type, aes(x = respondent_wall_type, y = percent)) +
geom_bar(stat = "identity", position = "dodge") +
ylab("Percent") +
Expand All @@ -407,7 +407,7 @@ in each village who own each item and then create a faceted series of
bar plots where each plot is a particular item. First we need to
calculate the percentage of people in each village who own each item:

```{r}
```{r percent-items-data}
percent_items <- interviews_plotting %>%
count(items_owned, village) %>%
## add a column with the number of people in each village
Expand All @@ -425,7 +425,7 @@ denominator in our percentage calculation. Instead, we need to specify the
number of respondents in each village. Using this data frame, we can now create
a multi-paneled bar plot.

```{r}
```{r percent-items-barplot}
ggplot(percent_items, aes(x = village, y = percent)) +
geom_bar(stat = "identity", position = "dodge") +
facet_wrap(~ items_owned) +
Expand Down Expand Up @@ -464,7 +464,7 @@ and think of ways you could improve the plot.
Now, let's change names of axes to something more informative than 'village' and
'percent' and add a title to the figure:

```{r, purl=FALSE}
```{r ggplot-customization, purl=FALSE}
ggplot(percent_items, aes(x = village, y = percent)) +
geom_bar(stat = "identity", position = "dodge") +
facet_wrap(~ items_owned) +
Expand All @@ -477,7 +477,7 @@ ggplot(percent_items, aes(x = village, y = percent)) +
The axes have more informative names, but their readability can be improved by
increasing the font size:

```{r number-species-year-with-right-labels-xfont-size, purl=FALSE}
```{r ggplot-customization-font-size, purl=FALSE}
ggplot(percent_items, aes(x = village, y = percent)) +
geom_bar(stat = "identity", position = "dodge") +
facet_wrap(~ items_owned) +
Expand All @@ -499,7 +499,7 @@ them vertically and horizontally so they don't overlap. You can use a 90-degree
angle, or experiment to find the appropriate angle for diagonally oriented
labels:

```{r number-species-year-with-theme, purl=FALSE}
```{r ggplot-customization-label-orientation, purl=FALSE}
ggplot(percent_items, aes(x = village, y = percent)) +
geom_bar(stat = "identity", position = "dodge") +
facet_wrap(~ items_owned) +
Expand All @@ -516,7 +516,7 @@ If you like the changes you created better than the default theme, you can save
them as an object to be able to easily apply them to other plots you may create:


```{r number-species-year-with-right-labels-xfont-orientation, purl=FALSE}
```{r ggplot-custom-themes, purl=FALSE}
grey_theme <- theme(axis.text.x = element_text(colour = "grey20", size = 12, angle = 45, hjust = 0.5, vjust = 0.5),
axis.text.y = element_text(colour = "grey20", size = 12),
text = element_text(size = 16))
Expand Down

0 comments on commit 4023e0e

Please sign in to comment.