owned this note
owned this note
Published
Linked with GitHub
# INBO CODING CLUB
26 November, 2019
Welcome!
## Share your code snippet
If you want to share your code snippet, copy paste your snippet within a section of three backticks (```):
As an **example**:
```
library(tidyverse)
...
```
(*you can copy paste this example and add your code further down, but do not fill in your code in this section*)
Your snippets:
### Challenge 1
Difference between `geom_bar` and `geom_histogram`. The first is for discrete variables, the second for continous variables, so yo uneed to group x- values in bins!
In our case, if you put binwidth = 1, you get the same plot:
```
ggplot(data = surveys,
mapping = aes(x = year)) +
geom_bar()
ggplot(data = surveys,
mapping = aes(x = year)) +
geom_histogram(binwidth = 1)
```
Patrik's snippet:
```
## CHALLENGE 1
# Plot the hindfoot_length as function of weight using points
ggplot(data = survey, aes(x = hindfoot_length, y=weight)) + geom_point()
# Plot the weight as function of species using boxplot
ggplot(data = survey, aes(x = species_id, y=weight)) + geom_boxplot()
# Replace the box plot with a violin plot
ggplot(data = survey, aes(x = species_id, y=weight)) + geom_violin()
# How many surveys per gender? Show it as histogram
ggplot(data = survey, aes(x = sex)) + geom_bar()
# How many surveys per year? Show it as histogram
ggplot(data = survey, aes(x = year)) + geom_bar()
```
### Challenge 2
BELANGRIJK!
Definieer in aesthetics (`aes()`) de variabelen die je wil visualiseren. Andere (vaste) properties, definieer je daarbuiten (bv. `alpha` met vaste waarde).
Sander's snippet:
```
ggplot(data = survey, mapping = aes(x = log(weight),
y = hindfoot_length,
color = sex)) +
geom_point(alpha = 0.5) +
ylab("hindfoot length") +
labs(title = "hindfoot length vs weight") +
scale_colour_manual(values = c("F"="red", "M" = "yellow" ), labels = c("M"="Males", "F"="Females"))
```
Emma's snippet:
```
ggplot(data = survey, mapping = aes(x = year, fill = sex)) +
geom_bar(position = "dodge", alpha = 0.5)+
labs(title = "Number of surveys per year", y = "number of surveys") +
coord_flip()
```
Patrik's Snippet:
```
ggplot(data = survey, mapping = aes(x = log(weight),
y = hindfoot_length, color = sex, alpha = 0.5)) +
geom_point() +
ylab("hindfoot length") +
ggtitle ("hindfoot length vs weight") +
scale_color_manual(values=c("F" = "red", "M"= "yellow"))
```
### Challenge 3
Plot Emma:
```
library(plotly)
p <- inat_bxl %>%
mutate(year = as.factor(year)) %>%
ggplot(aes(x = year, fill = species, width = 1)) +
geom_bar(stat = "count", position = "dodge")
ggplotly(p)
```
## Plot Peter
```
inat_bxl_bin <- inat_bxl %>% group_by(year, species) %>% count()
ggplot(data = inat_bxl_bin, mapping = aes(
x = year,
y = factor(species, levels = rev(levels(factor(species))))
)) +
geom_point(aes(size = n)) +
xlab("Year") +
ylab("Species")
```
## Plot Patrik
```
ggplot(data = inat_bxl, mapping = aes(x=year, fill = species)) +
geom_bar(position = "fill") +
facet_wrap(facets = vars(phylum))
```
## Plot Tanja
```
plot6 <- ggplot(data = inat_bxl) +
geom_bar(aes(x = species, fill = as.factor(year)), stat = "count", position = "stack") +
scale_fill_brewer(palette = "Spectral") +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, face = "italic", hjust = 1),
axis.line.x = element_blank(),
axis.ticks.x = element_blank()) +
labs(fill = "Year")
plot6
```
```
inat_bxl_stat <- inat_bxl %>%
group_by(year, species) %>%
count()
plot7 <- ggplot(data = inat_bxl_stat, aes(x = year, y = n)) +
geom_line(colour = "red", size = 1) +
facet_wrap(~ species) +
ylab("count") +
ggtitle("Evolution of observations by year")
plot7
```
## Plot Marijke en Wim
```
ggplot(inat_bxl, aes( x = year)) + geom_bar(fill = "black") +
facet_wrap(facet = vars(species)) +
theme_bw()
```
## Plot Andy
```
ggplot(data = inat_bxl, mapping = aes(x = year))+
geom_bar()+
facet_wrap(~species, scales = "free_y")+
xlab("Jaar") +
ylab("Aantal")+
ggtitle("Aantallen per jaar en per soort")
`
Which plot is your favourite?
Emma
Marijke/Wim *
Peter *
Tanja (plot 6) *