Harika bir ggplot dokunuşu

dplyr ile Programlama

Dr. Chester Ismay

Educator, Data Scientist, and R/Python Consultant

ggplot2 temelleri

library(ggplot2)
ggplot(
    world_bank_data, 
    aes(x = infant_mortality_rate)
) +

geom_histogram(color = "white")

infant_mortality_rate histogramı

dplyr ile Programlama

Başlık eklemek

ggplot(
    world_bank_data, 
    aes(x = infant_mortality_rate)
) + 
  geom_histogram(color = "white") +

ggtitle("A histogram of infant_mortality_rate")

Başlıklı ggplot2

dplyr ile Programlama

Fonksiyona sarmak

# Önceki grafik kodu
ggplot(world_bank_data, aes(x = infant_mortality_rate)) + 
  geom_histogram(color = "white") +
  ggtitle("A histogram of infant_mortality_rate")
# Bir fonksiyon tanımla
my_histogram <- function(

df, x_var) {
ggplot(df, aes(x = x_var)) + geom_histogram(color = "white") + ggtitle(paste("A histogram of", x_var)) }
dplyr ile Programlama

Fonksiyon üzerinde çalışmak

# Fonksiyona ilk deneme
my_histogram <- function(df, x_var) {
  ggplot(df, aes(x = x_var)) +
    geom_histogram(color = "white") +
    ggtitle(paste("A histogram of", x_var))
}

# Fonksiyonu çağır my_histogram(df = world_bank_data, x_var = infant_mortality_rate)
 Error in paste("A histogram of", x_var) : 
  object 'infant_mortality_rate' not found
dplyr ile Programlama

rlang eklemek

# Fonksiyona ilk deneme
my_histogram <- function(df, x_var) {
  ggplot(df, aes(x = x_var)) +
    geom_histogram(color = "white") +
    ggtitle(paste("A histogram of", x_var))
}
# Hata durmasın diye gerekli düzenlemeler
my_histogram <- function(df, x_var) {
  ggplot(df, aes(x = {{ x_var }})) +

geom_histogram(color = "white") + ggtitle(paste("A histogram of",
as_label(enquo(x_var))) }
dplyr ile Programlama

Fonksiyonumuzu kullanmak

# rlang operatörleriyle tanımlı fonksiyon
my_histogram <- function(df, x_var) {
  ggplot(df, aes(x = {{ x_var }})) +
    geom_histogram(color = "white") +
    ggtitle(paste(
      "A histogram of", 
      as_label(enquo(x_var))
    ))
}

# Fonksiyonu pipe ile çağır! world_bank_data %>% my_histogram(x_var = perc_rural_pop)

rlang ile ggplot2 histogramı

dplyr ile Programlama

Hadi pratik yapalım!

dplyr ile Programlama

Preparing Video For Download...