Skip to content

Commit

Permalink
updating packages in set up and adding labs function to data viz lesson
Browse files Browse the repository at this point in the history
  • Loading branch information
camilavargasp committed Feb 28, 2024
1 parent 2c32873 commit 27b025b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
46 changes: 22 additions & 24 deletions materials/sections/visualization-ggplot-leaflet.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,11 @@ Let's dive into creating and customizing plots with `ggplot2`.
library(readr)
library(dplyr)
library(tidyr)
library(forcats) # makes working with factors easier
library(ggplot2)
library(janitor) # expedite cleaning and exploring data
library(scales) # scale functions for visualization
library(leaflet) # interactive maps
library(DT) # interactive tables
library(scales) # scale functions for visualization
library(janitor) # expedite cleaning and exploring data
library(viridis) # colorblind friendly color pallet
```

Expand Down Expand Up @@ -316,8 +314,8 @@ ggplot(annual_esc_2000s,

Now let's work on making this plot look a bit nicer. We are going to"

- Add a title using `ggtitle()`
- Adjust labels using `ylab()`
- Add a title using `labs()`
- Adjust labels using `labs()`
- Include a built in theme using `theme_bw()`

There are a wide variety of built in themes in `ggplot` that help quickly set the look of the plot. Use the RStudio autocomplete `theme_` `<TAB>` to view a list of theme functions.
Expand All @@ -332,8 +330,8 @@ ggplot(annual_esc_2000s,
geom_point() +
facet_wrap( ~ sasap_region,
scales = "free_y") +
ylab("Escapement") +
ggtitle("Annual Salmon Escapement by Region") +
labs(title = "Annual Salmon Escapement by Region",
y = "Escapement") +
theme_bw()
```

Expand Down Expand Up @@ -374,8 +372,8 @@ ggplot(annual_esc_2000s,
geom_point() +
facet_wrap( ~ sasap_region,
scales = "free_y") +
ylab("Escapement") +
ggtitle("Annual Salmon Escapement by Region") +
labs(title = "Annual Salmon Escapement by Region",
y = "Escapement") +
theme_light() +
theme(legend.position = "bottom",
legend.title = element_blank())
Expand Down Expand Up @@ -404,8 +402,8 @@ ggplot(annual_esc_2000s,
geom_point() +
facet_wrap( ~ sasap_region,
scales = "free_y") +
ylab("Escapement") +
ggtitle("Annual Salmon Escapement by Region") +
labs(title = "Annual Salmon Escapement by Region",
y = "Escapement") +
my_theme
```

Expand Down Expand Up @@ -439,8 +437,8 @@ ggplot(annual_esc_2000s,
scale_x_continuous(breaks = seq(2000, 2016, 2)) +
facet_wrap( ~ sasap_region,
scales = "free_y") +
ylab("Escapement") +
ggtitle("Annual Salmon Escapement by Region") +
labs(title = "Annual Salmon Escapement by Region",
y = "Escapement") +
my_theme +
theme(axis.text.x = element_text(angle = 45,
vjust = 0.5))
Expand All @@ -466,8 +464,8 @@ ggplot(annual_esc_2000s,
scale_y_continuous(labels = comma) +
facet_wrap( ~ sasap_region,
scales = "free_y") +
ylab("Escapement") +
ggtitle("Annual Salmon Escapement by Region") +
labs(title = "Annual Salmon Escapement by Region",
y = "Escapement") +
my_theme +
theme(axis.text.x = element_text(angle = 45,
vjust = 0.5))
Expand All @@ -489,9 +487,8 @@ annual_region_plot <- ggplot(annual_esc_2000s,
scale_y_continuous(labels = comma) +
facet_wrap( ~ sasap_region,
scales = "free_y") +
ylab("Escapement") +
xlab("\nYear") +
ggtitle("Annual Salmon Escapement by Region") +
labs(title = "Annual Salmon Escapement by Region",
y = "Escapement") +
my_theme +
theme(axis.text.x = element_text(angle = 45,
vjust = 0.5))
Expand All @@ -504,6 +501,7 @@ annual_region_plot
```
#### Reordering things
`ggplot()` loves putting things in alphabetical order. But more frequent than not, that's not the order you actually want things to be plotted if you have categorical groups. Let’s find some total years of data by species for Kuskokwim.
Expand All @@ -513,7 +511,7 @@ annual_region_plot
#| message: false
## Number Years of data for each salmon species at Kuskokwim
n_years <- annual_esc %>%
n_years_kusk <- annual_esc %>%
group_by(sasap_region, species) %>%
summarize(n = n()) %>%
filter(sasap_region == "Kuskokwim")
Expand All @@ -523,21 +521,21 @@ Now let's plot this using `geom_bar()`.
```{r}
## base plot
ggplot(n_years,
ggplot(n_years_kusk,
aes(x = species,
y = n)) +
geom_bar(aes(fill = species),
stat = "identity")
```
Now, let's apply some of the customizations we have seen so far and learn some new ones.
Now, let's apply some of the customization we have seen so far and learn some new ones.
```{r}
## Reordering, flipping coords and other customization
ggplot(n_years,
ggplot(n_years_kusk,
aes(
x = fct_reorder(species, n),
x = order(species, n),
y = n,
fill = species
)) +
Expand Down
2 changes: 2 additions & 0 deletions materials/session_11.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ title-block-banner: true





{{< include /sections/visualization-ggplot-leaflet.qmd >}}

0 comments on commit 27b025b

Please sign in to comment.