library(palmerpenguins) # penguins data
library(stringr) # For title
library(ggplot2)
library(dplyr)
1.
str(penguins)
tibble [344 x 8] (S3: tbl_df/tbl/data.frame)
$ species : Factor w/ 3 levels "Adelie","Chinstrap",..: 1 1 1 1 1 1 1 1 1 1 ...
$ island : Factor w/ 3 levels "Biscoe","Dream",..: 3 3 3 3 3 3 3 3 3 3 ...
$ bill_length_mm : num [1:344] 39.1 39.5 40.3 NA 36.7 39.3 38.9 39.2 34.1 42 ...
$ bill_depth_mm : num [1:344] 18.7 17.4 18 NA 19.3 20.6 17.8 19.6 18.1 20.2 ...
$ flipper_length_mm: int [1:344] 181 186 195 NA 193 190 181 195 193 190 ...
$ body_mass_g : int [1:344] 3750 3800 3250 NA 3450 3650 3625 4675 3475 4250 ...
$ sex : Factor w/ 2 levels "female","male": 2 1 1 NA 1 2 1 2 NA NA ...
$ year : int [1:344] 2007 2007 2007 2007 2007 2007 2007 2007 2007 2007 ...
a.
### binwidth
bw <- 500
penguins %>%
ggplot(aes(x = body_mass_g)) +
geom_histogram(binwidth = bw) +
labs(x = "Body mass", y = "Count", title = "Histogram for the body mass", subtitle = str_c("binwidth = ", bw))
b.
penguins %>%
ggplot(aes(x = sex, y = body_mass_g)) +
geom_boxplot() +
labs(x = "Sex", y = "Body mass", title = "Side-by-side boxplot for the body mass")
c.
penguins %>%
ggplot(aes(x = species)) +
geom_bar() +
labs(x = "Species", y = "Count", title = "Barchart for the number of penguins with different species")
d.
### Remove NA values
penguins %>%
filter(!is.na(sex)) %>%
ggplot(aes(x = flipper_length_mm, y = body_mass_g, fill = sex)) +
geom_point(aes(color = sex, shape = sex)) +
geom_smooth(aes(color = sex)) +
labs(x = "Flipper length", y = "Body mass", title = "Scatterplot for body mass versus flipper length")
2.
a.
penguins %>%
arrange(desc(body_mass_g)) %>%
select(species) %>%
head(5)
b.
penguins %>%
summarise(
mean = mean(body_mass_g, na.rm = TRUE),
median = median(body_mass_g, na.rm = TRUE)
)
c.
penguins %>%
group_by(species) %>%
summarise(
mean = mean(body_mass_g, na.rm = TRUE),
median = median(body_mass_g, na.rm = TRUE)
)
d.
penguins %>%
filter(
sex == "female",
bill_length_mm >= 55
) %>%
select(species, sex)