Skip to content

Commit

Permalink
add more info about plot title customization
Browse files Browse the repository at this point in the history
  • Loading branch information
fmichonneau committed May 18, 2018
1 parent 7b00b9a commit c43a90c
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions _episodes_rmd/04-ggplot2.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,9 @@ Now we can use this new data frame to create our plot showing the
percentage of each house type in each village.

```{r barplot-wall-type}
ggplot(percent_wall_type, aes(x = village, y = percent, fill = respondent_wall_type)) +
geom_bar(stat = "identity", position = "dodge")
```
ggplot(percent_wall_type, aes(x = village, y = percent, fill = respondent_wall_type)) +
geom_bar(stat = "identity", position = "dodge")
```

> ## Exercise
>
Expand Down Expand Up @@ -499,13 +499,14 @@ After our manipulations, you may notice that the values on the x-axis are still
not properly readable. Let's change the orientation of the labels and adjust
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:
labels. With a larger font, the title also runs off. We can "\n" in the string
for the title to insert a new line:

```{r ggplot-customization-label-orientation, purl=FALSE}
ggplot(percent_items, aes(x = village, y = percent)) +
geom_bar(stat = "identity", position = "dodge") +
labs(title = "Percent of respondents in each village who owned each item",
facet_wrap(~ items) +
labs(title = "Percent of respondents in each village \n who owned each item",
x = "Village",
y = "Percent of Respondents") +
theme_bw() +
Expand All @@ -515,19 +516,21 @@ ggplot(percent_items, aes(x = village, y = percent)) +
```

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:
them as an object to be able to easily apply them to other plots you may create.
We can also add `plot.title = element_text(hjust = 0.5)` to center the title:


```{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))
text = element_text(size = 16),
plot.title = element_text(hjust = 0.5))
ggplot(percent_items, aes(x = village, y = percent)) +
geom_bar(stat = "identity", position = "dodge") +
labs(title = "Percent of respondents in each village who owned each item",
facet_wrap(~ items) +
labs(title = "Percent of respondents in each village \n who owned each item",
x = "Village",
y = "Percent of Respondents") +
grey_theme
Expand All @@ -554,14 +557,15 @@ Make sure you have the `fig_output/` folder in your working directory.
```{r ggsave-example, eval=FALSE, purl=FALSE}
my_plot <- ggplot(percent_items, aes(x = village, y = percent)) +
geom_bar(stat = "identity", position = "dodge") +
labs(title = "Percent of respondents in each village who owned each item",
facet_wrap(~ items) +
labs(title = "Percent of respondents in each village \n who owned each item",
x = "Village",
y = "Percent of Respondents") +
theme_bw() +
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))
text = element_text(size = 16),
plot.title = element_text(hjust = 0.5))
ggsave("fig_output/name_of_file.png", my_plot, width = 15, height = 10)
```
Expand Down

0 comments on commit c43a90c

Please sign in to comment.