You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.4 KiB
R

library(tidyverse)
library(palmerpenguins) # install.packages("palmerpenguins")
library(ggthemes)
penguins
view(penguins)
? penguins
# ggplot
ggplot(data = penguins,
mapping = aes(x=flipper_length_mm, y=body_mass_g, colour = species)) +
geom_point() +
geom_smooth(method = "lm")
# ggplot step 2:
ggplot(data = penguins,
mapping = aes(x=flipper_length_mm, y=body_mass_g)) +
geom_point(mapping = aes(colour = species, shape = species)) +
geom_smooth(method = "lm")
# ggplot final step:
ggplot(
data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point(aes(color = species, shape = species), size = 3.5) +
geom_smooth(method = "lm") +
labs(
title = "Body mass and flipper length",
subtitle = "Dimensions for Adelie, Chinstrap, and Gentoo Penguins",
x = "Flipper length [mm]", y = "Body mass [g]",
color = "Species", shape = "Species") +
scale_color_colorblind()
#-=-=-=-=-=-=-=
# Categorical variables:
# CV step 1:
ggplot(data = penguins,
mapping = aes(x=species)) +
geom_bar()
# CV step 2:
ggplot(data = penguins,
mapping = aes(x= fct_infreq(species))) +
geom_bar()
#-=-=-=-=-=-=-=
# Numerical variables:
ggplot(data = penguins,
mapping = aes(x = body_mass_g)) +
geom_histogram(binwidth = 350)
ggplot(penguins, aes(x = body_mass_g)) +
geom_density()
# numerical and categorical
ggplot(penguins, aes(x = species, y = body_mass_g)) +
geom_boxplot()
# geom density
ggplot(penguins, aes(x = body_mass_g, color = species)) +
geom_density(linewidth = 0.75)
ggplot(penguins, aes(x = body_mass_g, color = species, fill = species)) +
geom_density(alpha = 0.5)
# 1.5.2 Two categorical variables
ggplot(penguins, aes(x = island, fill = species)) +
geom_bar()
ggplot(penguins, aes(x = island, fill = species)) +
geom_bar(position = "fill")
# Two numerical variables
ggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point()
# Three or more variables
ggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point(aes(color = species, shape = island), size=3.5)
ggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point(aes(color = species, shape = species)) +
facet_wrap(~island)
ggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point()
setwd("~/lwc/courses/data-science/data-visualization")
ggsave(filename = "penguin-plot.png")