Skip to content

Commit

Permalink
fix ggplot page
Browse files Browse the repository at this point in the history
small typos
  • Loading branch information
nsbatra committed May 12, 2021
1 parent c1a7e5f commit 971545f
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions new_pages/ggplot_basics.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ These plot object aesthetics can be assigned values in two ways:

If you want the plot object aesthetic to be static, that is - to be the same for every observation in the data, you write its assignment within the geom but *outside* of any `mapping = aes()` statement. These assignments could look like `size = 1` or `color = "blue"`. Here are two examples:

* In the first example, the `mapping = aes()` is in the `ggplot()` command and the axes are mapping to age and weight columns in the data. The plot aesthetics `color = `, `size = `, and `alpha = ` (transparency) are assigned to static values. For clarity, this is done in the `geom_point()` function, as you may add other geoms afterward that would take different values for their plot aesthetics.
* In the first example, the `mapping = aes()` is in the `ggplot()` command and the axes are mapped to age and weight columns in the data. The plot aesthetics `color = `, `size = `, and `alpha = ` (transparency) are assigned to static values. For clarity, this is done in the `geom_point()` function, as you may add other geoms afterward that would take different values for their plot aesthetics.
* In the second example, the histogram requires only the x-axis mapped to a column. The histogram `binwidth = `, `color = `, `fill = ` (internal color), and `alpha = ` are again set within the geom to static values.

```{r, out.width=c('50%', '50%'), fig.show='hold', warning=F, message=F}
Expand All @@ -262,7 +262,7 @@ To achieve this, you map that plot aesthetic to a *column name* (not in quotes).

Two examples are below.

* In the first example, the `color = ` aesthetic (of each point) is mapped to te column `age` - and a scale has appeared in a legend! For now just note that the scale exists - we will show how to modify it in later sections.
* In the first example, the `color = ` aesthetic (of each point) is mapped to the column `age` - and a scale has appeared in a legend! For now just note that the scale exists - we will show how to modify it in later sections.
* In the second example two new plot aesthetics are also mapped to columns (`color = ` and `size = `), while the plot aesthetics `shape = ` and `alpha = ` are mapped to static values outside of any `mapping = aes()` function.

```{r, out.width=c('50%', '50%'), fig.show='hold', warning=F, message=F}
Expand Down Expand Up @@ -374,7 +374,7 @@ Note that depending on the geom, you will need to use different arguments to gro

For your information - the most basic way of grouping the data is by using only the `group = ` argument within `mapping = aes()`. However, this by itself will not change the colors, fill, or shapes. Nor will it create a legend. Yet the data are grouped, so statistical displays may be affected.

To adjust the order of groups in a plot, see the [ggplot tips] page or the page on [Working with factors]. There are many examples of grouped plots in the pages on plotting continuous and categorical data.
To adjust the order of groups in a plot, see the [ggplot tips] page or the page on [Factors]. There are many examples of grouped plots in the sections below on plotting continuous and categorical data.



Expand All @@ -384,7 +384,7 @@ Facets, or "small-multiples", are used to split one plot into a multi-panel figu

Faceting is a functionality that comes with **ggplot2**, so the legends and axes of the facet "panels" are automatically aligned. There are other packages discussed in the [ggplot tips] page that are used to combine completely different plots (**cowplot** and **patchwork**) into one figure.

Faceting is done with one the fullowing **ggplot2** functions:
Faceting is done with one of the following **ggplot2** functions:

1. `facet_wrap()` To show a different panel for each level of a *single* variable. One example of this could be showing a different epidemic curve for each hospital in a region. Facets are ordered alphabetically, unless the variable is a factor with other ordering defined.
+ You can invoke certain options to determine the layout of the facets, e.g. `nrow = 1` or `ncol = 1` to control the number of rows or columns that the faceted plots are arranged within.
Expand All @@ -398,7 +398,7 @@ Each of these functions accept a formula syntax to specify the column(s) for fac

* For `facet_grid()` you can also specify one or two columns to the formula (grid `rows ~ columns`). If you only want to specify one, you can place a period `.` on the other side of the tilde like `facet_grid(. ~ hospital)` or `facet_grid(hospital ~ .)`.

Facets can quickly contain an overwhelming amount of information - its good to ensure you don't have too many levels of each variable that you choose to facet by. Here are some quick examples with the malaria dataset (see [Download book and data]) which consists of daily case counts of malaria for facilities, by age group.
Facets can quickly contain an overwhelming amount of information - its good to ensure you don't have too many levels of each variable that you choose to facet by. Here are some quick examples with the malaria dataset (see [Download handbook and data]) which consists of daily case counts of malaria for facilities, by age group.

Below we import and do some quick modifications for simplicity:

Expand Down Expand Up @@ -515,7 +515,7 @@ ggplot(malaria_data, aes(x = data_date, y = malaria_tot)) +

<!-- ``` -->

### Factor level order in facets
### Factor level order in facets {.unnumbered}

See this [post](https://juliasilge.com/blog/reorder-within/) on how to re-order factor levels *within* facets.

Expand All @@ -524,7 +524,7 @@ See this [post](https://juliasilge.com/blog/reorder-within/) on how to re-order

### Saving plots {.unnumbered}

By default when you run a `ggplot()` command, the plot will be printed to the Plots RStudio pane. However, you can also save the plot as an object by using the assignment operator `<-` and giving it a name. Then will not print unless the object name itself is run. You can also print it by wrapping the plot name with `print()`, but this is only necessary in certain circumstances such as if the plot is created inside a *for loop* used to print multiple plots at once (see [Iteration, loops, and lists] page).
By default when you run a `ggplot()` command, the plot will be printed to the Plots RStudio pane. However, you can also save the plot as an object by using the assignment operator `<-` and giving it a name. Then it will not print unless the object name itself is run. You can also print it by wrapping the plot name with `print()`, but this is only necessary in certain circumstances such as if the plot is created inside a *for loop* used to print multiple plots at once (see [Iteration, loops, and lists] page).

```{r, warning=F, message=F}
# define plot
Expand Down Expand Up @@ -561,7 +561,7 @@ You can export as png, pdf, jpeg, tiff, bmp, svg, or several other file types, b

You can also specify the arguments `width = `, `height = `, and `units = ` (either "in", "cm", or "mm"). You can also specify `dpi = ` with a number for plot resolution (e.g. 300). See the function details by entering `?ggsave` or reading the [documentation online](https://ggplot2.tidyverse.org/reference/ggsave.html).


Remember that you can use `here()` syntax to provide the desired file path. See the [Import and export] page for more information.


## Labels
Expand All @@ -573,7 +573,7 @@ Within `labs()` you can provide character strings to these arguements:
* `x = ` and `y = ` The x-axis and y-axis title (labels)
* `title = ` The main plot title
* `subtitle = ` The subtitle of the plot, in smaller text below the title
* `caption = ` The caption of the plot, or bottom-right by default
* `caption = ` The caption of the plot, in bottom-right by default

Here is a plot we made earlier, but with nicer labels:

Expand All @@ -598,7 +598,7 @@ age_by_wt

Note how in the caption assignment we used `str_glue()` from the **stringr** package to implant dynamic R code within the string text. The caption will show the "Data as of: " date that reflects the maximum hospitalization date in the linelist. Read more about this in the page on [Characters and strings].

A note on specifying the *legend* title: within `labs()`, write the plot aesthetic argument that you used to create the legend. So for the example above we assigned `color = age` to create the legend. Therefore, we provide `color = ` to `labs()` and assigned the legend title desired ("Age" with capital A). If you create the legend with `aes(fill = COLUMN)`, then in `labs()` you would write `fill = ` to adjust the title of that legend. See the section on legends in the [ggplot tips] page for more details on editing legends.
A note on specifying the *legend* title: There is no one "legend title" argument, as you could have multiple scales in your legend. Within `labs()`, you can write the argument for the plot aesthetic used to create the legend, and provide the title this way. For example, above we assigned `color = age` to create the legend. Therefore, we provide `color = ` to `labs()` and assign the legend title desired ("Age" with capital A). If you create the legend with `aes(fill = COLUMN)`, then in `labs()` you would write `fill = ` to adjust the title of that legend. The section on color scales in the [ggplot tips] page provides more details on editing legends, and an alternative approach using `scales_()` functions.



Expand All @@ -612,7 +612,7 @@ One of the best parts of **ggplot2** is the amount of control you have over the

### Complete themes {.unnumbered}

As they are quite straight-forward, we will demonstrate the complete theme functions below and will not describe them further here. Note that any micro-adjustments with `theme()` should be make *after* use of a complete theme.
As they are quite straight-forward, we will demonstrate the complete theme functions below and will not describe them further here. Note that any micro-adjustments with `theme()` should be made *after* use of a complete theme.

Write them with empty parentheses.

Expand Down Expand Up @@ -647,11 +647,11 @@ ggplot(data = linelist, mapping = aes(x = age, y = wt_kg))+
The `theme()` function can take a large number of arguments, each of which edits a very specific aspect of the plot. There is no way we could cover all of the arguments, but we will describe the general pattern for them and show you how to find the argument name that you need. The basic syntax is this:

1. Within `theme()` write the argument name for the plot element you want to edit, like `plot.title = `
3. Provide an `element_()` function to the arguemnt
+ Most often use `element_text()`, but others include `element_rect()` for canvas background colors, or `element_blank()` to remove plot elements
4. Within the `element_()` function, write arguments assignments to make the fine adjustments you desire
3. Provide an `element_()` function to the argument
+ Most often, use `element_text()`, but others include `element_rect()` for canvas background colors, or `element_blank()` to remove plot elements
4. Within the `element_()` function, write argument assignments to make the fine adjustments you desire

So, that description was quite abstract and convoluted, so here are some examples.
So, that description was quite abstract, so here are some examples.

The below plot looks quite silly, but it serves to show you a variety of the ways you can adjust your plot.

Expand Down Expand Up @@ -705,7 +705,7 @@ The **tidyverse** documentation on [modifying theme](https://ggplot2.tidyverse.o

<span style="color: darkgreen;">**_TIP:_** Run `theme_get()` from **ggplot2** to print a list of all 90+ `theme()` arguments to the console.</span>

<span style="color: darkgreen;">**_TIP:_** If you ever want to remove an element of a plot, you can also do it through `theme()`. Just pass `element_blank()` to an argument to have it disappear completely.</span>
<span style="color: darkgreen;">**_TIP:_** If you ever want to remove an element of a plot, you can also do it through `theme()`. Just pass `element_blank()` to an argument to have it disappear completely. For legends, set `legend.position = "none".`</span>



Expand Down Expand Up @@ -816,12 +816,12 @@ To get smoothed proportions, you can use `geom_density()`:
```{r, fig.show='hold', message=FALSE, warning=FALSE, out.width=c('50%', '50%')}
# Frequency with proportion axis, smoothed
ggplot(data = linelist, mapping = aes(x = age)) +
geom_density(binwidth = 2, size = 2, alpha = 0.2)+
geom_density(size = 2, alpha = 0.2)+
labs(title = "Proportional density")

# Stacked frequency with proportion axis, smoothed
ggplot(data = linelist, mapping = aes(x = age, fill = gender)) +
geom_density(binwidth = 2, size = 2, alpha = 0.2, position = "stack")+
geom_density(size = 2, alpha = 0.2, position = "stack")+
labs(title = "'Stacked' proportional densities")
```

Expand Down Expand Up @@ -852,7 +852,7 @@ ggplot(data = linelist, mapping = aes(x = age, y = after_stat(density), color =

# Frequency with proportion axis, smoothed
ggplot(data = linelist, mapping = aes(x = age, y = after_stat(density), fill = gender)) +
geom_density(binwidth = 2, size = 2, alpha = 0.2)+
geom_density(size = 2, alpha = 0.2)+
labs(title = "Proportional, smoothed with geom_density()")
```

Expand Down

0 comments on commit 971545f

Please sign in to comment.