1 Veri Seti

load("D:/OLC_731/DATA/miniPISA.rda")
load("D:/OLC_731/DATA/PISA_OGR_2018.rda")
 load("D:/OLC_731/DATA/midiPISA.rda")

2 Gerekli Paketler

library(expss)
## Loading required package: maditr
## 
## To aggregate several columns with one summary: take(mtcars, mpg, hp, fun = mean, by = am)
## 
## Attaching package: 'expss'
## The following object is masked from 'package:ggplot2':
## 
##     vars
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:expss':
## 
##     compute, contains, na_if, recode, vars, where
## The following objects are masked from 'package:maditr':
## 
##     between, coalesce, first, last
## The following object is masked from 'package:kableExtra':
## 
##     group_rows
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
## 
## Attaching package: 'tidyr'
## The following objects are masked from 'package:expss':
## 
##     contains, nest
library(tibble)  
library(haven)
## 
## Attaching package: 'haven'
## The following objects are masked from 'package:expss':
## 
##     is.labelled, read_spss
library(sjlabelled)
## 
## Attaching package: 'sjlabelled'
## The following objects are masked from 'package:haven':
## 
##     as_factor, read_sas, read_spss, read_stata, write_sas, zap_labels
## The following object is masked from 'package:dplyr':
## 
##     as_label
## The following object is masked from 'package:expss':
## 
##     read_spss
## The following object is masked from 'package:ggplot2':
## 
##     as_label
library(psych)
## 
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
library(pastecs)
## 
## Attaching package: 'pastecs'
## The following object is masked from 'package:tidyr':
## 
##     extract
## The following objects are masked from 'package:dplyr':
## 
##     first, last
## The following objects are masked from 'package:maditr':
## 
##     first, last
midiPISA %>% 
# "O_" başlayan ve "OD" içeren değişkenlerin seçimi
  select(starts_with("OD") & contains("MA")) %>% 
  lapply(.,sd) # her bir değişkenin standart sapmasının hesaplanması
## $ODOKUMA1
## [1] 87.78006
## 
## $ODOKUMA2
## [1] 87.696
## 
## $ODOKUMA3
## [1] 87.07692
## 
## $ODOKUMA4
## [1] 87.40305
## 
## $ODOKUMA5
## [1] 87.21323
midiPISA %>% 
select(starts_with("OD") & contains("MA")) %>% 
summarise(across(everything(), list(sd = sd),na.rm=TRUE)) 
## Warning: There was 1 warning in `summarise()`.
## ℹ In argument: `across(everything(), list(sd = sd), na.rm = TRUE)`.
## Caused by warning:
## ! The `...` argument of `across()` is deprecated as of dplyr 1.1.0.
## Supply arguments directly to `.fns` through an anonymous function instead.
## 
##   # Previously
##   across(a:b, mean, na.rm = TRUE)
## 
##   # Now
##   across(a:b, \(x) mean(x, na.rm = TRUE))
# her bir değişkenin standart sapmasının hesaplanması
#group_by ile de yapılabilir

2.1 Özetleyici İstatistiklerin Sunulması

describe(midiPISA %>% 
select(CINSIYET,ODOKUMA1)) # özetleyici istatistiklerinin hesaplanması

2.2 Değişkenlik Katsatısı

round(stat.desc(midiPISA %>% 
select(CINSIYET,OKUMA_ZEVK)),2)  # değişkenlik katsayısının hesaplanması

2.3 Mod

tab <- table(midiPISA$Anne_Egitim) # her benzersiz değer için oluşum sayısı
tab
## 
##    0    1    2    3    4    5    6 
##  695 1882 1362  575  675  759  887

2.4 Frekans Tablosu Oluşturma

library(dplyr)
midiPISA %>%
  group_by(Anne_Egitim) %>% # Anne_Egitim e göre gruplandırma
  count() %>%  # frekans tablosu oluşturma
  ungroup() # gruplandırmanın kaldırılması

2.5 Özet Tablosu Oluşturma

midiPISA %>%
  summarise(ort = mean(ODOKUMA1), # ortalama
            sd = sd(ODOKUMA1),    # standart sapma 
            n = n())              # frekans hesaplama

3 VERİ GÖRSELLEŞTİRME

# install.packages("tidyverse", repos="https://cran.rstudio.com") # paketin yüklenmesi
library("tidyverse") # paketin aktifleştirilmesi
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.0     ✔ readr     2.1.5
## ✔ lubridate 1.9.3     ✔ stringr   1.5.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ psych::%+%()             masks ggplot2::%+%()
## ✖ psych::alpha()           masks ggplot2::alpha()
## ✖ forcats::as_factor()     masks sjlabelled::as_factor(), haven::as_factor()
## ✖ sjlabelled::as_label()   masks dplyr::as_label(), ggplot2::as_label()
## ✖ dplyr::between()         masks maditr::between()
## ✖ dplyr::coalesce()        masks maditr::coalesce()
## ✖ readr::cols()            masks maditr::cols()
## ✖ dplyr::compute()         masks expss::compute()
## ✖ tidyr::contains()        masks dplyr::contains(), expss::contains()
## ✖ pastecs::extract()       masks tidyr::extract()
## ✖ dplyr::filter()          masks stats::filter()
## ✖ pastecs::first()         masks dplyr::first(), maditr::first()
## ✖ stringr::fixed()         masks expss::fixed()
## ✖ dplyr::group_rows()      masks kableExtra::group_rows()
## ✖ haven::is.labelled()     masks expss::is.labelled()
## ✖ purrr::keep()            masks expss::keep()
## ✖ dplyr::lag()             masks stats::lag()
## ✖ pastecs::last()          masks dplyr::last(), maditr::last()
## ✖ purrr::modify()          masks expss::modify()
## ✖ purrr::modify_if()       masks expss::modify_if()
## ✖ dplyr::na_if()           masks expss::na_if()
## ✖ tidyr::nest()            masks expss::nest()
## ✖ sjlabelled::read_sas()   masks haven::read_sas()
## ✖ sjlabelled::read_spss()  masks haven::read_spss(), expss::read_spss()
## ✖ sjlabelled::read_stata() masks haven::read_stata()
## ✖ dplyr::recode()          masks expss::recode()
## ✖ stringr::regex()         masks expss::regex()
## ✖ purrr::transpose()       masks maditr::transpose()
## ✖ dplyr::vars()            masks expss::vars(), ggplot2::vars()
## ✖ purrr::when()            masks expss::when()
## ✖ dplyr::where()           masks expss::where()
## ✖ sjlabelled::write_sas()  masks haven::write_sas()
## ✖ sjlabelled::zap_labels() masks haven::zap_labels()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
#base paketini evde kendin çalıştır
library(dplyr)
library(magrittr)
## 
## Attaching package: 'magrittr'
## The following object is masked from 'package:purrr':
## 
##     set_names
## The following object is masked from 'package:pastecs':
## 
##     extract
## The following object is masked from 'package:tidyr':
## 
##     extract
## The following objects are masked from 'package:expss':
## 
##     and, equals, not, or
library(haven)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
load("D:/OLC_731/DATA/PISA_OGR_2018.rda")
miniPISA <- PISA_OGR_2018 %>%
select(CINSIYET, SINIF,KITAPSAYISI, SES, Anne_Egitim, Baba_Egitim,Okuloncesi_yil,OKUL_TUR,OKUMA_ZEVK,
       OK_YETERLIK,ODOKUMA1) 
# kategorik değişkenlerin faktör olarak kaydı
miniPISA<- miniPISA %>% mutate_if(is.labelled, sjlabelled::as_factor)
#etiketli spps verileri için kullanabiliriz

# Faktör değiskenlere düzey atama amacıyla yazılan fonksiyon
levelsnames <- function(x){
  levels(x) <- names(attr(x,"labels"))
  x
}
# yazılan fonksiyonun faktör değişkenlere uygulanması
miniPISA <-mutate_if(miniPISA,is.factor, levelsnames)
head(miniPISA)

3.1 GGPLOT2 paketinin Yüklenmesi

library(ggplot2)
#qplot(x, y, data, geom) #quik fonksiyonu
#ggplot(x, y, data, geom) # yapmak istediklerimizi %>%  yerine + kullanıyoruz
#ggplot( data, aes(x, y)) + geom.grafikismi() #temel sistemin oluşturulması 
#ggplot(x, y, data, geom="density") 
#ggplot( data, aes(x, y)) + geom.density()
ggplot(miniPISA, aes(x=ODOKUMA1))+
  geom_density()

3.2 Yoğunluk Grafiği

ggplot(miniPISA, aes(x=ODOKUMA1))+
  geom_density(linetype="dashed", fill="pink") +theme_light()

?linetype #0 = blank, 1 = solid, 2 = dashed, 3 = dotted, 4 = dotdash, 5 = longdash, 6 = twodash
## starting httpd help server ... done

3.3 HİSTOGRAM

grafik_1 <- ggplot(miniPISA, aes(x=ODOKUMA1))+ geom_histogram() #ilk katmanın oluşturulması
grafik_1 # oluşturulan nesnenin kaydedilmesi
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

#+ geom ile yemi katmanlar eklenir

3.4 Temel Grafikler

grafik_1 +
  geom_histogram()  +       
  facet_wrap(~CINSIYET, ncol=2) # cinsiyet değişkenindeki faktörlerin sütunda yer alması
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

grafik_1 +
  geom_histogram()  +       
  facet_wrap(~CINSIYET, nrow=2) # cinsiyet değişkenindeki faktörlerin sütunda yer alması
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

3.5 grid

#facet_grid(satırdeğişkeni~sütundeğişkeni)
#facet_grid(satırdeğişkeni~.) 
#facet_grid(.~sütundeğişkeni)
#çapraz tablo gibi grafik oluşturma
library(haven)
miniPISA <- 
  miniPISA %>% 
  mutate_if(is.labelled, funs(as_factor(.)))
## Warning: `funs()` was deprecated in dplyr 0.8.0.
## ℹ Please use a list of either functions or lambdas:
## 
## # Simple named list: list(mean = mean, median = median)
## 
## # Auto named with `tibble::lst()`: tibble::lst(mean, median)
## 
## # Using lambdas list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
 ggplot(miniPISA, aes(x=ODOKUMA1)) +
  geom_histogram()+  #histogram çizilmesi
  facet_grid(SINIF~CINSIYET) # sınıf ve cinsiyete göre yüzey eklenmesi
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

   miniPISA %>% filter( SINIF== "SINIF 9") %>% 

  ggplot(miniPISA %>% filter( SINIF=="SINIF 9") , aes (x= ODOKUMA1)) +
  geom_histogram()+  #histogram çizilmesi
  facet_wrap( ~CINSIYET) + theme_apa()

##color ile gruplandırma

p1 <- ggplot(
      miniPISA %>% 
      group_by(SINIF,CINSIYET) %>% 
      mutate(ort=mean(ODOKUMA1)) %>%  # ortalama puanların sütun olarak ekleme
        ungroup(), # gruplamanın iptal edilmesi
     aes(x=SINIF, y=ort, color=CINSIYET )) + # cinsiyete göre sınıf düzeyinde ortalamaların verilmesi
  geom_point() +  #saçılım grafiği oluşturma
  xlab("Sınıf Düzeyi")+ # x eksenine etiket verilmesi
  ylab("Ortalama Puan") #y eksenine etiket verilmesi

p1 # nesnenin kaydedilmesi

p1+ggtitle("Cinsiyet ve Sınıf Düzeyine Göre Ortalama" )

3.6 Facet_grid

ggplot(miniPISA,aes(x=ODOKUMA1, y=OKUMA_ZEVK, color=CINSIYET)) + 
  geom_point() +  #saçılım grafiği çizilmesi
  facet_grid(.~SINIF) #yüzey ekleme birden fazla sütun 
## Warning: Removed 69 rows containing missing values or values outside the scale range
## (`geom_point()`).

3.7 grafik nesenesi olarak kaydetme

p2 <- ggplot(miniPISA, aes(x=SINIF, y=ODOKUMA1)) + # x ve y eksenlerini belirleme
  geom_point() # saçılım grafiği çizme
p2

3.8 Color

ggplot(miniPISA, aes(CINSIYET, OK_YETERLIK)) +
  geom_point(color = "purple") # saçılım grafiğinin rengini seçme
## Warning: Removed 199 rows containing missing values or values outside the scale range
## (`geom_point()`).

3.9 size ve shape parametreleri

ggplot(miniPISA, aes(CINSIYET, OK_YETERLIK)) +
  geom_point(color = "blue",size=5, shape="a") # saçılım grafiğinin rengini, size büyüklük
## Warning: Removed 199 rows containing missing values or values outside the scale range
## (`geom_point()`).

ggplot(miniPISA, aes(SINIF, OK_YETERLIK, color = CINSIYET)) + # cinsiyete göre gruplandırma
  geom_point() # saçılılm grafiği çizme
## Warning: Removed 199 rows containing missing values or values outside the scale range
## (`geom_point()`).

3.10 position

ggplot(miniPISA, aes(SINIF, OK_YETERLIK, color = CINSIYET)) + # cinsiyete göre renk 
  # açısından gruplandırma
  geom_point(position = "jitter") # üst üste gelen noktaları kaydırma
## Warning: Removed 199 rows containing missing values or values outside the scale range
## (`geom_point()`).

3.11 Size Parametresi

ggplot(miniPISA, aes(CINSIYET, OK_YETERLIK, size = SINIF)) + #sınıf değişkenine göre büyüklük 
  # açısından gruplandırma
  geom_point() #saçılım grafiği çizme
## Warning: Using size for a discrete variable is not advised.
## Warning: Removed 199 rows containing missing values or values outside the scale range
## (`geom_point()`).

# sınıf değişkenine göre büyüklük açısından gruplandırma
ggplot(miniPISA, aes(CINSIYET, OK_YETERLIK, size = SINIF)) +
  geom_point(position = "jitter") # üst üste gelen noktaları kaydırma
## Warning: Using size for a discrete variable is not advised.
## Warning: Removed 199 rows containing missing values or values outside the scale range
## (`geom_point()`).

ggplot(miniPISA, aes(CINSIYET, OK_YETERLIK, size = SINIF)) + 
  geom_point(position = position_jitter(width = 0.2, height = 0.1))
## Warning: Using size for a discrete variable is not advised.
## Warning: Removed 199 rows containing missing values or values outside the scale range
## (`geom_point()`).

3.12 Katmanlar

ggplot(miniPISA, aes(ODOKUMA1, OK_YETERLIK, color = SINIF)) +
  geom_point(alpha = 0.3)
## Warning: Removed 199 rows containing missing values or values outside the scale range
## (`geom_point()`).

grafik1 <- ggplot(miniPISA, aes(ODOKUMA1, OK_YETERLIK, color = SINIF))
grafik1 +geom_point(alpha = 1.2)
## Warning: Removed 199 rows containing missing values or values outside the scale range
## (`geom_point()`).

ggplot(miniPISA[1:10,], aes(ODOKUMA1, OK_YETERLIK))+
         geom_text(aes(label = CINSIYET))

## Scale

#x eksenin kategorik veya sürekli olmasına göre değişiklik gösterebilir.
#eksen limeitlerini ve düzeylerini düzenlemek için kullanılır.
 sekiller <- data.frame(sekil = 0:24)
ggplot(sekiller, aes(0, 0, shape = sekil)) +
  geom_point(aes(shape = sekil), size = 5, fill = 'red') +
  scale_shape_identity() + 
  facet_wrap(~sekil) +
  theme_void()

ggplot(miniPISA, aes(x = ODOKUMA1,y = OK_YETERLIK, color = CINSIYET)) +
geom_point(position = "jitter") +
scale_color_manual(values = c("red", "blue")) #defult turuncu ve yeşildi değiştirmek için kod
## Warning: Removed 199 rows containing missing values or values outside the scale range
## (`geom_point()`).

ggplot(miniPISA, aes(x = ODOKUMA1,y = OK_YETERLIK,color = CINSIYET)) + # cinsiyete göre gruplandırma
geom_point(position = "jitter") +  # üst üste gelen noktaları kaydırma 
scale_x_continuous("Okuma Puanları",limits = c(100,900)) + # x eksenindeki okuma puanlarını sınırlandırma
scale_color_discrete("Cinsiyet",labels=c("KIZ","ERKEK"))  # cinsiyete göre  scale_color_discrete yeniden adlandırma
## Warning: Removed 199 rows containing missing values or values outside the scale range
## (`geom_point()`).

3.13 Breaks

ggplot(miniPISA, aes(x = ODOKUMA1,
y = OK_YETERLIK,
color = CINSIYET)) +
geom_point(position = "jitter") +
scale_x_continuous("Okuma Puanları",limits = c(100,900),
         breaks=seq(100,900,100)) + #100 er 100 er artırma
  scale_color_discrete("Cinsiyet")
## Warning: Removed 199 rows containing missing values or values outside the scale range
## (`geom_point()`).

3.14 Expand

ggplot(miniPISA, aes(x = ODOKUMA1,
y = OK_YETERLIK,
color = CINSIYET)) +
geom_point(position = "jitter") +
scale_x_continuous("Okuma Puanları",limits = c(100,900),
                   breaks=seq(100,900,100), expand=c(0,0)) + #sınırları gerçek değerde tutma
scale_color_discrete("Cinsiyet")
## Warning: Removed 199 rows containing missing values or values outside the scale range
## (`geom_point()`).

3.15 Labs

#isimlendirme yapma
ggplot(miniPISA, aes(x = ODOKUMA1,
y = OK_YETERLIK,
color = CINSIYET)) +
geom_point(position = "jitter") +
  labs(x = "\nBasari Puanları", #\n boşluk bırakma
       y = "\n\nYeterlik Puanları",
       color = "Grup")
## Warning: Removed 199 rows containing missing values or values outside the scale range
## (`geom_point()`).

3.16 BAR GRAFİĞİ

ggplot(miniPISA, aes(CINSIYET, fill = SINIF)) + geom_bar() + # bar grafiği
  labs(x = "Cinsiyet",
       y = "Frekans") #etiketleme yapılması

#Birden fazla kategorik değişkenin gösterilmesi ; bar 
ggplot(data = miniPISA, mapping = aes(x = CINSIYET)) + #aes ile yaptığımız şey mapp'in işlevleri
  geom_bar() 

3.17 Bar grafiğinde yüzde çizme

ggplot(data = miniPISA, mapping = aes(x = CINSIYET)) + 
  geom_bar(aes(y = (..count..)/sum(..count..)))+ 
  scale_y_continuous(name = "Yüzde", labels=scales::percent) # "scales ::" paketi etkinleştirmeden işlevi kullanma  
## Warning: The dot-dot notation (`..count..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(count)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

ggplot(data = miniPISA, mapping = aes(x = CINSIYET)) + 
  geom_bar(aes(y = (..count..)/sum(..count..)))+ 
  scale_y_continuous(name = "Yüzde", labels=scales::percent) 

3.18 Scale fiil fonskiyonu

ggplot(miniPISA, aes(CINSIYET, fill = SINIF)) +
  geom_bar() +
   labs(x = "Cinsiyet",
       y = "Frekans") +
  scale_fill_manual("CINSIYET", values = c("red","blue","orange","green", #kategorinin renklerini değiştirme
                                           "darkblue","purple"))

DeBruine, L., & Barr, D. (2022). Data skills for reproducible research. Zenodo. https://doi.org/10.5281/zenodo.6527194
R Core Team. (2022). R: A language and environment for statistical computing. R Foundation for Statistical Computing. https://www.R-project.org/
Xie, Y. (2016). Bookdown: Authoring books and technical documents with R markdown. Chapman; Hall/CRC. https://bookdown.org/yihui/bookdown
Xie, Y. (2022). Bookdown: Authoring books and technical documents with r markdown. https://github.com/rstudio/bookdown